0

I am trying to blink two LED's, starting at slow rate then increasing to fast s follows: 1 on, 1&2 on, 2 on, 1&2 on, 1 on, etc. The blinks go through the loop twice then speed up and repeat until both are on continuously. Then I want LED's 3 & 4 to come on. I got the 1 & 2 LED's to blink as I want but anything I put following my for / if loop goes into the loop. This works for blinking the 1 & 2 LED's:

int led1 = 9;           
int led2 = 10;           
int led3 = 11;
int led4 = 12;
int delay1 = 700;   
int delayAmount1 = delay1 / 10;    

void setup() {

pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
}

void loop() {{
delay1 = delay1 - delayAmount1;

for (int i=0 ; i <2 ; i++) {

if (delay1 > 10)

digitalWrite(led1, HIGH); //yellow on (yellow)

delay(delay1);

digitalWrite(led2, HIGH); //red and yellow on (orange)

delay(delay1);

digitalWrite(led1, LOW); //yellow off (red)

delay(delay1);

digitalWrite(led1, HIGH); //yellow on (orange)

delay(delay1);

digitalWrite(led2, LOW); //red off (yellow)

digitalWrite (led1, LOW); //yellow off (all off)

}
}

// digitalWrite(led3, HIGH); //blue on

//delay(1000);

//digitalWrite(led4, HIGH); //green on

}

If I remove the // from

// digitalWrite(led3, HIGH); //blue on
//delay(1000);
//digitalWrite(led4, HIGH); //green on

they become part of the loop. Can someone tell me what I am doing wrong? Kind of new to this.

VE7JRO
  • 2,554
  • 18
  • 25
  • 29
JBrown
  • 3
  • 2

2 Answers2

0

The if(delay1>10) does not have the conditional code in {}, so it should look like

if(delay1>10) { 
//conditional code, what happens if statement == true
} 

And if I understood your project and code correctly, when delay1 <= 10 then you want the led3 and led4 to light up. So you want to add an optional else{} block. This optional block will be skipped, if the if statement is executed. If the if statement is not executed, because for example your delay1 is smaller or equal to 10, then the else block will be executed.

else{
//led3 and led4 light up
}
Zunzulla alagaty
  • 164
  • 1
  • 1
  • 13
0

You have a number of code formatting problems, some aesthetic, and some structural.

You need to indent your code, and you need to use braces. If you don't format your code well you will have a very hard time figuring out what you are doing.

Here is an attempt to clean up your code:

void loop() {

delay1 = delay1 - delayAmount1;

for (int i=0 ; i <2 ; i++) {
    if (delay1 > 10) { 
        digitalWrite(led1, HIGH); //yellow on (yellow)
        delay(delay1);
        digitalWrite(led2, HIGH); //red and yellow on (orange)
        delay(delay1);
        digitalWrite(led1, LOW); //yellow off (red)
        delay(delay1);
        digitalWrite(led1, HIGH); //yellow on (orange)
        delay(delay1);
        digitalWrite(led2, LOW); //red off (yellow)
        digitalWrite (led1, LOW); //yellow off (all off)
    }
}
// digitalWrite(led3, HIGH); //blue on
//delay(1000);
//digitalWrite(led4, HIGH); //green on

}

(I'm not sure what's supposed to be inside the if statement or not since you didn't have an opening brace.)

You also had 2 opening braces on your loop statement, which is very odd.

Note that using delay() to try to control more than one thing at a time is a bad idea. The delay() function causes your program to freeze everything until the delay is over. If you try to blink different LEDs at different rates, you can't. If you try to detect button presses while you are also blinking LEDs, you miss the button presses while your code is in a delay. There are lots of threads on the various Arduino boards on how to avoid using delay. Search on "Arduino blink no delay" to find the basics.

Duncan C
  • 5,682
  • 3
  • 17
  • 29