Samuel Klit Samuel Klit - 3 months ago 24
C++ Question

Arduino weird 12696ms delay

I'm making a traffic light with an Arduino Uno.

The problem is that after it completed a cycle it delays 12696ms. I have no idea where that number comes from.

Here is the code

/* Traffic light 1 pins:
* Red = 10
* Yellow = 9
* Green = 8
*
* Traffic light 2 pins:
* Red = 11
* Yellow = 12
* Green = 13
*/
int cycle[] = {10,13,0,5000,5000,0, 9,12,10,3000,3000,3000, 8,11,0,5000,5000,0, 12,9,11,3000,3000,3000};

void setup() {
for(int i = 8; i <=13; i++){
pinMode(i, OUTPUT);
}
}

void loop() {
for(int i = 0; i <= 24; i+=6){
digitalWrite(cycle[i], HIGH);
digitalWrite(cycle[i+1], HIGH);
digitalWrite(cycle[i+2], HIGH);

int delayTime = 0;
for(int i1 = 0; i1 <= 3; i1++){
if(cycle[i+3+i1] > delayTime){
delayTime = cycle[i+3+i1];
}
}
delay(delayTime);

digitalWrite(cycle[i], LOW);
digitalWrite(cycle[i+1], LOW);
digitalWrite(cycle[i+2], LOW);
}
}


I might have gone a little too creative with my solution.

Answer

Take a look at your for loop. You have 24 values, numbered 0 to 23. So your loop should be

for(int i = 0; i < 24; i+=6){

not

for(int i = 0; i <= 24; i+=6){

You're waiting on a garbage value - whatever is after the array in memory.

Comments