In: Computer Science
Using the first code of this lab (Figure 1), write a code that displays the status of a push button on the LCD, that is, when you press it you should see “Pushed” on the LCD and when you release it, you should see “Released”
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis() / 1000);
}
This is supposed to run on an Arduino. Thanks for your help!
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
Note: In my circuit, I have attached push button to digital pin 7 on arduino. check the diagrams attached. bottom left terminal of button is connected to pin 7 as well as to a 10K resistor, which is connected to the ground (GND), bottom right terminal is connected to 5V pin.
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//pin number where button is connected.
//change it if your button is connected to a different pin
const int buttonPin=7;
//state of the button
int btnState=0;
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
//reading button state
btnState=digitalRead(buttonPin);
//if state is HIGH, printing Pushed on LCD, else printing
//Released
if(btnState==HIGH){
lcd.print("Pushed ");
}else{
lcd.print("Released");
}
delay(50); //giving a slight delay
}
/*Circuit Diagram & OUTPUT*/