I am running a simple sketch to blink four LEDs on an ATMega8 based board. The code calls randomSeed to initiate the random function using the input of the unconnected analog0 pin. When I print the random variables to Serial, everything works fine. However, if I comment out the Serial.println code, the LEDs appear to each blink at their own constant rate.
That is, they all blink at different rates, but the rate for each LED never changes, even though random is called each loop. Of course, I cannot verify the exact values being output by random in this case, because when I print them to the Serial monitor, the code works fine. However, I am absolutely sure that when I watch the LEDs blink, they are not changing the blink interval. When I do print the to Serial, the LEDs definitely DO blink at random intervals. It does not matter if the serial monitor is running or not.
As a test, I tried printing the value of analogRead(0) instead of the random variables. I found that the LEDs do blink randomly when I do this. Just be be sure, I tried only printing "Hello world" to serial. Again, I found that the LEDs blinked at random intervals.
I'm running Arduino 1.0.1 on Windows 7. Here's my code:
const int ledPin0 = 13; // the number of the on board LED pin on the cheapDuino
const int ledPin1 = 9;
const int ledPin2 = 10;
const int ledPin3 = 11;
// Variables will change:
int ledState0 = LOW; // ledState used to set the LED
int ledState1 = LOW;
int ledState2 = LOW;
int ledState3 = LOW;
long previousMillis0 = 0; // will store last time LED was updated
long previousMillis1 = 0;
long previousMillis2 = 0;
long previousMillis3 = 0;
// the interval variables are long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval0 = 1000; // interval at which to blink (milliseconds)
long interval1 = 1000;
long interval2 = 1000;
long interval3 = 1000;
void setup() {
Serial.begin(19200);
// set the digital pin as output:
pinMode(ledPin0, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
randomSeed(analogRead(0));
}
void loop()
{
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
// randomSeed(analogRead(0));
// Serial.println(analogRead(0));
// Serial.println("Hello world");
interval0 = random(250,3000); // set random interval for LED
interval1 = random(250,3000);
interval2 = random(250,3000);
interval3 = random(250,3000);
// Serial.println(interval0); // Weird; the intervals only seem random each loop if any
// Serial.println(interval1); // output is printed to serial. No explanation.
Serial.println(interval2);
// Serial.println(interval3);
// Serial.println(" ");
unsigned long currentMillis = millis();
if(currentMillis - previousMillis0 > interval0) {
// save the last time you blinked the LED
previousMillis0 = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState0 == LOW)
ledState0 = HIGH;
else
ledState0 = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin0, ledState0);
}
if(currentMillis - previousMillis1 > interval1) {
// save the last time you blinked the LED
previousMillis1 = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState1 == LOW)
ledState1 = HIGH;
else
ledState1 = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin1, ledState1);
}
if(currentMillis - previousMillis2 > interval2) {
// save the last time you blinked the LED
previousMillis2 = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState2 == LOW)
ledState2 = HIGH;
else
ledState2 = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin2, ledState2);
}
if(currentMillis - previousMillis3 > interval3) {
// save the last time you blinked the LED
previousMillis3 = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState3 == LOW)
ledState3 = HIGH;
else
ledState3 = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin3, ledState3);
}
}