9
9
SparkFun Electronics
10
10
Date: 2023/11/27
11
11
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.
13
13
14
14
Hardware Connections:
15
15
IoT RedBoard --> AS7331
24
24
*/
25
25
26
26
#include < Arduino.h>
27
- #include < Wire.h>
28
27
#include < SparkFun_AS7331.h>
28
+ #include < Wire.h>
29
29
30
30
SfeAS7331ArdI2C myUVSensor;
31
31
32
32
const uint8_t interruptPin = 26 ;
33
33
volatile bool newDataReady = false ;
34
34
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!" );
77
86
}
78
87
79
- void loop () {
88
+ void loop ()
89
+ {
80
90
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 ;
84
95
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." );
95
98
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
+ }
96
106
}
97
107
98
- void dataReadyInterrupt () {
99
- newDataReady = true ;
108
+ void dataReadyInterrupt ()
109
+ {
110
+ newDataReady = true ;
100
111
}
0 commit comments