Home › Forums › Mayfly Data Logger › Using AltSoftSerial with Mayfly › Reply To: Using AltSoftSerial with Mayfly
2021-08-18 at 1:19 PM
#15837
I’m struggling to use AltSoftSerial in place of Software Serial to connect an Atlas-Scientific Color sensor to a Mayfly. The script below successfully communicates and reports sensor data as expected with Software Serial (with lines 21 and 26 active instead of lines 22 and 27 as shown below). As shown below I don’t get any data from the sensor. Any advice?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
/** ========================================================================= * 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 <ensign@stroudcenter.org> * Build Environment: Arduino IDE * Hardware Platform: EnviroDIY Mayfly Arduino Datalogger V0.5 * * 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 Grove connection * red wire from RGB sensor to red Grove wire to V on Mayfly Grove connection * white wire from RGB sensor to white Grove wire to D6 on Mayfly Grove connection * green wire from RGB sensor to yellow Grove wire to D5 on Mayfly Grove connection * ======================================================================= */ //#include <SoftwareSerial.h> //include the SoftwareSerial library #include <AltSoftSerial.h> //this is the alternative to SoftwareSerial #define rx 5 //define Mayfly pin 5 as the receive pin #define tx 6 //define Mayfly pin 6 as the transmit pin //SoftwareSerial RGBserial(rx, tx); //define how the software serial port is going to work AltSoftSerial RGBserial(rx, tx); //this is the alternative to SoftwareSerial void setup() { //set up the hardware Serial.begin(9600); //set baud rate for the hardware serial port_0 to 9600 RGBserial.begin(9600); //set baud rate for the software serial port to 9600 pinMode(22, OUTPUT); //On the Mayfly Data Logger, pin 22 supplies power to the Grove ports digitalWrite(22, LOW); } void loop() { // digitalWrite(22, HIGH); //turn on power to the Mayfly's Grove ports delay(500); //wait 500 milliseconds RGBserial.readStringUntil("\r"); //holds for 1 second; this and the subsequent two commands remove the introductory returns from the RGB sensor RGBserial.readStringUntil("\r"); //holds for 1 second RGBserial.readStringUntil("\r"); //holds for 1 second RGBserial.print('R'); //send that string to the Atlas Scientific product RGBserial.print('\r'); //add a <CR> to the end of the string delay(500); //wait 500 milliseconds int red = RGBserial.parseInt(); //myserial.parseFloat is an alternative, but unnecessary here int green = RGBserial.parseInt(); int blue = RGBserial.parseInt(); int lux = RGBserial.parseInt(); while (RGBserial.available() > 0) { // this gets ride of the "*OK" or anything else trailing at the end RGBserial.read(); } 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(1000); // wait ___ milliseconds; adjust this value to set the sampling interval for the sensor } |