I had just finished making the project with the LCD from Genuino Uno and as I had some programming experience as well as a Genuino that could display 16 Characters across 2 Lines to an LCD, I though I was ready for some fun. Basically I took that project, removed the tilt sensor and instead connected two push button switches. I then wrote out some code to work with it. Basically, the code would ask a question and you answer Yes or No. I mostly just asked about if you liked this considering that the experience was more about me learning than about making something useful.
The code worked pretty well with only one minor and one major issue;
Even though I had randomised the questions, question number 7 ("Do you like Corn Chips") always came first followed by a question on Marshmallows (number 1).
The more major issue was that regardless of which button I had pressed, the answer the LCD displayed was always No, even when I had pressed yes. But only for the first one. Then it seemed to be random for some weird reason.
Here is the code:
#include <LiquidCrystal.h> // Import LCD
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Allocate ports for LCD
const int yesPin = 8; // Defining digital pin 8 as linking to 'yes' push button switch.
const int noPin = 9; // Same as for yes but for no.
int receivedAnswer = 0; // Some vars to help manage display
int askedQuestion = 0;
int pressedNo = 0;
int pressedYes = 0;
int question; // Var to randomise to create a random set of questions.
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // Initalise Serial at 9600.
pinMode(yesPin, INPUT); // Digital Pin 8
pinMode(noPin, INPUT); // Digital Pin 9. Both input mode.
lcd.begin(16, 2); // Initalise LCD Display.
lcd.print("I will ask you,"); // Inital strings to display on LCD including delays in between, resetting cursor (to following line, etc) and clearing LCD.
delay(2500);
lcd.setCursor(0, 1);
lcd.print("A question.");
delay(2500);
lcd.clear();
lcd.print("Answer yes");
delay(2500);
lcd.setCursor(0, 1);
lcd.print("Or answer no.");
delay(2500);
lcd.clear();
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(yesPin) == HIGH && receivedAnswer == 0){ // Checking if we have NOT received an answer yet and the digitalPin marked yesPin (8) is reading HIGH.
// Yes!
Serial.println("Pressed Yes!"); // Output to be read.
pressedYes = 1; // Re defining vars to assist with LCD management.
receivedAnswer = 1;
lcd.clear(); // Clear LCD.
}
if (digitalRead(noPin) == HIGH && receivedAnswer == 0) { // The same thing as before (with the yes).
// No!
pressedNo = 1;
Serial.println("Pressed No!");
receivedAnswer = 1;
lcd.clear();
}
while (receivedAnswer == 0 && askedQuestion == 0) { // While we have not received answer nor asked quesiton.
question = random(8); // Generate random number to ask question from.
switch (question) { // Switch statement from 0 - 7
case 0:
lcd.print("Do you like ");
lcd.setCursor(0, 1);
delay(2500);
lcd.print("Chocolate?");
delay(2500);
break;
case 1:
lcd.print("Do you like ");
lcd.setCursor(0, 1);
delay(2500);
lcd.print("Marshmallows?");
delay(2500);
break;
case 2:
lcd.print("Do you like ");
lcd.setCursor(0, 1);
delay(2500);
lcd.print("Pizza?");
delay(2500);
break;
case 3:
lcd.print("Do you like ");
lcd.setCursor(0, 1);
delay(2500);
lcd.print("Cake?");
delay(2500);
break;
case 4:
lcd.print("Do you like ");
lcd.setCursor(0, 1);
delay(2500);
lcd.print("Cheese?");
delay(2500);
break;
case 5:
lcd.print("Do you like ");
lcd.setCursor(0, 1);
delay(2500);
lcd.print("Rain?");
delay(2500);
break;
case 6:
lcd.print("Do you like ");
lcd.setCursor(0, 1);
delay(2500);
lcd.print("Sand?");
delay(2500);
break;
case 7:
lcd.print("Do you like ");
lcd.setCursor(0, 1);
delay(2500);
lcd.print("Corn Chips?"); // Quick side note, for some reason whenever I run my Genuino Uno it seems to always ask Corn Chips first and then Marshmallows.
delay(2500);
break;
}
askedQuestion = 1; // We have asked the question, don't need to do it again until we have a response.
}
if (pressedYes == 1) { // If Yes switch was pressed.
lcd.clear();
lcd.print("You answered "); // Print stuff to LCD.
lcd.setCursor(0, 1);
delay(2500);
lcd.print("Yes!");
delay(2500);
lcd.clear();
pressedYes = 0; // We have printed what we need to the LCD. Don't need to do it again.
receivedAnswer = 0;
askedQuestion = 0;
}
if (pressedNo == 1) { // Exactly the same as above but for No
lcd.clear();
lcd.print("You answered ");
lcd.setCursor(0, 1);
delay(2500);
lcd.print("No!");
delay(2500);
lcd.clear();
pressedNo = 0;
receivedAnswer = 0;
askedQuestion = 0;
}
}
I hope you can help me. I have proofread this code numerous times and I can't find any issues.
Thank you!