Atlas Scientific Ardiuno to Particle code issues

@ScruffR - you’re a gentleman and a scholar.

I got it working, and it also resolved my earlier hard fault issues (SOS & 1 red blink).

The following is the code I’ve used successfully. Any ideas for improvements are most welcome!

#include "application.h"

String sensorstring = "";                             //a string to hold the data from the Atlas Scientific product
boolean sensor_string_complete = false;               //have we received all the data from the Atlas Scientific product
float pH;                                             //used to hold a floating point number that is the pH
int oldTime = 0;

void setup() {                                        //set up the hardware
  Serial.begin(9600);                                 //set baud rate for the hardware serial port_0 to 9600
  Serial1.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
}

void loop() {
  Serial.println('loop begin');
   Serial1.print('r');
   Serial1.print('\r');
   delay(100);

    oldTime = millis();
    while(sensor_string_complete == false && millis()-oldTime < 5000) {
        while(Serial1.available()) {                                        //if we see that the Atlas Scientific product has sent a character
            char inchar = (char)Serial1.read();                             //get the char we just received
            sensorstring += inchar;                                         //add the char to the var called sensorstring
            if (inchar == '\r') {                                           //if the incoming character is a <CR>
                sensor_string_complete = true;                              //set the flag
            }
        }
    }

  if (sensor_string_complete == true) {               //if a string from the Atlas Scientific product has been received in its entirety
    Serial.println(sensorstring); 
    if (isdigit(sensorstring[0])) {                   //if the first character in the string is a digit
      pH = sensorstring.toFloat();                    //convert the string to a floating point number so it can be evaluated by the Arduino
      if (pH >= 7.0) {                                //if the pH is greater than or equal to 7.0
        Serial.println("high");                       //print "high" this is demonstrating that the Arduino is evaluating the pH as a number and not as a string
      }
      if (pH <= 6.999) {                              //if the pH is less than or equal to 6.999
        Serial.println("low");                        //print "low" this is demonstrating that the Arduino is evaluating the pH as a number and not as a string
      }
    }
    sensorstring = "";                                //clear the string
    sensor_string_complete = false;                   //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
  }
  
  delay(1000);
}

Additionally, I’ve tested that the code works on both Photons and Electrons.

1 Like