I am new to arduino.
I have created a circuit , here is the image:
I want something that as I press the button the sequence of LEDs will change and as I press the button again , the sequence will change again.
I programmed code as following but it don't works correctly:
int led1 = 11;
int led2 = 10;
int output = 2;
int input = 4;
int mode = 0;
void setup() {
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(output,OUTPUT);
pinMode(input,INPUT_PULLUP);
digitalWrite(output,LOW);
}
void loop() {
while(mode == 0 && digitalRead(input) == 1){
digitalWrite(led1,HIGH);
digitalWrite(led2,HIGH);
delay(100);
digitalWrite(led1,LOW);
delay(100);
digitalWrite(led1,HIGH);
delay(100);
digitalWrite(led2,LOW);
delay(100);
digitalWrite(led2,HIGH);
}
if(digitalRead(output) == 0){
mode++;
if(mode >= 2){mode = 0;}
}
while(mode == 1 && digitalRead(input) == 1){
analogWrite(led1,255);
delay(400);
analogWrite(led1,150);
delay(400);
analogWrite(led2,255);
delay(400);
analogWrite(led2,150);
}
}
According to the above code , two leds led1 and led2 are connected with arduino. When the button is off the both LEDs turns on then led1 turns off then turn on again , next time led2 turns off and turns on again.As the button will be pressed , brightness of led1 will be less than the brightness of led2 and then will again be equal to the brightness of led2, then brightness of led2 will be less than the brightness of led1 and then will be equal to the brightness of led1. But as I press the button , nothing happens but the led1 and led2 keeps turning on and off . However sometimes when I press the button then it works.
What is the problem with my code and how to solve it?
delay()
from your code.