I am using an esp32 in Arduino. What I want to do is: If I press the button once, it should Serial.print "I was pressed once" If I press the button twice, it should Serial.print "I was pressed twice"
I am using the attachInterrupt() function, but I don't know how to tell the code how to read it when I press the button twice. What my code also does is turning on a LED when it senses I pressed the button.
Here is what I have achieved so far :
int boton = 0;
int led = 5;
int valorBoton; //value of the button, if it off(1) or on (0)
unsigned int count = 0 ; //counter
void setup() {
Serial.begin(115200); //velocity
pinMode(led, OUTPUT); //OUTPUT LED
pinMode(boton, INPUT); //INFUPT BUTTON
digitalWrite(led, LOW); //THE LED IS LOW INITIALLY
attachInterrupt(digitalPinToInterrupt(0),button1,RISING);
}
void loop() {
count++;
Serial.println(count); //printing the counter
delay(1000);
}
void button1(){ //the function button1 is a parameter of attachInterrupt
digitalWrite(led, HIGH); //when it is pressed, led is on
Serial.println("I was pressed");
count = 0; // if I was pressed, then the count starts from cero all over again
}
I expect to print Serial.println("I was pressed twice") when I press the button