Skip to content

Commit 8809ba1

Browse files
committed
Update examples, fix bugs.
Made the command mode the first example. Made continuous mode the second example and filled it out. Populated Example 3 for Synchronous start mode. Fixed getStatus function. Fixed startMeasurement function.
1 parent 605227d commit 8809ba1

File tree

5 files changed

+158
-20
lines changed

5 files changed

+158
-20
lines changed

examples/Example01_BasicSetup/Example01_BasicSetup.ino

Lines changed: 0 additions & 13 deletions
This file was deleted.

examples/Example02_OneShot/Example02_OneShot.ino renamed to examples/Example01_Basic_OneShot/Example01_Basic_OneShot.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void loop() {
3232
if(SFE_BUS_OK != myUVSensor.setStartStateMode(START_STATE_ENABLED))
3333
Serial.println("Error starting reading!");
3434

35-
delay((1 << myUVSensor.getConfigConversionTime()));
35+
delay(2+(1 << myUVSensor.getConfigConversionTime()));
3636

3737
if(SFE_BUS_OK != myUVSensor.readAllUV())
3838
Serial.println("Error reading UV.");
@@ -46,4 +46,4 @@ void loop() {
4646

4747
delay(2000);
4848

49-
};
49+
};
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "SparkFun_AS7331.h"
2+
3+
SfeAS7331ArdI2C myUVSensor;
4+
5+
int8_t result = SFE_BUS_OK;
6+
7+
const uint8_t interruptPin = 26;
8+
volatile bool newDataReady = false;
9+
10+
void setup() {
11+
Serial.begin(115200);
12+
while(!Serial){delay(100);};
13+
Serial.println("AS7331 UV A/B/C Continuous mode example.");
14+
15+
// Configure Interrupt.
16+
pinMode(interruptPin, INPUT);
17+
attachInterrupt(digitalPinToInterrupt(interruptPin), dataReadyInterrupt, RISING);
18+
19+
// Initialize sensor and run default setup.
20+
if(myUVSensor.begin() == false) {
21+
Serial.println("Sensor failed to begin. Please check your wiring!");
22+
Serial.println("Spinning...");
23+
while(1);
24+
}
25+
26+
Serial.println("Sensor began.");
27+
28+
// Set break time to 900us (112 * 8us) to account for the time it takes to poll data.
29+
if(SFE_BUS_OK != myUVSensor.setBreakTime(112)) {
30+
Serial.println("Sensor did not set break time properly.");
31+
Serial.println("Spinning...");
32+
while(1);
33+
}
34+
35+
// Set measurement mode and change device operating mode to measure.
36+
if(myUVSensor.startMeasurement(MEAS_MODE_CONT) == false) {
37+
Serial.println("Sensor did not get set properly.");
38+
Serial.println("Spinning...");
39+
while(1);
40+
}
41+
42+
Serial.println("Set mode to continuous. Starting measurement...");
43+
44+
// Begin measurement.
45+
if(SFE_BUS_OK != myUVSensor.setStartStateMode(START_STATE_ENABLED))
46+
Serial.println("Error starting reading!");
47+
48+
}
49+
50+
void loop() {
51+
52+
if(newDataReady) {
53+
newDataReady = false;
54+
55+
if(SFE_BUS_OK != myUVSensor.readAllUV())
56+
Serial.println("Error reading UV.");
57+
58+
Serial.print("UVA:");
59+
Serial.print(myUVSensor.measures.uva);
60+
Serial.print(" UVB:");
61+
Serial.print(myUVSensor.measures.uvb);
62+
Serial.print(" UVC:");
63+
Serial.println(myUVSensor.measures.uvc);
64+
}
65+
66+
}
67+
68+
void dataReadyInterrupt() {
69+
newDataReady = true;
70+
}

examples/Example03_SYNS_Mode/Example03_SYNS_Mode.ino

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,81 @@ SfeAS7331ArdI2C myUVSensor;
44

55
int8_t result = SFE_BUS_OK;
66

