#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 RED_CONTROL() {
digitalWrite(RED_LED, digitalRead(RED_BUTTON)); // Update RED LED state based on button
}
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
} ButtonState;
public:
Button(uint8_t OUTpin,uint8_t INpin)
{
this.OUTpin = OUTpin;
this.INpin = INpin;
}
void ChangeState(State newState)
{
this.ButtonState = newState;
}
State getStatus()
{
return this.ButtonState;
}
};
void setup() {
// Configure button pins as input with internal pull-up
pinMode(RED_BUTTON, INPUT_PULLUP);
pinMode(GREEN_BUTTON, INPUT_PULLUP);
pinMode(BLUE_BUTTON, INPUT_PULLUP);
pinMode(YELLOW_BUTTON, INPUT_PULLUP);
// Configure LED pins as output
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(BLUE_LED, OUTPUT);
pinMode(YELLOW_LED, 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
}