2

I'm trying to build a datalogger using Arduino UNO, I'll use RS232 for the communication between PC and Arduino, the problem I'm facing now is that I'm using delay(10000) : 10 seconds as a timestep for data acquisition. however, I can't find any solution to receive a string from the serial input to Arduino while delay is executing. Here is the code :

void setup() {
  Serial.begin(9600);
}

void loop() {
  serialRoutine();
  dataLoggingEvery10Second(); // That's a function to receive data from the sensor and store it in SD card.
  delay(10000);
}

  // A function to handle all the serial in out
  void serialRoutine(){
  // print the string when a newline arrives:
  while(Serial.readString() == ""){
    String serstr = Serial.readString();
      /*
      *  Getters
      */
      if(serstr == "GETDATA"){
        Serial.println("Here is my data");
      } 
      serstr = "";
  }
}

Any help would be highly appreciated !

5
  • You could also look at interrupts. – Code Gorilla Aug 14 '17 at 9:16
  • Why the delay at all? Nothing will happen until it receives GETDATA anyway, so what is the point of the delay? – Majenko Aug 14 '17 at 9:19
  • I can't find any solution to receive a string from the serial input to Arduino while delay is executing. - why do you need to receive data anyway? Aren't you sending it? – Nick Gammon Aug 14 '17 at 9:19
  • Sorry guys, I forgot to put the data acquisition function between serialRoutine(); and delay(10000); – mbarki mohamed Aug 14 '17 at 9:24
  • while(Serial.readString() == ""){String serstr = Serial.readString(); - this seems to me to be reading two strings. – Nick Gammon Aug 14 '17 at 9:56
1
void loop() {
  static unsigned long prev = 0;
  unsigned long now = millis();

  if (now - prev >= 10000L) {
    serialRoutine();
    dataLoggingEvery10Second();
    prev = now;
  }
}

This will executed at time 10s, 20s, etc. I

2
  • Thank you so much ! is this code applicable for a timestep of 15 to 30 minutes ? – mbarki mohamed Aug 14 '17 at 9:13
  • Sure. You only have to change the 10000L value for the interval that you want. – user31481 Aug 14 '17 at 9:20
0

If your PC is sending GETDATA, then it can send it every 10 seconds (or every 15 or 30 minutes) so you don't need any delays at the Arduino end at all.

I have a page about getting serial data without blocking which illustrates how you can be doing other things (like reading sensors) without blocking.

An example sketch is:

/*
Example of processing incoming serial data without blocking.

Author:   Nick Gammon
Date:     13 November 2011. 
Modified: 31 August 2013.

Released for public use.
*/

// how much serial data we expect before a newline
const unsigned int MAX_INPUT = 50;

void setup ()
  {
  Serial.begin (115200);
  } // end of setup

// here to process incoming serial data after a terminator received
void process_data (const char * data)
  {
  // for now just display it
  // (but you could compare it to some value, convert to an integer, etc.)
  Serial.println (data);
  }  // end of process_data

void processIncomingByte (const byte inByte)
  {
  static char input_line [MAX_INPUT];
  static unsigned int input_pos = 0;

  switch (inByte)
    {

    case '\n':   // end of text
      input_line [input_pos] = 0;  // terminating null byte

      // terminator reached! process input_line here ...
      process_data (input_line);

      // reset buffer for next time
      input_pos = 0;  
      break;

    case '\r':   // discard carriage return
      break;

    default:
      // keep adding if not full ... allow for terminating null byte
      if (input_pos < (MAX_INPUT - 1))
        input_line [input_pos++] = inByte;
      break;

    }  // end of switch

  } // end of processIncomingByte  

void loop()
  {
  // if serial data available, process it
  while (Serial.available () > 0)
    processIncomingByte (Serial.read ());

  // do other stuff here like testing digital input (button presses) ...

  }  // end of loop

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.