I suppose the typical introduction should go here...
I am relatively new to Arduino. Help please! ![]()
I'm having some fun with an LCD, and feel like posting my code to see if there's any better way of doing things. Please note that I haven't even uploaded this to my Arduino to see if it works as intended, but it seems to compile fine.
The idea is for it to print words letter by letter, and depending on user input, display relevant information. So, without further adieu, here is the code:
//////////////////////////////////////////////////////////////////
/////////// WOULD YOU LIKE TO PLAY A GAME? /////////////
//////////////////////////////////////////////////////////////////
// LIBRARIES //
#include <LiquidCrystal.h>
// INITIALIZE LIBRARIES //
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// PIN ASSIGNMENTS //
#define TRUEBUTTON 7
#define FALSEBUTTON 8
// GLOBAL VARIABLES //
boolean introPassed = false;
int typeDelay = 300;
char stringIntroLine1[] = {'W','o','u','l','d',' ','y','o','u',' ','l','i','k','e'};
char stringIntroLine2[] = {'t','o',' ','p','l','a','y',' ','a',' ','g','a','m','e','?'};
char stringStage1Line1[] = {'F','a','i','r',' ','e','n','o','u','g','h','.'};
char stringStage1Line2[] = {'L','e','t','\'','s',' ','b','e','g','i','n','.'};
int introLine1Length = 14;
int introLine2Length = 15;
int stage1Line1Length = 12;
int stage1Line2Length = 12;
// SETUP //
void setup() {
lcd.begin(16, 2);
lcd.blink();
pinMode(TRUEBUTTON, INPUT);
pinMode(FALSEBUTTON, INPUT);
}
// MAIN CODE //
void loop() {
// WE ONLY WANT THE INTRO TO RUN THROUGH ONCE! //
if (introPassed == false) {
for(int n = 0; n < introLine1Length; n++) {
lcd.print(stringIntroLine1[n]);
delay(typeDelay);
}
lcd.setCursor(0, 1);
for(int n = 0; n < introLine2Length; n++) {
lcd.print(stringIntroLine2[n]);
delay(typeDelay);
}
int trueButtonState = digitalRead(TRUEBUTTON); // WE'LL NEED THIS FOR THE NEXT PART!
int falseButtonState = digitalRead(FALSEBUTTON); //WE'LL NEED THIS FOR THE NEXT PART!
while (trueButtonState == LOW && falseButtonState == LOW) {
trueButtonState = digitalRead(TRUEBUTTON);
falseButtonState = digitalRead(FALSEBUTTON);
}
lcd.clear();
if (trueButtonState == HIGH) {
introPassed = !introPassed;
for(int n = 0; n < stage1Line1Length; n++) {
lcd.print(stringStage1Line1[n]);
delay(typeDelay);
}
lcd.setCursor(0, 1);
for(int n = 0; n < stage1Line2Length; n++) {
lcd.print(stringStage1Line2[n]);
delay(typeDelay);
}
}
else if (falseButtonState == HIGH) {
gameOver();
}
else {
showError();
}
}
}
void gameOver() {
lcd.clear();
lcd.print("Haha, try again.");
lcd.setCursor(0, 1);
lcd.print("Restarting...");
delay(2000);
}
void showError() {
lcd.clear();
lcd.print("OH NO!");
lcd.setCursor(0, 1);
lcd.print("You broke me.");
delay(2000);
}
It's incomplete... sort of. ![]()