#define RED_BUTTON 15 // Pin connected to the RED button (interrupt pin)
// #define GREEN_BUTTON 2 // Pin connected to the GREEN button (interrupt pin)
// #define BLUE_BUTTON 0 // Pin connected to the BLUE button (interrupt pin)
// #define YELLOW_BUTTON 4 // Pin connected to the YELLOW button (interrupt pin)
#define RED_LED 16 // Pin connected to the RED LED
// #define GREEN_LED 17 // Pin connected to the GREEN LED
// #define BLUE_LED 5 // Pin connected to the BLUE LED
// #define YELLOW_LED 18 // Pin connected to the YELLOW LED
// ISR functions
// void IRAM_ATTR GREEN_CONTROL() {
// digitalWrite(GREEN_LED, digitalRead(GREEN_BUTTON)); // Update GREEN LED state based on button
// }
// void IRAM_ATTR BLUE_CONTROL() {
// digitalWrite(BLUE_LED, digitalRead(BLUE_BUTTON)); // Update BLUE LED state based on button
// }
// void IRAM_ATTR YELLOW_CONTROL() {
// digitalWrite(YELLOW_LED, digitalRead(YELLOW_BUTTON)); // Update YELLOW LED state based on button
// }
class Button{
uint8_t INpin ;
uint8_t OUTpin ;
enum State{
OFF = 0,
ON = 1
} position;
public:
Button(uint8_t OUTpin,uint8_t INpin)
{
this->OUTpin = OUTpin;
this->INpin = INpin;
pinMode(this->OUTpin , OUTPUT);
pinMode(this->INpin, INPUT_PULLUP);
}
void ChangeState()
{
digitalWrite(this->OUTpin,digitalRead(INpin));
switch(digitalRead(INpin)){
case 0 : position = OFF;break;
case 1 : position = ON;break;
}
}
State getStatus()
{
return this->position;
}
};
Button Redbutton(RED_LED,RED_BUTTON);
void IRAM_ATTR RED_CONTROL() {
// digitalWrite(RED_LED, digitalRead(RED_BUTTON)); // Update RED LED state based on button
Redbutton.ChangeState();
}
void setup() {
Serial.begin(115200);
// Configure button pins as input with internal pull-up
// Configure LED pins as output
// Attach interrupts to button pins
attachInterrupt(digitalPinToInterrupt(RED_BUTTON), RED_CONTROL, CHANGE);
// attachInterrupt(digitalPinToInterrupt(GREEN_BUTTON), GREEN_CONTROL, CHANGE);
// attachInterrupt(digitalPinToInterrupt(BLUE_BUTTON), BLUE_CONTROL, CHANGE);
// attachInterrupt(digitalPinToInterrupt(YELLOW_BUTTON), YELLOW_CONTROL, CHANGE);
}
void loop() {
// Main loop can execute other tasks; the LEDs are controlled by ISR
Serial.print("LED : ");
Serial.println(Redbutton.getStatus());
delay(200);
}