Skip to content

Commit f722e31

Browse files
author
Andrew England
committed
updating examples, removing serial and wire begin from inside of lib
1 parent a12f0cc commit f722e31

File tree

6 files changed

+136
-106
lines changed

6 files changed

+136
-106
lines changed

examples/Example1_BasicReadings/Example1_BasicReadings.ino

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,51 @@
7878
AS726X sensor;
7979

8080
void setup() {
81+
Wire.begin();
82+
Serial.begin(115200);
83+
8184
sensor.begin();
8285
}
8386

8487
void loop() {
8588
sensor.takeMeasurements();
86-
sensor.printMeasurements();
89+
//Prints all measurements
90+
if (sensor.getVersion() == SENSORTYPE_AS7262)
91+
{
92+
//Visible readings
93+
Serial.print(" Reading: V[");
94+
Serial.print(sensor.getCalibratedViolet(), 2);
95+
Serial.print("] B[");
96+
Serial.print(sensor.getCalibratedBlue(), 2);
97+
Serial.print("] G[");
98+
Serial.print(sensor.getCalibratedGreen(), 2);
99+
Serial.print("] Y[");
100+
Serial.print(sensor.getCalibratedYellow(), 2);
101+
Serial.print("] O[");
102+
Serial.print(sensor.getCalibratedOrange(), 2);
103+
Serial.print("] R[");
104+
Serial.print(sensor.getCalibratedRed(), 2);
105+
}
106+
else if (sensor.getVersion() == SENSORTYPE_AS7263)
107+
{
108+
//Near IR readings
109+
Serial.print(" Reading: R[");
110+
Serial.print(sensor.getCalibratedR(), 2);
111+
Serial.print("] S[");
112+
Serial.print(sensor.getCalibratedS(), 2);
113+
Serial.print("] T[");
114+
Serial.print(sensor.getCalibratedT(), 2);
115+
Serial.print("] U[");
116+
Serial.print(sensor.getCalibratedU(), 2);
117+
Serial.print("] V[");
118+
Serial.print(sensor.getCalibratedV(), 2);
119+
Serial.print("] W[");
120+
Serial.print(sensor.getCalibratedW(), 2);
121+
}
122+
123+
Serial.print("] tempF[");
124+
Serial.print(sensor.getTemperatureF(), 1);
125+
Serial.print("]");
126+
127+
Serial.println();
87128
}

examples/Example2_SensorSettings/Example2_SensorSettings.ino

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,51 @@ byte GAIN = 0;
2626
byte MEASUREMENT_MODE = 0;
2727

2828
void setup() {
29+
Wire.begin();
30+
Serial.begin(115200);
31+
2932
sensor.begin(Wire, GAIN, MEASUREMENT_MODE);//Initializes the sensor with non default values
3033
}
3134

3235
void loop() {
3336
sensor.takeMeasurements();
34-
sensor.printMeasurements();
37+
38+
if (sensor.getVersion() == SENSORTYPE_AS7262)
39+
{
40+
//Visible readings
41+
Serial.print(" Reading: V[");
42+
Serial.print(sensor.getCalibratedViolet(), 2);
43+
Serial.print("] B[");
44+
Serial.print(sensor.getCalibratedBlue(), 2);
45+
Serial.print("] G[");
46+
Serial.print(sensor.getCalibratedGreen(), 2);
47+
Serial.print("] Y[");
48+
Serial.print(sensor.getCalibratedYellow(), 2);
49+
Serial.print("] O[");
50+
Serial.print(sensor.getCalibratedOrange(), 2);
51+
Serial.print("] R[");
52+
Serial.print(sensor.getCalibratedRed(), 2);
53+
}
54+
else if (sensor.getVersion() == SENSORTYPE_AS7263)
55+
{
56+
//Near IR readings
57+
Serial.print(" Reading: R[");
58+
Serial.print(sensor.getCalibratedR(), 2);
59+
Serial.print("] S[");
60+
Serial.print(sensor.getCalibratedS(), 2);
61+
Serial.print("] T[");
62+
Serial.print(sensor.getCalibratedT(), 2);
63+
Serial.print("] U[");
64+
Serial.print(sensor.getCalibratedU(), 2);
65+
Serial.print("] V[");
66+
Serial.print(sensor.getCalibratedV(), 2);
67+
Serial.print("] W[");
68+
Serial.print(sensor.getCalibratedW(), 2);
69+
}
70+
71+
Serial.print("] tempF[");
72+
Serial.print(sensor.getTemperatureF(), 1);
73+
Serial.print("]");
74+
75+
Serial.println();
3576
}

