I was experimenting with for loops when I noticed that a simple increment loop executes faster than a decrement loop. I can't think of any reason why it should be like that. Is there something in my code that is causing the different in execution speed?
The code is as follows:
// Global Variables
const int spk = 7;
const int led = 6;
int j = 0; // counter variable
void setup() {
pinMode(spk, OUTPUT);
pinMode(led, OUTPUT);
Serial.begin(9600);
} // end of setup
void loop() {
tone(spk, 300, 500); // first note with duration
// increment loop
for (j=0; j <= 500; j = j + 1) {
digitalWrite(led, HIGH);
Serial.println(j);
} // end 1st note loop
tone(spk, 100, 500); // second note with duration
// decrement loop
for (j = 500; j >= 0; j=j-1) {
digitalWrite(led, LOW);
Serial.print("the value is=");
Serial.print("\t");
Serial.println(j);
} // end 2nd Note loop
} // end setup