0

I am trying to read information coming from an Atlas Scientific FLO30 chip.

The following code works fine in an individual sketch:

String inputstring = "";                                                       //a string to hold incoming data from the PC
String sensorstring = "";                                                      //a string to hold the data from the Atlas Scientific product
String LPM="";
float LPM_INT;

boolean input_stringcomplete = false;                                          //have we received all the data from the PC
boolean sensor_stringcomplete = false;                                         //have we received all the data from the Atlas Scientific product
#define CIRC_PUMP_PIN 10 // circulation pump pin - high when on, low when off

unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change :
const long interval = 1000;   



  void setup(){                                                                //set up the hardware
     Serial.begin(9600);                                                       //set baud rate for the hardware serial port_0 to 9600
     Serial3.begin(38400);                                                      //set baud rate for software serial port_3 to 9600
     inputstring.reserve(5);                                                   //set aside some bytes for receiving data from the PC
     sensorstring.reserve(30);                                                 //set aside some bytes for receiving data from Atlas Scientific product
     pinMode(CIRC_PUMP_PIN,OUTPUT);
     digitalWrite(CIRC_PUMP_PIN,HIGH);


void loop(){



unsigned long currentMillis = millis();

  if(currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

            if(Serial3.available()>0){
              while(sensor_stringcomplete==false){
        char inchar = (char)Serial3.read();                              //get the char we just received
              sensorstring += inchar;                                          //add it to the inputString
              Serial.println("adding to string");
            if(inchar == '\r') {sensor_stringcomplete = true;}               //if the incoming character is a <CR>, set the flag 
             }
            }


 if (sensor_stringcomplete){                                                   //if a string from the Atlas Scientific product has been received in its entierty 
      Serial.println(sensorstring);                                            //send that string to to the PC's serial monitor
      byte n = sensorstring.length();
      LPM = sensorstring.substring(n-8,n);
      Serial.print("This is the extracted part ");
      Serial.println(LPM);
      LPM_INT=LPM.toFloat();
      Serial.print("Now in float ");
      Serial.println(LPM_INT);
      LPM="";
      sensorstring = "";                                                       //clear the string:
      sensor_stringcomplete = false;                                           //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
      }

      if(sensor_stringcomplete==false){
       Serial.println("Skipping");

      }
  }




      }

however when I try to integrate the code with another master sketch, the Arduino seems to jam or lockup when it enters the following part of the code, which is called periodically from the main void loop():

    if(Serial3.available()>0){
      while(sensor_stringcomplete==false){
char inchar = (char)Serial3.read();                              //get the char we just received
      sensorstring += inchar;                                          //add it to the inputString
      Serial.println("adding to string");
    if(inchar == '\r') {sensor_stringcomplete = true;}               //if the incoming character is a <CR>, set the flag 
     }
        }

All the code is the same and I do not understand why the Arduino locks up. I intially thought it was a problem with memory as the master code is singificantly bigger but the line "sensorstring = ""; " and "sensorstring.reserve(30) " should take care of this? Besides it cannot even get one reading from the sensor.

Can anyone give insight as to why this might be?

0

Firstly, don't use String. It's wasteful of memory and the dynamic aspects of it don't sit well on a tiny resource-starved 8-bit microcontroller.

Secondly, your method of reading from the serial is flawed right from the start.

if(Serial3.available()>0){

... If there is at least 1 character available ...

while(sensor_stringcomplete==false){
    char inchar = (char)Serial3.read(); //get the char we just received
    sensorstring += inchar;     //add it to the inputString
    Serial.println("adding to string");
    if(inchar == '\r') {sensor_stringcomplete = true;} //if the incoming character is a <CR>, set the flag 
}

... read characters until I get the end of string character regardless of if those characters are even available.

In order to read the data from the serial port you have to ensure that the data is actually available. Just waiting for the first character to arrive and then reading (much faster than the serial is able to receive) other characters is doomed to failure.

I have discussed this at great length in a blog article, including how to perform non-blocking string reads that will not interfere with the rest of your program: http://hacking.majenko.co.uk/reading-serial-on-the-arduino

It even mentions your specific situation since it is something that is so commonly done wrong.

| improve this answer | |
  • Thanks Majenko, interesting stuff. I thought it could be a problem like this. I have integrated this readline function into my master code and have placed: " static char buffer[80]; if (readline(Serial3.read(), buffer, 80) > 0) { Serial.print("You entered: >"); Serial.print(buffer); Serial.println("<"); } " But now the there is nothing being printed on the Serial monitor, again the code works in solitary! – Gawnie Jul 1 '15 at 13:43
  • If the code works by itself, but doesn't work when integrated with your sketch, then there must be something else in your sketch that is stopping it. Can you post your entire non-working sketch? – Majenko Jul 1 '15 at 13:56
  • its too long for comments so Ill post it under the original! – Gawnie Jul 1 '15 at 14:46
  • Its too long to add! – Gawnie Jul 1 '15 at 14:51
  • Ok, so put it somewhere like PasteBin and provide a link. – Majenko Jul 1 '15 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.