examples/Example3_BulbReadings/Example3_BulbReadings.ino

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,45 @@ void setup() {
3131
}
3232

3333
void loop() {
34+
Wire.begin();
35+
Serial.begin(115200);
36+
3437
sensor.takeMeasurementsWithBulb();
35-
sensor.printMeasurements();
38+
39+
if (sensor.getVersion() == SENSORTYPE_AS7262)
40+
{
41+
//Visible readings
42+
Serial.print(" Reading: V[");
43+
Serial.print(sensor.getCalibratedViolet(), 2);
44+
Serial.print("] B[");
45+
Serial.print(sensor.getCalibratedBlue(), 2);
46+
Serial.print("] G[");
47+
Serial.print(sensor.getCalibratedGreen(), 2);
48+
Serial.print("] Y[");
49+
Serial.print(sensor.getCalibratedYellow(), 2);
50+
Serial.print("] O[");
51+
Serial.print(sensor.getCalibratedOrange(), 2);
52+
Serial.print("] R[");
53+
Serial.print(sensor.getCalibratedRed(), 2);
54+
}
55+
else if (sensor.getVersion() == SENSORTYPE_AS7263)
56+
{
57+
//Near IR readings
58+
Serial.print(" Reading: R[");
59+
Serial.print(sensor.getCalibratedR(), 2);
60+
Serial.print("] S[");
61+
Serial.print(sensor.getCalibratedS(), 2);
62+
Serial.print("] T[");
63+
Serial.print(sensor.getCalibratedT(), 2);
64+
Serial.print("] U[");
65+
Serial.print(sensor.getCalibratedU(), 2);
66+
Serial.print("] V[");
67+
Serial.print(sensor.getCalibratedV(), 2);
68+
Serial.print("] W[");
69+
Serial.print(sensor.getCalibratedW(), 2);
70+
}
71+
72+
Serial.print("] tempF[");
73+
Serial.print(sensor.getTemperatureF(), 1);
74+
Serial.print("]");
3675
}

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=SparkFun AS726X
2-
version=1.0.2
2+
version=1.0.3
33
author=Andrew England
44
maintainer=SparkFun Electronics <sparkfun.com>
55
sentence=A library to drive the AMS AS726X NIR/VIS Spectrum Sensor
6-
paragraph=See code for comments.
6+
paragraph=The SparkFun AS726X Visible Spectral Sensor Breakout brings spectroscopy to the palm of your hand, making it easier than ever to measure and characterize how different materials absorb and reflect different wavelengths of light. The AS726X Breakout is unique in its ability to communicate by both an I2C interface and serial interface using AT commands. Hookup is easy, thanks to the Qwiic connectors attached to the board --- simply plug one end of the Qwiic cable into the breakout and the other into one of the Qwiic Shields, then stack the board on a development board. You’ll be ready to upload a sketch to start taking spectroscopy measurements in no time.
77
category=Sensors
88
url=https://github.com/sparkfun/SparkFun_AS726X_Arduino_Library
99
architectures=*

src/AS726X.cpp

Lines changed: 8 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@ AS726X::AS726X()
88

99
}
1010

11-
void AS726X::begin(TwoWire &wirePort, byte gain, byte measurementMode)
11+
bool AS726X::begin(TwoWire &wirePort, byte gain, byte measurementMode)
1212
{
1313
_i2cPort = &wirePort;
14-
_i2cPort->begin();
15-
Serial.begin(115200);
1614
_sensorVersion = virtualReadRegister(AS726x_HW_VERSION);
1715
if (_sensorVersion != 0x3E && _sensorVersion != 0x3F) //HW version for AS7262 and AS7263
1816
{
19-
Serial.print("ID (should be 0x3E or 0x3F): 0x");
20-
Serial.println(_sensorVersion, HEX);
17+
return false;
2118
}
2219

2320
setBulbCurrent(0b00); //Set to 12.5mA (minimum)
@@ -35,12 +32,14 @@ void AS726X::begin(TwoWire &wirePort, byte gain, byte measurementMode)
3532

3633
if (_sensorVersion == 0)
3734
{
38-
Serial.println("Sensor failed to respond. Check wiring.");
39-
while (1); //Freeze!
35+
return false;
4036
}
37+
return true;
38+
}
4139