7+
const uint8_t synPin = 27;
8+
9+
const uint8_t interruptPin = 26;
10+
volatile bool newDataReady = false;
11+
712
void setup() {
13+
Serial.begin(115200);
14+
while(!Serial){delay(100);};
15+
Serial.println("AS7331 UV A/B/C Synchronous Start mode example.");
16+
17+
// Configure SYN pin.
18+
pinMode(synPin, OUTPUT);
19+
digitalWrite(synPin, HIGH); // Active low, so start high.
20+
21+
// Configure Interrupt.
22+
pinMode(interruptPin, INPUT);
23+
attachInterrupt(digitalPinToInterrupt(interruptPin), dataReadyInterrupt, RISING);
24+
25+
// Initialize sensor and run default setup.
26+
if(myUVSensor.begin() == false) {
27+
Serial.println("Sensor failed to begin. Please check your wiring!");
28+
Serial.println("Spinning...");
29+
while(1);
30+
}
31+
32+
Serial.println("Sensor began.");
33+
34+
// // Set break time to 900us (112 * 8us) to account for the time it takes to poll data.
35+
// if(SFE_BUS_OK != myUVSensor.setBreakTime(112)) {
36+
// Serial.println("Sensor did not set break time properly.");
37+
// Serial.println("Spinning...");
38+
// while(1);
39+
// }
40+
41+
// Set measurement mode and change device operating mode to measure.
42+
if(myUVSensor.startMeasurement(MEAS_MODE_SYNS) == false) {
43+
Serial.println("Sensor did not get set properly.");
44+
Serial.println("Spinning...");
45+
while(1);
46+
}
47+
48+
Serial.println("Set mode to synchronous. Starting measurement...");
49+
50+
// Begin measurement.
51+
if(SFE_BUS_OK != myUVSensor.setStartStateMode(START_STATE_ENABLED))
52+
Serial.println("Error starting reading!");
53+
54+
digitalWrite(synPin, LOW);
55+
delay(1);
56+
digitalWrite(synPin, HIGH);
857

958
}
1059

1160
void loop() {
1261

13-
};
62+
if(newDataReady) {
63+
newDataReady = false;
64+
65+
if(SFE_BUS_OK != myUVSensor.readAllUV())
66+
Serial.println("Error reading UV.");
67+
68+
digitalWrite(synPin, LOW);
69+
delay(1);
70+
digitalWrite(synPin, HIGH);
71+
72+
Serial.print("UVA:");
73+
Serial.print(myUVSensor.measures.uva);
74+
Serial.print(" UVB:");
75+
Serial.print(myUVSensor.measures.uvb);
76+
Serial.print(" UVC:");
77+
Serial.println(myUVSensor.measures.uvc);
78+
}
79+
80+
}
81+
82+
void dataReadyInterrupt() {
83+
newDataReady = true;
84+
}

src/SparkFun_AS7331.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,6 @@ class SfeAS7331Base {
279279
if(_state.pd == POWER_DOWN_ENABLE)
280280
if(SFE_BUS_OK != setPowerState(POWER_DOWN_DISABLE)) return false;
281281

282-
if(_state.opMode != DEVICE_MODE_MEAS)
283-
if(SFE_BUS_OK != setOperationMode(DEVICE_MODE_MEAS)) return false;
284-
285282
if(_state.mmode != measMode)
286283
{
287284
if(SFE_BUS_OK != setStandbyMode(STANDBY_DISABLED))
@@ -290,6 +287,9 @@ class SfeAS7331Base {
290287
return false;
291288
}
292289

290+
if(_state.opMode != DEVICE_MODE_MEAS)
291+
if(SFE_BUS_OK != setOperationMode(DEVICE_MODE_MEAS)) return false;
292+
293293
if(startMeasure)
294294
if(SFE_BUS_OK != setStartStateMode(START_STATE_ENABLED)) return false;
295295

@@ -1090,7 +1090,17 @@ class SfeAS7331Base {
10901090
/// @return 0 if successful, negative if error, positive for warning.
10911091
int8_t getStatus(sfe_as7331_reg_meas_osr_status_t *statusReg)
10921092
{
1093-
return readRegister(SFE_AS7331_REGISTER_MEAS_OSR_STATUS, statusReg, 2U);
1093+
int8_t result = SFE_BUS_OK;
1094+
uint8_t statusRaw[2];
1095+
1096+
result = readRegister(SFE_AS7331_REGISTER_MEAS_OSR_STATUS, statusRaw, 2U);
1097+
1098+
if(SFE_BUS_OK != result)
1099+
return result;
1100+
1101+
statusReg->word = ((uint16_t)statusRaw[1] << 8) | statusRaw[0];
1102+
1103+
return SFE_BUS_OK;
10941104
}
10951105

10961106
/// @brief Gets the operational state register when in configuration operation mode.

0 commit comments

Comments
 (0)