class GPIO{
public:
virtual void configure()=0;
virtual void toggle()=0;
};
class LED : public GPIO{
public:
LED(int pin) : pin(pin){}
void configure() override{
pinMode(pin, OUTPUT);
}
void toggle() override{
digitalWrite(pin, HIGH);
delay(500);
digitalWrite(pin,LOW);
}
private :
int pin;
//bool state;
};
class Button : public GPIO{
public:
Button(int buttonPin,LED& led):pin(buttonPin),associatedLED(led){}
void configure() override {
pinMode(pin, INPUT);
}
void toggle() override{
if(digitalRead(pin) == HIGH){
if(!buttonPressed){
associatedLED.toggle();
buttonPressed = true;
}
}else{
buttonPressed = false;
}
}
private:
int pin;
LED& associatedLED;
bool buttonPressed;
};
LED led1(13);
Button button1(2, led1);
void setup() {
led1.configure();
button1.configure();
}
void loop() {
// put your main code here, to run repeatedly:
button1.toggle();
//delay(1000);
}