42-
if (_sensorVersion == SENSORTYPE_AS7262) Serial.println("AS7262 online!");
43-
if (_sensorVersion == SENSORTYPE_AS7263) Serial.println("AS7263 online!");
40+
uint8_t AS726X::getVersion()
41+
{
42+
return _sensorVersion;
4443
}
4544

4645
//Sets the measurement mode
@@ -100,95 +99,6 @@ void AS726X::disableInterrupt()
10099
virtualWriteRegister(AS726x_CONTROL_SETUP, value); //Write
101100
}
102101

103-
//Prints all measurements
104-
void AS726X::printMeasurements()
105-
{
106-
float tempF = getTemperatureF();
107-
108-
if (_sensorVersion == SENSORTYPE_AS7262)
109-
{
110-
//Visible readings
111-
Serial.print(" Reading: V[");
112-
Serial.print(getCalibratedViolet(), 2);
113-
Serial.print("] B[");
114-
Serial.print(getCalibratedBlue(), 2);
115-
Serial.print("] G[");
116-
Serial.print(getCalibratedGreen(), 2);
117-
Serial.print("] Y[");
118-
Serial.print(getCalibratedYellow(), 2);
119-
Serial.print("] O[");
120-
Serial.print(getCalibratedOrange(), 2);
121-
Serial.print("] R[");
122-
Serial.print(getCalibratedRed(), 2);
123-
}
124-
else if (_sensorVersion == SENSORTYPE_AS7263)
125-
{
126-
//Near IR readings
127-
Serial.print(" Reading: R[");
128-
Serial.print(getCalibratedR(), 2);
129-
Serial.print("] S[");
130-
Serial.print(getCalibratedS(), 2);
131-
Serial.print("] T[");
132-
Serial.print(getCalibratedT(), 2);
133-
Serial.print("] U[");
134-
Serial.print(getCalibratedU(), 2);
135-
Serial.print("] V[");
136-
Serial.print(getCalibratedV(), 2);
137-
Serial.print("] W[");
138-
Serial.print(getCalibratedW(), 2);
139-
}
140-
141-
Serial.print("] tempF[");
142-
Serial.print(tempF, 1);
143-
Serial.print("]");
144-
145-
Serial.println();
146-
}
147-
148-
void AS726X::printUncalibratedMeasurements()
149-
{
150-
float tempF = getTemperatureF();
151-
152-
if (_sensorVersion == SENSORTYPE_AS7262)
153-
{
154-
//Visible readings
155-
Serial.print(" Reading: V[");
156-
Serial.print(getViolet());
157-
Serial.print("] B[");
158-
Serial.print(getBlue());
159-
Serial.print("] G[");
160-
Serial.print(getGreen());
161-
Serial.print("] Y[");
162-
Serial.print(getYellow());
163-
Serial.print("] O[");
164-
Serial.print(getOrange());
165-
Serial.print("] R[");
166-
Serial.print(getRed());
167-
}
168-
else if (_sensorVersion == SENSORTYPE_AS7263)
169-
{
170-
//Near IR readings
171-
Serial.print(" Reading: R[");
172-
Serial.print(getR());
173-
Serial.print("] S[");
174-
Serial.print(getS());
175-
Serial.print("] T[");
176-
Serial.print(getT());
177-
Serial.print("] U[");
178-
Serial.print(getU());
179-
Serial.print("] V[");
180-
Serial.print(getV());
181-
Serial.print("] W[");
182-
Serial.print(getW());
183-
}
184-
185-
Serial.print("] tempF[");
186-
Serial.print(tempF, 1);
187-
Serial.print("]");
188-
189-
Serial.println();
190-
}
191-
192102
//Tells IC to take measurements and polls for data ready flag
193103
void AS726X::takeMeasurements()
194104
{

src/AS726X.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212
class AS726X {
1313
public:
1414
AS726X();
15-
void begin(TwoWire &wirePort = Wire, byte gain = 3, byte measurementMode = 3);
15+
bool begin(TwoWire &wirePort = Wire, byte gain = 3, byte measurementMode = 3);
1616
void takeMeasurements();
17+
uint8_t getVersion();
1718
void takeMeasurementsWithBulb();
18-
void printMeasurements();
19-
void printUncalibratedMeasurements();
2019
byte getTemperature();
2120
float getTemperatureF();
2221
void setMeasurementMode(byte mode);

0 commit comments

Comments
 (0)