Detecting the long press state of the push button
To add the functionality of the button to function when it is pressed for a long time, we have made a simple circuit whose circuit schematic is given below followed by the components that we have used to make the circuit.
- Arduino Uno
- Connecting wires
- 1 push button
- Breadboard
Hardware assembly for detecting the long press state of push button
To see how the circuit will look like on the actual hardware we have made the hardware assembly which is given below in the image.
As you can see in the image above, we have used pin 8 of Arduino to connect it with the push button using the green wire and using the black wire to connect the push button with the ground pin of Arduino.
Arduino code for detecting the long press state of push button
To detect the long press of the push button we have programmed the microcontroller using the millis() function. We have detected the long press state by getting the time for which the state of the push button remained HIGH and LOW. After that we calculated the duration for which the button was pressed and then compared it with some constant time value based on that we can say that the button was pressed for a long time.
In this case we have given the constant time value of one second that is 1000 milliseconds as the short press for the button. If the press duration is more than time for the short press time, then it will be considered as the long press and will be displayed on the serial monitor. Below we have given the Arduino sketch used to program the controller for detecting the long press state of the push button.
int SHORT_TIME = 1000; /* Time that will be considered as the short press time */
long ON_Duration;/* Variable that will store the value of time for which the button is pressed */
int previousState = LOW; /* Setting the initial state of push button HIGH as we are using the INPUT_PULLUP mode */
int presentState; /* Variable that will store the present state if the button*/
unsigned long press_Time = 0; /* Time at which the button was pressed */
unsigned long release_Time = 0;/*Time at which the button is released */
void setup() {
Serial.begin(9600);/* Giving the baud rate for serial communication*/
pinMode(BUTTON, INPUT_PULLUP);/* Giving the working mode to the push button that is INPUT_PULLUP (inverts the inputs of button )*/
}
void loop() {
// Read the state of the switch/button:
presentState = digitalRead(BUTTON);/* Getting the present state of the push button */
if(previousState == HIGH && presentState == LOW) /* If button is pressed */
press_Time = millis();/* Save the time in milliseconds using the Millis function */
else if(previousState == LOW && presentState == HIGH) { /* If button is released*/
release_Time = millis();/* save the time at which the button was released */
long ON_TIME = release_Time - press_Time;/* calculating the time for which the button remained in the LOW state*/
if( ON_TIME > SHORT_TIME ) /* comparing the value of time for which the button is pressed to the value for short press time*/
Serial.println("Button is pressed for a long time ");/* printing the data on the serial monitor */
}
previousState = presentState;/* saving the present value in the previous value */
}
Hardware demonstration
We have made the circuit on the hardware right according to the hardware assembly that is discussed earlier, and the image below shows the hardware implementation of the push button circuit to detect the long press state of the button.
When we press the button for more than one second then the Arduino code shows on the Serial monitor that button is pressed for the long time as shown in the image below:
Conclusion
The significance of using the long press state of the push button is that you can use a single button to perform more than one task. So to explain how to detect the long press state of the push button we have designed a circuit and implemented it on the actual hardware and also we have provided the Arduino sketch that will provide a great understating of the topic.