0

I have the following sketch...

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
}

void loop() {
  if(Serial.available() > 0){
    Serial.println("Hello world from Ardunio!");
  }
}

This works great with my client code...

import SerialPort from 'serialport';
const port = new SerialPort('/dev/ttyACM0', {
  baudRate: 115200,
  autoOpen: false
})

port.open(function (err) {
  if (err) {
    return console.log('Error opening port: ', err.message)
  }

  // Because there's no callback to write, write errors will be emitted on the port:
  console.log("port opened");
  port.on('readable', function () {
    console.log('Data:', port.read())
  })
})

Now I want to slow it down so I change the sketch to...

void loop() {
  if(Serial.available() > 0){
    Serial.println("Hello world from Ardunio!");
  }
  delay(10000);
}

I would expect this to delay each message by 10 seconds, however, it seems to never rerun so I do not get any messages. What am I missing? Why would delay break the loop?

  • What if you double the line Serial.println("Hello world from Ardunio!");? – Brian Cannard Apr 11 at 18:38
  • what happens if you remove the delay? – jsotola Apr 11 at 18:48
  • I thought it was working fine but now I am questioning that as I am having trouble again after going back from a couple of other things I was trying. I am not sure if when it was working I was still accidentally using while. I will try adding a few writes with delays and see if that helps at all. The ultimate goal is to write to the serial port every n seconds. – Jackie Apr 11 at 18:54
  • It just doesn't seem that loop is looping, If I remove the if and add multiple prints and delays it does seem to work. Why doesn't it work if I use the if statement? – Jackie Apr 11 at 18:57
  • Do you write anything to the arduino on the serial line? – Sim Son Apr 11 at 19:16
2

NEVER use delay() in a loop - its a bad habit you get used to and then you return later on with complex code asking why nothing is happening or your webserver is dead for 10 seconds every time you receive a request.

So instead of

void loop() {
  if(Serial.available() > 0){
    Serial.println("Hello world from Ardunio!");
  }
  delay(10000);
 }

you would do

unsigned long timerStart = 0;
unsigned long myTimer = 10000;

setup(){....}

 void loop() {
  if(Serial.available() > 0 ){
     if(millis() - timerStart > myTimer){      // non blocking
       Serial.println("Hello world from Ardunio!");
       timerStart = millis(); // reset timer
     }    
   }
 }

What your code does at the moment is enter the IF clause if exactly in this millis the buffer is filled, then wait for ten seconds ignoring whatever you do. Next chance tohit and so on. Delay() is blocking processing => not a good thing todo in communication scenarios,

| improve this answer | |
  • Sure makes sense thanks – Jackie Apr 11 at 20:18
1

Get rid of if(Serial.available() > 0). You only need that if statement if you're expecting information from Serial, but it doesn't seem to be the case for your project.

Also, only use delay() if you have a really good reason to.

| improve this answer | |
  • So that is the next step. So that is triggered when you write? – Jackie Apr 11 at 20:18
  • Also that was probably why it worked the first time, I tried to simplify the code for SO and assumed removing the write would not be an issue. I was wrong – Jackie Apr 11 at 20:20
  • 1
    if(Serial.available() > 0) is triggered when something is received from the PC. If you have a problem with your code - post the entire code. You don't know where the bug is - it could be in the code that was left out – P_B Apr 11 at 21:15

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.