Good morning all,
I am hoping for a bit of help. I am completely new to coding so please go easy on me. I have spent some time going through the simple projects such as turning an LED on and basic button functions as well as following tutorials for creating "crystal balls" and the likes, which all went well. I have now started on my own project and have come across a couple of problems.
The platform is an Arduino Uno, with a 16 x 2 LCD screen and 2 push buttons.
What I am trying to achieve: -
When the Arduino is powered up a message should be displayed, after a set period of time this message changes to another. To proceed from the second message a button must be pressed before the loop begins.
The loop then consists of some text and a counter. When one button is pressed the counter increases and when another button is pressed the counter goes back to 0.
The two items which aren't working are the proceed to loop on button command and the counter returning to zero. I have spent a lot of time trying to fix the problem and researching but am not getting anywhere so this post is a last resort - I don't usually like bothering others for what is a simple problem for most!
Hopefully someone will be able to help!
Thank you.
#include <LiquidCrystal.h>
LiquidCrystal lcd(2,3,4,5,6,7);
const int buttonDownAdd = 8; // button position on Arduino
int countDownAdd = 10; // button counter
int buttonStateDownAdd = 0; // current state of button
int lastButtonStateDownAdd = 0; // previous state of button
const int buttonEnterClear = 9; // set up clear button
int buttonStateEnterClear = 0; // current state of button
int lastButtonStateEnterClear = 0; // previous state of button
void setup()
{
pinMode (buttonDownAdd, INPUT); // set up button as input
pinMode (buttonEnterClear, INPUT); // set up button as input
lcd.begin(16, 2); // set up LCD
lcd.setCursor(3,0); // print message to LCD
lcd.print("Welcome to");
lcd.setCursor(4,1);
lcd.print("MiScore!");
delay(3000); // delay 3 seconds before changing the message
lcd.clear(); // print new message to screen
lcd.setCursor(1,0);
lcd.print("Press Green to");
lcd.setCursor(5,1);
lcd.print("Begin!");
}
void loop()
{
buttonStateDownAdd = digitalRead(buttonDownAdd); // read buttonDownAdd to see it's current position
if (buttonStateDownAdd != lastButtonStateDownAdd) { // check to see if the button has changed from its previous state
if (buttonStateDownAdd == HIGH){ // if the button has changed then move to the query
{buttonStateDownAdd = digitalRead(buttonDownAdd); // read buttonDownAdd again to see it's current position
if (buttonStateDownAdd != lastButtonStateDownAdd) { // check to see if the button has changed from its previous state
if (buttonStateDownAdd == HIGH){
countDownAdd++; // if the button has been pressed add 1 to countDownAdd
lcd.clear(); // write the current countDownAdd to the LCD
lcd.setCursor(2,0);
lcd.print("You have had");
lcd.setCursor(4,1);
lcd.print(countDownAdd);
lcd.setCursor(8,1);
lcd.print("dabs");
}
else {}
delay (25);
}
else {}
delay (25);
lastButtonStateDownAdd = buttonStateDownAdd;} // save the current state of the button for the next loop
{buttonStateEnterClear = digitalRead(buttonEnterClear); // read buttonEnterClear to see it's current position
if (buttonStateEnterClear != lastButtonStateEnterClear) { // check to see if the button has changed from its previous state
if (buttonStateEnterClear == HIGH){ // if the button has been pressed reset CountDownAdd and update the LCD screen
countDownAdd=0;
lcd.clear();
lcd.setCursor(2,0);
lcd.print("You have had");
lcd.setCursor(4,1);
lcd.print(countDownAdd);
lcd.setCursor(8,1);
lcd.print("dabs");
}
else {}
delay (25);
}
else {}
delay (25);
lastButtonStateEnterClear = buttonStateEnterClear;} // save the current state of the button for the next loop
}
else {}
delay (25);
lastButtonStateDownAdd = buttonStateDownAdd; // save the current state of the button for the next loop
}
else {}
delay (25);
lastButtonStateDownAdd = buttonStateDownAdd; // save the current state of the button for the next loop
}