Hi!
I am doing a project were when I click button1, the LED get 10% brighter each time I click it. And when every time that I click button2, the LED should get 10% lower. My issue is when I click button1, the LED keeps on going up by 10 infinity, and doesn't stop at my stopping point of 255. For button2, it keeps on going down by 10 infinity, and doesn't go down my only 10. If anyone can help me that would be great!
Project Link (Code + Wiring): Push Button (LED Dimmer using Buzzer) - Wokwi Arduino and ESP32 Simulator
Code:
int BUTTON1 = 7; // Button input at pin 7.
int BUTTON2 = 6; // Button input at pin 6.
int RED = 3;
int BLUE = 10;
int BUTTONRead1; // to store input value of the first button (0, 1)
int BUTTONRead2; // to store input value of the second button (0, 1)
int j;
int h;
void setup(){
Serial.begin(9600); // initializing serail communication.
pinMode(RED, OUTPUT); // Defining LED pin as output.
pinMode(BLUE, OUTPUT); // Defining LED pin as output.
pinMode(BUTTON1, INPUT_PULLUP); // Defining Button pin as INPUT_PULLUP.
pinMode(BUTTON2, INPUT_PULLUP); // Defining Button pin as INPUT_PULLUP.
}
void loop(){
BUTTONRead1 = digitalRead(BUTTON1); // Reading Button1 Input.
BUTTONRead2 = digitalRead(BUTTON2); // Reading Button2 Input.
if(BUTTONRead1 == 0 && j<=255) {
if(j<=255){
for (j<255; j=j+10;){
Serial.println(j);
analogWrite(RED, j);
delay(10);
}
}
}
if(BUTTONRead2 == 0) {
if(j<=255){
for (h>255; h=h-10;){
Serial.println(h);
analogWrite(RED, h);
delay(10);
}
}
}
delay(300); // delay to make output readable on serial monitor.
}