Skip to content

Commit d8feee1

Browse files
authored
Merge pull request #8 from sparkfun/feature/sf-toolkit-v1
Port to SparkFun Toolkit v1.0
2 parents cbfe624 + 9c8953b commit d8feee1

File tree

8 files changed

+783
-557
lines changed

8 files changed

+783
-557
lines changed
Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/*
22
Using the AMS AS7331 Spectral UV Sensor in Command/One Shot (CMD) Mode.
33
4-
This example shows how operate the AS7331 in the default CMD mode. The start
4+
This example shows how operate the AS7331 in the default CMD mode. The start
55
command is sent, then delays until the conversion time has passed before
66
reading out the UV values.
77
88
By: Alex Brudner
99
SparkFun Electronics
1010
Date: 2023/11/17
1111
SparkFun code, firmware, and software is released under the MIT License.
12-
Please see LICENSE.md for further details.
12+
Please see LICENSE.md for further details.
1313
1414
Hardware Connections:
1515
IoT RedBoard --> AS7331
@@ -23,57 +23,65 @@
2323
*/
2424

2525
#include <Arduino.h>
26-
#include <Wire.h>
2726
#include <SparkFun_AS7331.h>
27+
#include <Wire.h>
2828

2929
SfeAS7331ArdI2C myUVSensor;
3030

