/** ========================================================================= * RGBsensorAutoMode.ino * Example of using the Atlas Scientific RGB sensor modified from * code provided by Atlas Scientific: * https://atlas-scientific.com/probes/ezo-rgb-embedded-color-sensor/ * * author: Scott Ensign * Build Environment: Arduino IDE * Hardware Platform: EnviroDIY Mayfly Arduino Datalogger V0.3 * * DISCLAIMER: * THIS CODE IS PROVIDED "AS IS" - NO WARRANTY IS GIVEN. * * The wiring from the RGB sensor was spliced to a Grove cable as follows: * black wire from RGB sensor to black Grove wire to grd on Mayfly D6-7 Grove connection * red wire from RGB sensor to red Grove wire to V on Mayfly D6-7 Grove connection * white wire from RGB sensor to white Grove wire to D7 on Mayfly D6-7 Grove connection * green wire from RGB sensor to yellow Grove wire to D6 on Mayfly D6-7 Grove connection * ======================================================================= */ #include //include the SoftwareSerial library #define rx 6 //define Mayfly pin 6 (D6) as the receive pin #define tx 7 //define Mayfly pin 7 (D7) as the transmit pin SoftwareSerial myserial(rx, tx); //define how the software serial port is going to work String sensorstring = ""; //a string to hold the data from the Atlas Scientific product char sensorstring_array[40]; void setup() { //set up the hardware Serial.begin(9600); //set baud rate for the hardware serial port_0 to 9600 myserial.begin(9600); //set baud rate for the software serial port to 9600 sensorstring.reserve(30); //set aside some bytes for receiving data from Atlas Scientific product pinMode(22, OUTPUT); //On the Mayfly Data Logger, pin 22 supplies power to the digitalWrite(22, LOW); } void loop() { // digitalWrite(22, HIGH); //turn on power to the Mayfly's Grove ports delay(500); //wait 500 milliseconds myserial.readStringUntil("\r"); //holds for 1 second; this and the subsequent two commands remove the introductory returns from the RGB sensor myserial.readStringUntil("\r"); //holds for 1 second myserial.readStringUntil("\r"); //holds for 1 second myserial.print('R'); //send that string to the Atlas Scientific product myserial.print('\r'); //add a to the end of the string delay(500); //wait 500 milliseconds int red = myserial.parseInt(); //myserial.parseFloat is an alternative, but unnecessary here int green = myserial.parseInt(); int blue = myserial.parseInt(); int lux = myserial.parseInt(); while (myserial.available() > 0) { // this gets ride of the "*OK" or anything else trailing at the end myserial.read(); } digitalWrite(22, LOW); // turn off power to the sensor Serial.print("Red: "); // print "Red: " to the serial monitor Serial.println(red); // print the red value from the sensor Serial.print("Green: "); // ... Serial.println(green); Serial.print("Blue: "); Serial.println(blue); Serial.print("Lux: "); Serial.println(lux); delay(5000); // wait 5000 milliseconds; adjust this value to set the sampling interval for the sensor }