Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!