31-
void setup() {
32-
Serial.begin(115200);
33-
while(!Serial){delay(100);};
34-
Serial.println("AS7331 UV A/B/C Command (One-shot) mode Example.");
35-
36-
Wire.begin();
37-
38-
// Initialize sensor and run default setup.
39-
if(myUVSensor.begin() == false) {
40-
Serial.println("Sensor failed to begin. Please check your wiring!");
41-
Serial.println("Halting...");
42-
while(1);
43-
}
44-
45-
Serial.println("Sensor began.");
46-
47-
// Set measurement mode and change device operating mode to measure.
48-
if(myUVSensor.prepareMeasurement(MEAS_MODE_CMD) == false) {
49-
Serial.println("Sensor did not get set properly.");
50-
Serial.println("Halting...");
51-
while(1);
52-
}
53-
54-
Serial.println("Set mode to command.");
31+
void setup()
32+
{
33+
Serial.begin(115200);
34+
while (!Serial)
35+
{
36+
delay(100);
37+
};
38+
Serial.println("AS7331 UV A/B/C Command (One-shot) mode Example.");
39+
40+
Wire.begin();
41+
42+
// Initialize sensor and run default setup.
43+
if (myUVSensor.begin() == false)
44+
{
45+
Serial.println("Sensor failed to begin. Please check your wiring!");
46+
Serial.println("Halting...");
47+
while (1)
48+
;
49+
}
50+
51+
Serial.println("Sensor began.");
52+
53+
// Set measurement mode and change device operating mode to measure.
54+
if (myUVSensor.prepareMeasurement(MEAS_MODE_CMD) == false)
55+
{
56+
Serial.println("Sensor did not get set properly.");
57+
Serial.println("Halting...");
58+
while (1)
59+
;
60+
}
61+
62+
Serial.println("Set mode to command.");
5563
}
5664

57-
void loop() {
58-
59-
// Send a start measurement command.
60-
if(kSTkErrOk != myUVSensor.setStartState(true))
61-
Serial.println("Error starting reading!");
62-
63-
// Wait for a bit longer than the conversion time.
64-
delay(2+myUVSensor.getConversionTimeMillis());
65+
void loop()
66+
{
67+
68+
// Send a start measurement command.
69+
if (ksfTkErrOk != myUVSensor.setStartState(true))
70+
Serial.println("Error starting reading!");
6571

66-
// Read UV values.
67-
if(kSTkErrOk != myUVSensor.readAllUV())
68-
Serial.println("Error reading UV.");
72+
// Wait for a bit longer than the conversion time.
73+
delay(2 + myUVSensor.getConversionTimeMillis());
6974

70-
Serial.print("UVA:");
71-
Serial.print(myUVSensor.getUVA());
72-
Serial.print(" UVB:");
73-
Serial.print(myUVSensor.getUVB());
74-
Serial.print(" UVC:");
75-
Serial.println(myUVSensor.getUVC());
75+
// Read UV values.
76+
if (ksfTkErrOk != myUVSensor.readAllUV())
77+
Serial.println("Error reading UV.");
7678

77-
delay(2000);
79+
Serial.print("UVA:");
80+
Serial.print(myUVSensor.getUVA());
81+
Serial.print(" UVB:");
82+
Serial.print(myUVSensor.getUVB());
83+
Serial.print(" UVC:");
84+
Serial.println(myUVSensor.getUVC());
7885

86+
delay(2000);
7987
}

examples/Example02_CONT_Mode/Example02_CONT_Mode.ino

Lines changed: 71 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
SparkFun Electronics
1010
Date: 2023/11/27
1111
SparkFun code, firmware, and software is released under the MIT License.
12-
Please see LICENSE.md for further details.
12+
Please see LICENSE.md for further details.
1313
1414
Hardware Connections:
1515
IoT RedBoard --> AS7331
@@ -24,77 +24,88 @@
2424
*/
2525

2626
#include <Arduino.h>
27-
#include <Wire.h>
2827
#include <SparkFun_AS7331.h>
28+
#include <Wire.h>
2929

3030
SfeAS7331ArdI2C myUVSensor;
3131

3232
const uint8_t interruptPin = 26;
3333
volatile bool newDataReady = false;
3434

35-
void setup() {
36-
Serial.begin(115200);
37-
while(!Serial){delay(100);};
38-
Serial.println("AS7331 UV A/B/C Continuous mode example.");
39-
40-
Wire.begin();
41-
42-
// Configure Interrupt.
43-
pinMode(interruptPin, INPUT);
44-
attachInterrupt(digitalPinToInterrupt(interruptPin), dataReadyInterrupt, RISING);
45-
46-
// Initialize sensor and run default setup.
47-
if(myUVSensor.begin() == false) {
48-
Serial.println("Sensor failed to begin. Please check your wiring!");
49-
Serial.println("Halting...");
50-
while(1);
51-
}
52-
53-
Serial.println("Sensor began.");
54-
55-
// Set the delay between measurements so that the processor can read out the
56-
// results without interfering with the ADC.
57-
// Set break time to 900us (112 * 8us) to account for the time it takes to poll data.
58-
if(kSTkErrOk != myUVSensor.setBreakTime(112)) {
59-
Serial.println("Sensor did not set break time properly.");
60-
Serial.println("Halting...");
61-
while(1);
62-
}
63-
64-
// Set measurement mode and change device operating mode to measure.
65-
if(myUVSensor.prepareMeasurement(MEAS_MODE_CONT) == false) {
66-
Serial.println("Sensor did not get set properly.");
67-
Serial.println("Spinning...");
68-
while(1);
69-
}
70-
71-
Serial.println("Set mode to continuous. Starting measurement...");
72-
73-
// Begin measurement.
74-
if(kSTkErrOk != myUVSensor.setStartState(true))
75-
Serial.println("Error starting reading!");
76-
35+
void setup()
36+
{
37+
Serial.begin(115200);
38+
while (!Serial)
39+
{
40+
delay(100);
41+
};
42+
Serial.println("AS7331 UV A/B/C Continuous mode example.");
43+
44+
Wire.begin();
45+
46+
// Configure Interrupt.
47+
pinMode(interruptPin, INPUT);
48+
attachInterrupt(digitalPinToInterrupt(interruptPin), dataReadyInterrupt, RISING);
49+
50+
// Initialize sensor and run default setup.
51+
if (myUVSensor.begin() == false)
52+
{
53+
Serial.println("Sensor failed to begin. Please check your wiring!");
54+
Serial.println("Halting...");
55+
while (1)
56+
;
57+
}
58+
59+
Serial.println("Sensor began.");
60+
61+
// Set the delay between measurements so that the processor can read out the
62+
// results without interfering with the ADC.
63+
// Set break time to 900us (112 * 8us) to account for the time it takes to poll data.
64+
if (ksfTkErrOk != myUVSensor.setBreakTime(112))
65+
{
66+
Serial.println("Sensor did not set break time properly.");
67+
Serial.println("Halting...");
68+
while (1)
69+
;
70+
}
71+
72+
// Set measurement mode and change device operating mode to measure.
73+
if (myUVSensor.prepareMeasurement(MEAS_MODE_CONT) == false)
74+
{
75+
Serial.println("Sensor did not get set properly.");
76+
Serial.println("Spinning...");
77+
while (1)
78+
;
79+
}
80+
81+
Serial.println("Set mode to continuous. Starting measurement...");
82+
83+
// Begin measurement.
84+
if (ksfTkErrOk != myUVSensor.setStartState(true))
85+
Serial.println("Error starting reading!");
7786
}
7887

79-
void loop() {
88+
void loop()
89+
{
8090

81-
// If an interrupt has been generated...
82-
if(newDataReady) {
83-
newDataReady = false;
91+
// If an interrupt has been generated...
92+
if (newDataReady)
93+
{
94+
newDataReady = false;
8495

85-
if(kSTkErrOk != myUVSensor.readAllUV())
86-
Serial.println("Error reading UV.");
87-
88-
Serial.print("UVA:");
89-
Serial.print(myUVSensor.getUVA());
90-
Serial.print(" UVB:");
91-
Serial.print(myUVSensor.getUVB());
92-
Serial.print(" UVC:");
93-
Serial.println(myUVSensor.getUVC());
94-
}
96+
if (ksfTkErrOk != myUVSensor.readAllUV())
97+
Serial.println("Error reading UV.");
9598

99+
Serial.print("UVA:");
100+
Serial.print(myUVSensor.getUVA());
101+
Serial.print(" UVB:");
102+
Serial.print(myUVSensor.getUVB());
103+
Serial.print(" UVC:");
104+
Serial.println(myUVSensor.getUVC());
105+
}
96106
}
97107

98-
void dataReadyInterrupt() {
99-
newDataReady = true;
108+
void dataReadyInterrupt()
109+
{
110+
newDataReady = true;
100111
}

0 commit comments

Comments
 (0)