Answer the question
In order to leave comments, you need to log in
The LED on pin 6 does not turn off. What is wrong in my sketch?
Each time a button is pressed, the LED on the corresponding pin lights up.
As soon as the counter led_counter reaches the value 9, its value is set to = 4 and each time the button is pressed
, the enabled LEDs turn off in turn, but this does not happen on pin 6.
LED state inversion is implemented by digitalWrite(led_counter, !digitalRead(led_counter)); , but for some reason the value of digitalRead(led_counter) on pin 6 when the LED is on is = 0 and when inverted it remains on.
Question for the connoisseurs. What's wrong?
boolean but1 = 0;
boolean led_flag = 0;
boolean flag = 0;
unsigned long last_press;
byte led_counter = 4;
void setup() {
pinMode(A5,INPUT_PULLUP);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
digitalWrite(4,0);
digitalWrite(5,0);
digitalWrite(6,0);
digitalWrite(7,0);
digitalWrite(8,0);
digitalWrite(9,0);
Serial.begin(9600);
}
void loop() {
boolean but1 = !digitalRead(A5);
if (but1==1 && flag==0 && millis()-last_press > 50){
flag = !flag;
Serial.println("Button pressed");
last_press = millis();
}
if (but1==0 && flag==1){
flag = !flag;
//Serial.println("Button released"+ String(led_counter));
led_flag = !led_flag;
digitalWrite(led_counter, !digitalRead(led_counter));
led_counter++;
if(led_counter>9) led_counter = 4;
}
}
Answer the question
In order to leave comments, you need to log in
digitalWrite(led_counter, !digitalRead(led_counter));
This design is somewhat controversial.
I would do this:
led = !led ;
digitalWrite ( led_counter, led );
And on each pass the state will change to the opposite.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question