#define BUTTON_PIN 34 // ESP32 pin GPIO25, which connected to button
#define LED 4
#define LED_PIN 2 // ESP32 pin GPIO18, which connected to led
#define BUTTON 25
int check=0;
// variables will change:
int led_state = LOW, led2_state=LOW; // the current state of LED
int button_state;
int last_button2_state;
int t1=3000, t2=1000;
unsigned long lastTimeButtonStateChanged = millis();
unsigned long debounceDuration = 50; //milis()
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT_PULLUP);
pinMode(BUTTON_PIN, INPUT_PULLUP);
last_button2_state = digitalRead(BUTTON_PIN);
}
void loop() {
//button màu đen điều chỉnh tần suất sáng tối của đèn
if(millis() - lastTimeButtonStateChanged >= debounceDuration){
int button2_state = digitalRead(BUTTON_PIN); // read new state
delay(0);
if(last_button2_state!=button2_state){
lastTimeButtonStateChanged=millis();
last_button2_state = button2_state;
if( button2_state==LOW){
if(led2_state==LOW){
check=1;
led2_state=HIGH;
}
else{
check=0;
led2_state=LOW;
}
}
digitalWrite(LED, led2_state);
if (check==1){
t1=1000;
t2=3000;
}
else {
t1=3000;
t2=1000;
}
}
}
//điều chỉnh đèn nhấp nháy
button_state = digitalRead(BUTTON);
if(button_state==LOW){
digitalWrite(LED_PIN,HIGH);
delay(t1);
digitalWrite(LED_PIN, LOW);
delay(t2);
}
else{
digitalWrite(LED_PIN, LOW);
delay(100);
}
}