Go Down

Topic: Odd Behavior (Read 445 times) previous topic - next topic

itsthemedication

Okay, this is the code:

Code: [Select]

/*
 Send numbers to serial out
*/

void setup()
{
 Serial.begin(9600);
 // wait for any serial activity to start
 while (!Serial.available())
 {
 }
 // then send a comma separated file of x,y data
 for (byte i=0;i<256;i++)
 {
    Serial.print(i,DEC);
    Serial.print(',');
    Serial.println(random(0,255),DEC);
 }
}

void loop()
{

}


I enter a character, it starts as expected but never stops.  It runs over and over like it was in the loop section instead of the setup section.

programmer

#1
Aug 02, 2010, 02:20 am Last Edit: Aug 02, 2010, 11:10 pm by programmer Reason: 1
The problem comes because the byte data type can only hold a number from 0 to 255. When it reaches 255, it rolls over to 0, and so never becomes 256 and as a result the loop will never stop.

itsthemedication

how stupid of me - thanks!

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy