Skip to content

Commit 81815e4

Browse files
author
Elias Santistevan
committed
Adds software interrupt example code, fixes some typos throughout
1 parent 3567419 commit 81815e4

File tree

4 files changed

+140
-11
lines changed

4 files changed

+140
-11
lines changed

Examples/Example1_Read_Ambient_Basics/Example1_Read_Ambient_Basics.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ float gain = .125;
3535

3636
// Possible integration times in milliseconds: 800, 400, 200, 100, 50, 25
3737
// Higher times give higher resolutions and should be used in darker light.
38-
int time = 100
38+
int time = 100;
3939
long luxVal = 0;
4040

4141
void setup(){

Examples/Example2_Light_Interrupt/Example2_Light_Interrupt.ino renamed to Examples/Example2_Light_Interrupt_Hardware/Example2_Light_Interrupt_Hardware.ino

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ int numbValues = 1;
4343

4444
// Interrupt pin
4545
int intPin = 3;
46+
int interrupt;
4647

4748
void setup(){
4849

@@ -83,19 +84,24 @@ void setup(){
8384
long highVal = light.readHighThresh();
8485
Serial.println(highVal);
8586

86-
// Now we enable the interrupt, now that he thresholds are set.
87-
light.enableInt();
88-
Serial.print("Is interrupt enabled: ");
89-
light.readIntSetting() ? Serial.println("Yes") : Serial.println("No");
90-
91-
9287
// This setting modifies the number of times a value has to fall below or
9388
// above the threshold before the interrupt fires! Values include 1, 2, 4 and
9489
// 8.
9590
light.setProtect(numbValues);
9691
Serial.print("Number of values that must fall below/above threshold before interupt occurrs: ");
9792
int protectVal = light.readProtect();
9893
Serial.println(protectVal);
94+
95+
// Now we enable the interrupt, now that he thresholds are set.
96+
light.enableInt();
97+
Serial.print("Is interrupt enabled: ");
98+
int intVal = light.readIntSetting();
99+
if(intVal == 1)
100+
Serial.println("Yes");
101+
else
102+
Serial.println("No");
103+
104+
99105
Serial.println("-------------------------------------------------");
100106

101107
// Give some time to read our settings on startup.
@@ -110,12 +116,11 @@ void loop(){
110116
Serial.println(" Lux");
111117

112118
if (digitalRead(intPin) == LOW){
113-
int intVal = light.readInterrupt();
114-
if (intVal == 1)
119+
interrupt = light.readInterrupt();
120+
if (interrupt == 1)
115121
Serial.println("High threshold crossed!");
116-
else if (intVal == 2){
122+
else if (interrupt == 2)
117123
Serial.println("Low threshold crossed!");
118-
}
119124
}
120125

121126
delay(200);
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
This example code will walk you through how to set the interrupts on the
3+
SparkFun VEML6030 Ambient Light Sensor. You can set both high and low
4+
thresholds as well as how many values must be below or above the threshold
5+
before an interrupt occurs. This example does not require the interrupt pin on
6+
the product to be connected a pin on your micro-controller.
7+
8+
SparkFun Electronics
9+
Author: Elias Santistevan
10+
Date: July 2019
11+
12+
License: This code is public domain but if you use this and we meet someday, get me a beer!
13+
14+
Feel like supporting our work? Buy a board from Sparkfun!
15+
https://www.sparkfun.com/products/15436
16+
17+
*/
18+
19+
#include <Wire.h>
20+
#include "SparkFun_VEML6030_Ambient_Light_Sensor.h"
21+
22+
// Close the address jumper on the product for addres 0x10.
23+
#define AL_ADDR 0x48
24+
25+
SparkFun_Ambient_Light light(AL_ADDR);
26+
27+
// Possible values: .125, .25, 1, 2
28+
// Both .125 and .25 should be used in most cases except darker rooms.
29+
// A gain of 2 should only be used if the sensor will be covered by a dark
30+
// glass.
31+
float gain = .125;
32+
33+
// Possible integration times in milliseconds: 800, 400, 200, 100, 50, 25
34+
// Higher times give higher resolutions and should be used in darker light.
35+
int time = 100;
36+
long luxVal = 0;
37+
38+
39+
// Interrupt settings.
40+
long lowThresh = 20;
41+
long highThresh = 400;
42+
int numbValues = 1;
43+
44+
// Interrupt variable
45+
int interrupt;
46+
47+
void setup(){
48+
49+
Wire.begin();
50+
Serial.begin(115200);
51+
52+
if(light.begin())
53+
Serial.println("Ready to sense some light!");
54+
else
55+
Serial.println("Could not communicate with the sensor!");
56+
57+
// Again the gain and integration times determine the resolution of the lux
58+
// value, and give different ranges of possible light readings. Check out
59+
// hoookup guide for more info. The gain/integration time also affects
60+
// interrupt threshold settings so ALWAYS set gain and time first.
61+
light.setGain(gain);
62+
light.setIntegTime(time);
63+
64+
Serial.println("Reading settings...");
65+
Serial.print("Gain: ");
66+
float gainVal = light.readGain();
67+
Serial.print(gainVal, 3);
68+
Serial.print(" Integration Time: ");
69+
int timeVal = light.readIntegTime();
70+
Serial.println(timeVal);
71+
72+
// Set both low and high thresholds, they take values in lux.
73+
light.setIntLowThresh(lowThresh);
74+
light.setIntHighThresh(highThresh);
75+
// Let's check that they were set correctly.
76+
// There are some rounding issues inherently in the IC's design so your lux
77+
// may be one off.
78+
Serial.print("Low Threshold: ");
79+
long lowVal = light.readLowThresh();
80+
Serial.print(lowVal);
81+
Serial.print(" High Threshold: ");
82+
long highVal = light.readHighThresh();
83+
Serial.println(highVal);
84+
85+
// This setting modifies the number of times a value has to fall below or
86+
// above the threshold before the interrupt fires! Values include 1, 2, 4 and
87+
// 8.
88+
light.setProtect(numbValues);
89+
Serial.print("Number of values that must fall below/above threshold before interupt occurrs: ");
90+
int protectVal = light.readProtect();
91+
Serial.println(protectVal);
92+
93+
// Now we enable the interrupt, now that he thresholds are set.
94+
light.enableInt();
95+
Serial.print("Is interrupt enabled: ");
96+
int intVal = light.readIntSetting();
97+
if (intVal == 1)
98+
Serial.println("Yes");
99+
else
100+
Serial.println("No");
101+
102+
103+
Serial.println("-------------------------------------------------");
104+
105+
// Give some time to read our settings on startup.
106+
delay(3000);
107+
}
108+
109+
void loop(){
110+
111+
luxVal = light.readLight();
112+
Serial.print("Ambient Light Reading: ");
113+
Serial.print(luxVal);
114+
Serial.println(" Lux");
115+
116+
117+
interrupt = light.readInterrupt();
118+
if (interrupt == 1)
119+
Serial.println("High threshold crossed!");
120+
else if (interrupt == 2)
121+
Serial.println("Low threshold crossed!");
122+
123+
delay(200);
124+
}

0 commit comments

Comments
 (0)