Reading an integer.

Hello.

I’m new to anything that is arduino related. I got my board a few days ago. Started playing around with it and i’m stuck at one point. Would like to find an explanation why is it not working as intended.

So basicly, what i was trying to do, user sends an interval in miliseconds to the board and board blinks the diod using that interval. For example, if user sends 2000, the diod will blink with 2 secs interval.

Everything seems to work just fine, except for… after user sends the first interval, it works great in the first time, but then, out of no idea where the interval changes to 0 and just goes mad…

Heres my code and an example:

long int timer = 1000;//Laiks milisekundēs starp diodes izslēgšanu un ieslēgšanu.  

void setup()
{
  Serial.begin(4800); 
  pinMode(13, OUTPUT);
}

void loop()
{
  //Katrā iterācijā printē iestatīto laika intervālu. 
  Serial.print("Interval: ");
  Serial.println(timer); 

  //Ja lietotājs padod vērtību kontrolierim un tā nav vienāda ar 0, kontrolierī tiek nomainīts laika intervāls. 
  if(Serial.available() > 0)
  { 
      timer = Serial.parseInt();
      
      //Izdrukā jauniestatīto laika intervālu. 
      Serial.print("Setting Interval: ");
      Serial.println(timer);      
      delay(100);
  }

  //Ieslēdz diodi. 
    digitalWrite(13, HIGH); 
    delay(100);    

   //Izslēdz diodi. 
    digitalWrite(13, LOW);   
    delay(timer);    
}
Interval: 1000
Interval: 1000
Interval: 1000
Interval: 1000
Setting Interval: 5000
Interval: 5000
Setting Interval: 0 <-- It records as if user sent something to the board, altho nothing was sent to it
Interval: 0
Interval: 0
Interval: 0
Interval: 0
Interval: 0
Interval: 0
Interval: 0
Interval: 0
Interval: 0

I can’t figure it out, why it swaps it to 0 altho user does not send anything to the board.

Set the serial monitor to "no line ending".

(You have it set to "newline", "carriage return" or both.)

Thank you, worked. :slight_smile:

xRazor:
Thank you, worked. :slight_smile:

I wish everyone was so easy to please. :smiley:

Maybe have a look at Serial Input Basics

...R