Pause Code When Button Is Pushed Then Resume When Pushed Again
Arduino is an open up source and become famous in very short time, due to its powerful features and simplicity. Arduino comes as boon for inventor, creative person etc. One can make their own system without having much noesis about core electronics and assembly programming.
In this commodity yous will learn how to pause the lawmaking and resume using a push push switch. Quondam, we desire our code to run only when user do some activeness similar to press switch of go some value of sensor. Few years back while making game loonshit for manual battel bot, I had used two switches for two team whether they are ready to start lucifer or not. When one team press the switch indicating they are gear up, the program volition wait for some other team to press the switch. When both the team go ready the timer for 3 minutes will commencement with beep sound. In the tutorial "" y'all will learn how to pause the program and resume just when i printing the button without using delay function. We cannot employ filibuster function where program have to resume afterward user interaction.
Figure 2: Author Prototype of Pause and Resume Arduino Program using Switch
Annotation: For demo purpose we are using 16×ii alphanumeric LCD.
Components Required Pause and Resume Arduino Program using Switch
Arduino nano x one
Push Push button Switch x 2
10k resistor x ii
sixteen×two LCD x 1
10K Variable resistor x ane
330E resistor ten 1
Circuit Description of Pause and Resume Arduino Program using Switch
The circuit of "" is shown in effigy 1, it is built effectually arduino Nano, ii button switches, an LCD and few other electronic passive components like resistor. LCD is used here only for demo purpose. If you have any confusing about interfacing LCD with arduino then please do watch interfacing video posted in youtube.
Ii switches with pulldown resistor is connected to arduino nano analog pin A0 and A1 every bit shown in circuit diagram. Two pulldown resistors are also used hither in order to overcome the transient response of switch. These switches are configured in high level trigger considering one terminate of both switches is connected to +5V power supply.
Software Code:
Software lawmaking is written in arduino programming and compiled using arduino IDE. Let's see the code. For explanation nosotros are going to separate the code in diverse small section.
Defining the pin of button and LCD used in the project. If you connect the LCD as show in circuit, you practice not have to change it but if you modify your connection yous also have to alter the pivot definition.
int Button1 = A0 ; int Button2 = A1 ; const int rs = 7 , en = 6 , d4 = five , d5 = 4 , d6 = 3 , d7 = 2 ; LiquidCrystal lcd ( rs , en , d4 , d5 , d6 , d7 ) ; |
In setup role we will ascertain the property of button switch i.east. whether we use it as input or output. Similarly, nosotros also begin the LCD.
void setup ( ) { lcd . begin ( 16 , 2 ) ; pinMode ( Button1 , INPUT ) ; pinMode ( Button2 , INPUT ) ; } |
Loop function run repeatedly, when loop part run, information technology calls button function and returns to loop office merely when push is pressed.
void loop ( ) { CheckButton1 ( ) ; CheckButton2 ( ) ; } |
CheckButton() function check whether the push i is pressed or non. When button is pressed, the value become high for CheckButtonPress1 and render back to loop role.
1 ii 3 4 v 6 7 eight 9 ten 11 12 13 14 15 xvi 17 | void CheckButton1 ( ) { int CheckButtonPress1 = 0 ; while ( ane ) { CheckButtonPress1 = digitalRead ( Button1 ) ; lcd . setCursor ( 0 , 0 ) ; lcd . print ( "Press 1st Button" ) ; lcd . setCursor ( 0 , 1 ) ; lcd . impress ( " to proceed" ) ; if ( CheckButtonPress1 == High ) { lcd . articulate ( ) ; lcd . setCursor ( 0 , 0 ) ; lcd . print ( "1st But. Pressed" ) ; delay ( 2000 ) ; return ; } } } |
Consummate Software Code
1 ii 3 4 v vi 7 8 9 10 11 12 13 14 15 16 17 xviii 19 xx 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #include <LiquidCrystal.h> int Button1 = A0 ; int Button2 = A1 ; const int rs = vii , en = half dozen , d4 = 5 , d5 = 4 , d6 = 3 , d7 = ii ; LiquidCrystal lcd ( rs , en , d4 , d5 , d6 , d7 ) ; void setup ( ) { lcd . begin ( 16 , 2 ) ; pinMode ( Button1 , INPUT ) ; pinMode ( Button2 , INPUT ) ; } void loop ( ) { CheckButton1 ( ) ; CheckButton2 ( ) ; lcd . articulate ( ) ; lcd . setCursor ( 0 , 0 ) ; lcd . print ( "***Plan is***" ) ; lcd . setCursor ( 0 , 1 ) ; lcd . print ( "***Resumed***" ) ; delay ( 5000 ) ; } void CheckButton1 ( ) { int CheckButtonPress1 = 0 ; while ( 1 ) { CheckButtonPress1 = digitalRead ( Button1 ) ; //lcd.articulate(); lcd . setCursor ( 0 , 0 ) ; lcd . print ( "Press 1st Push" ) ; lcd . setCursor ( 0 , i ) ; lcd . print ( " to continue" ) ; if ( CheckButtonPress1 == High ) { lcd . articulate ( ) ; lcd . setCursor ( 0 , 0 ) ; lcd . impress ( "1st Only. Pressed" ) ; delay ( 2000 ) ; return ; } } } void CheckButton2 ( ) { int CheckButtonPress2 = 0 ; while ( 1 ) { CheckButtonPress2 = digitalRead ( Button2 ) ; //lcd.clear(); lcd . setCursor ( 0 , 0 ) ; lcd . impress ( "Press 2nd Button" ) ; lcd . setCursor ( 0 , one ) ; lcd . print ( " to continue" ) ; if ( CheckButtonPress2 == Loftier ) { lcd . clear ( ) ; lcd . setCursor ( 0 , 0 ) ; lcd . impress ( "2nd Merely. Pressed" ) ; delay ( 2000 ) ; return ; } } } |
Source: https://bestengineeringprojects.com/pause-and-resume-arduino-program-using-switch/