Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Below is the Arduino code which is working as assigned. Button 1 is attached to Pin 4 . Button 1 ( Pin 4 ) when

Below is the Arduino code which is working as assigned. Button 1 is attached to Pin 4. Button 1(Pin 4) when pressed is clearing the LCD and resetting the counter to zero. Button 2 is attached to Pin 5. Button 2(Pin 5) when held down is incrementing the LCD counter continuously by 1(it is inside a while loop). Within the code, both pin 4 & 5 digital reads are set to ==HIGH, instead of LOW. This was necessary to achieve the desired output given the wiring and circuit board. However, this appears to be backwards, as the pressing the buttons should pull the input resistor to ground and creating a LOW state. Why would the code work as assigned if the button digital reads are set to ==HIGH instead of LOW? Thank you, code follows.....
#include
// Define LCD pins
LiquidCrystal lcd(13,12,11,10,9,8);
// Global counter variable
int counter =0;
void setup(){
// Set up LCD
lcd.begin(16,2);
lcd.clear();
counter =0;
// Set up button pins with pull-up resistors
pinMode(4, INPUT_PULLUP); // Button 1
pinMode(5, INPUT_PULLUP); // Button 2
}
void loop(){
// Check if button one is pushed
while (digitalRead(4)== HIGH){
// Clear LCD and reset counter
lcd.clear();
delay(500);
lcd.setCursor(0,1);
lcd.print("Counter: 0");
counter =0;
}
// Check if button two is pushed
while (digitalRead(5)== HIGH){
// Increment counter
counter++;
// Write counter value to LCD
lcd.setCursor(0,1);
lcd.print("Counter: "+ String(counter));
delay(500); // Add a delay to avoid rapid counting due to button bouncing
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Xxxviii Special Issue On Database And Expert Systems Applications Lncs 11250

Authors: Abdelkader Hameurlain ,Roland Wagner ,Sven Hartmann ,Hui Ma

1st Edition

3662583836, 978-3662583838

More Books

Students also viewed these Databases questions

Question

What is structured English?

Answered: 1 week ago