Hello!
I am having trouble with a project I am working on with an LCD display with an arduino.
This is my project thus far, and I am trying to get the LCD to update on button press. But, whenever I press the button nothing happens.
Here's my code:
#include <LiquidCrystal.h>
#define pass (void)0
using namespace std;
const int buttonPin = 10;
int buttonState = 0;
int lastButtonState = 0;
int buttonPushCount = 0;
bool bPress = false;
int value = 10;
int numPoints = 0;
String points;
String basePoints;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
lcd.begin(16, 2);
basePoints = "0000";
points = basePoints;
pinMode(buttonPin, INPUT_PULLUP);
lcd.setCursor(0,0);
lcd.print(" POINTS");
lcd.setCursor(2,1);
lcd.print(points);
}
void loop()
{
checkButton();
if(bPress)
{
bPress = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SCORE!");
lcd.setCursor(2, 1);
lcd.print("+100");
delay(1000);
}
else
{
pass;
}
}
void checkButton()
{
buttonState = digitalRead(buttonPin);
if(buttonState != lastButtonState)
{
if(buttonState == LOW)
{
bPress = true;
}
}
buttonState = lastButtonState;
}
I am unsure as to what the problem is.
Thank you for reading and your help!
You have this the wrong way round. Try:
lastButtonState = buttonState;
What does this bit of code do:
ichthys522:
else
{
pass;
}
1 Like
The pinMode() of your button pin is set to INPUT_PULLUP which means that you need to arrange the circuit to take the button pin LOW when the button is pressed
Is that the way that your circuit is wired ? What is the resistor connected to the button doing there ?
1 Like
Please check your button's connection.
1 Like
The resistor is to connect the button to ground. Is this wrong?
Okay, thank you!
That pass keyword is (void)0 (line 3) , but I could have implemented it wrong
It's supposed to just skip right through the else statement, "pass" it
It is not needed when using INPUT_PULLUP. Just take the pin LOW when the button is pressed and detect the change of state and the current LOW state to determine that the button has become pressed
1 Like
Okay! It is working! Here's my code:
#include <LiquidCrystal.h>5
using namespace std;
const int buttonPin = 10;
int buttonState = 0;
int lastButtonState = 0;
int buttonPushCount = 0;
bool bPress = false;
int value = 10;
int numPoints = 0;
String points;
String basePoints;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
lcd.begin(16, 2);
basePoints = "0000";
points = basePoints;
pinMode(buttonPin, INPUT_PULLUP);
lcd.setCursor(0,0);
lcd.print(" ULTIMATE");
lcd.setCursor(2,1);
lcd.print("PINBALL");
delay(1000);
lcd.clear();
}
void loop()
{
lcd.setCursor(0, 0);
lcd.print("POINTS:");
lcd.setCursor(2, 1);
lcd.print(points);
checkButton();
if(bPress)
{
bPress = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SCORE!");
lcd.setCursor(2, 1);
lcd.print("+100");
delay(500);
lcd.clear();
}
}
void checkButton()
{
buttonState = digitalRead(buttonPin);
if(buttonState == LOW)
{
bPress = true;
}
if(buttonState != lastButtonState)
{
if(buttonState == LOW)
{
bPress = true;
}
}
lastButtonState = buttonState;
}
Here's the circuit:
I just need to make the point value update
Thank you all for your help, I really appreciate it!
God bless!
Here's the code with point update:
#include <LiquidCrystal.h>5
using namespace std;
const int buttonPin = 10;
int buttonState = 0;
int lastButtonState = 0;
int buttonPushCount = 0;
bool bPress = false;
int value = 10;
int numPoints = 0;
String points;
String basePoints;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
lcd.begin(16, 2);
int basePoints = 0000;
numPoints = basePoints;
pinMode(buttonPin, INPUT_PULLUP);
lcd.setCursor(0,0);
lcd.print(" ULTIMATE");
lcd.setCursor(2,1);
lcd.print("PINBALL");
delay(1000);
lcd.clear();
}
void loop()
{
lcd.setCursor(0, 0);
lcd.print("POINTS:");
lcd.setCursor(2, 1);
lcd.print(numPoints);
checkButton();
if(bPress)
{
bPress = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SCORE!");
lcd.setCursor(2, 1);
lcd.print("+100");
numPoints = numPoints + 100;
delay(500);
lcd.clear();
}
}
void checkButton()
{
buttonState = digitalRead(buttonPin);
if(buttonState == LOW)
{
bPress = true;
}
if(buttonState != lastButtonState)
{
if(buttonState == LOW)
{
bPress = true;
}
}
lastButtonState = buttonState;
}
system
Closed
October 4, 2023, 4:47pm
10
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.