3

I have this really simple sketch where I blink a led light. If I send another value threw the serial monitor then first there is always a brief second where the light doesn't blink anymore. Why is that, and how can I get rid of that?

int the_delay = 300;
void setup() {
 Serial.begin(115200);
 pinMode(12, OUTPUT);
}
void loop() {
 digitalWrite(12, HIGH); 
 delay(the_delay); 
 digitalWrite(12, LOW); 
 delay(the_delay); 
}
void serialEvent() {
 if (Serial.available()) { 
 the_delay = Serial.parseInt();
 Serial.println(the_delay);
 }
}

In case it helps, eventually I would like to send values with 60fps.

asked Apr 5, 2016 at 20:21

4 Answers 4

3

It's because you're using Serial.parseInt(). It uses a timeout to find the end of the data on the serial port, and while it's waiting for that timeout it can do nothing else.

You would be better off manually reading the serial data a character at a time to build up an input string (C-string, not Arduino String) with a proper line terminator (line-feed, \n) and then convert that into an integer with atoi().

answered Apr 5, 2016 at 20:33
2

Try typing a non-digit character after the delay value; even Enter should work if your terminal is configured to send any kind of end-of-line. Serial.parseInt() waits the full timeout (default: 1000 ms) after each digit, for the next one. A non-digit will end the input value immediately.

answered Apr 5, 2016 at 21:58
1

See How to process incoming serial data without blocking. An interim solution is to get the Serial Monitor to send a newline automatically when you press Send.

Here is example code that processes input without blocking:

// Example of processing incoming serial data without blocking.
// 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
answered Apr 6, 2016 at 6:11
1

Edit as follows: Serial.begin(115200); Serial.setTimeout(10); //Maximum time in ms that it should wait for data

The default delay is 1000ms. The code above sets it to 10ms. Play around with the Timeout value and check for faults. https://www.arduino.cc/reference/en/language/functions/communication/serial/settimeout/

answered Oct 15, 2021 at 9:01
1
  • This is good so far as it goes. But, it seems mostly a repeat of an existing answer. Commented Oct 15, 2021 at 11:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.