Im trying to make a small quiz on the screen, but I can't figure out how to check the buttons again
there must be an easier way than what I am doing!!
If you could take a look I would be greatful.
#include <LiquidCrystal.h>
#include <Button.h>
Button button1 = Button(8, PULLDOWN);
Button button2 = Button(9, PULLDOWN);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int buttonState1 = 0;
int buttonState2 = 0;
int firstRun = 0;
void setup() {
lcd.begin(16, 2);
}
void loop() {
if(firstRun == 0) { // Where are we?
firstRun = 1;
lcd.setCursor(0,0);
lcd.print("Q1"); // Print Question
lcd.setCursor(0,1);
lcd.print("Yes or No above");
}
if(button1.uniquePress()) {
if(firstRun == 1) {
delay(200);
lcd.clear();
lcd.print("Q2");
firstRun = 2;
}
if(firstRun == 2) {
if(button1.isPressed()) {
lcd.clear();
lcd.print("A1");
delay(3000);
firstRun = 0;
} else {
if(button2.isPressed()) {
lcd.clear();
lcd.print("A2");
delay(3000);
firstRun = 0;
}
}
}
}
if(button2.uniquePress()) {
if(firstRun == 1) {
delay(200);
lcd.clear();
lcd.print("Q3");
firstRun = 3;
}
if(firstRun == 3) {
if(button1.isPressed()) {
lcd.clear();
lcd.print("A3");
delay(3000);
firstRun = 0;
}
if(button2.isPressed()) {
lcd.clear();
lcd.print("A4");
delay(3000);
firstRun = 0;
}
}
}
}
And do you have pulldowns fitted?
I got it working - it seems I had a embedded 'if' problem.
Also, tidier code, and debug your buttons! Yay!
#include <Button.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Button button1 = Button(8, PULLDOWN);
Button button2 = Button(9, PULLDOWN);
int firstRun = 0;
int b1 = 0;
int b2 = 0;
void setup() {
lcd.begin(16, 2);
}
void loop() {
buttonState();
if(firstRun == 0) { // Where are we?
firstRun = 1;
lcd.setCursor(0,0);
lcd.print("Q1"); // Print Question
lcd.setCursor(0,1);
lcd.print("Yes or No above");
}
if(b1 == 1) { // if yes is pressed, print Q2
if(firstRun == 1) {
question();
lcd.print("Q2");
firstRun = 2;
}
}
if(b1 == 1) {
if(firstRun == 2) { // if yes is pressed again, print A1
lcd.clear();
lcd.print("A1");
delay(3000); // wait for a bit
lcd.clear();
firstRun = 0; // got back to the beggining
}
}
if(b2 == 1) { // if no is pressed, print A2
if(firstRun == 2) {
lcd.clear();
lcd.print("A2");
delay(3000);
lcd.clear();
firstRun = 0;
}
}
if(b2 == 1) { // if no is pressed , print Q3
if(firstRun == 1) {
question();
lcd.print("Q3");
firstRun = 3;
}
}
if(b1 == 1) {
if(firstRun == 3) {
lcd.clear();
lcd.print("A3");
delay(3000);
lcd.clear();
firstRun = 0;
}
}
if(b2 == 1) {
if(firstRun == 3) {
lcd.clear();
lcd.print("A4");
delay(3000);
lcd.clear();
firstRun = 0;
}
}
}
void buttonState() {
delay(100);
b1 = button1.isPressed();
b2 = button2.isPressed();
}
void question() {
lcd.clear();
lcd.setCursor(3,1);
lcd.print("Y or N");
lcd.setCursor(0,0);
delay(500);
buttonState();
}
/*
void loop() { /////////////////////////////////////DEBUG/////////////////////////////////////
buttonState();
lcd.setCursor(0,0);
lcd.print(b1);
lcd.setCursor(0,1);
lcd.print(b2);
} /////////////////////////////////////DEBUG////////////////////////////////////
*/
I am 100% certain I am beating around the bat, and this is stupidly clomplex, but, it works!
if(firstRun == 2) {
lcd.clear();
lcd.print("A2");
delay(3000);
lcd.clear();
firstRun = 0;
Anytime you have repetitive code blocks which is mainly re-used steps with only marginal differences... it screams to become a subroutine where you just pass it parameters.
How would I add perameters so that it prints something different each time?