I know this has already been discussed a few times, but it's sort of different for me: I want to simulate my game on Tinkercad before I build it, but the 16x2 LCD I am using for my game half the time shows the ç (cedilla) symbol before each word. And the lcd.clear() does not always clear the words. To be clear, this is all on Tinkercad.
The following is the code for my whole game:
#define NOTE_C3 131
#define NOTE_C4 262
#define NOTE_G3 196
#define NOTE_A3 220
#define NOTE_B3 247
#define NOTE_B0 31
#define NOTE_D3 147
//Defining notes for piezo (used later on)
#include <LiquidCrystal.h>
//add LCD library to be able to print on the LCD
LiquidCrystal lcd(0,15,16,17,18,19);
//initialize the LCD
String array[] = {"Welcome!", "To this game", "This is a Simon", "Says Game!",
"In This Game,", "You Have To See", "The Pattern of",
"The LEDs in a","row of 6 blinks", "and then copy",
"them exactly", "with the buttons", "underneath them.",
"Just like a", "Simon Says Game", "There are a",
"Total of 20", "levels, and each", "level is harder",
"than the last.","You have a max", "of 5 tries.",
"Try your best!"};
//creates array of words for instructions at beginning of game
int startBut = 1; //pin number of start Button
int buz = A0; // pin number of piezo
int maxLevel = 6; // number of LED blinks in one level
int ledSeq[6]; // sequence of the LEDs
int mySeq[6]; // sequence that the player enters
int speed = 1000; // initial speed of LED blinks
int gameTimes = 0; // initial number of game errors
int level = 1; // initial level that player starts with
int points = 0;// initial points that player starts with
int lcdHelp = 0;// variable used to print out every 2nd string in LCD array
int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
// tune for game when player wins a level/the game
int noteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4
};
// duration(in musical notes) of each note in melody
void setup()
{
lcd.begin(16,2);
// turns on LCD screen
for (int i = 0; i <25; i+=2) {
lcd.setCursor(0,0);
lcd.print(array[i]);
lcd.setCursor(0,1);
lcdHelp = i+1;
lcd.print(array[lcdHelp]);
delay(500);
lcd.clear();
}
// prints out LED Alertness Game instructions
for(int ledPin = 7; ledPin > 1; ledPin--) {
pinMode(ledPin, OUTPUT);
}
pinMode(startBut, INPUT);
for (int butPin = 13; butPin > 7; butPin--) {
pinMode(butPin, INPUT);
}
//initializes the LEDs and the Buttons
Serial.begin(9600);
//turns on the Serial Monitor
}
void loop () {
if (gameTimes<4 && speed>0) /*when player has not finished the game
or entered the incorrect order 4 times*/
{
if (digitalRead(startBut) == LOW) {
delay(500);
tone (buz, NOTE_C3, 1000/4);
delay(500);
Serial.println("start");
delay(500);
getSequence();
displaySeq();
butPres();
}
/*After the player presses the start Button,a flat note buzzes and then
the LCD sequence is created, displayed, and inputted by the player*/
}
else if (gameTimes == 4) /*When the player has entered the pattern
incorrectly 4 times */
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("GAME OVER");
tone (buz, NOTE_C4, 1000);
delay(500);
tone (buz, NOTE_D3, 1000);
delay(500);
tone (buz, NOTE_B0, 1000);
delay(500);
exit(0);
// prints GAME OVER on the LCD screen and buzzes 3 sad notes
}
else //when the player finished the game
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("CONGRAGULATIONS!");
lcd. setCursor(0,1);
lcd.print("YOU BEAT THE GAME!");
delay (1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("TOTAL POINTS: " + points);
lcd.clear();
/*prints congragulatory message
and prints out player's points*/
for (int thisNote = 0; thisNote < 8; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(buz, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(buz);
} // plays happy tune from melody method
exit (0); //turns off the game
}
}
void displaySeq () {
for (int k = 7; k>1; k--) {
digitalWrite(k, LOW);
}
/* Turns off all of the LEDs before starting the
display sequence */
delay(500);
for (int i = 0; i<maxLevel; i++) {
digitalWrite(ledSeq[i], HIGH);
delay(speed);
digitalWrite(ledSeq[i], LOW);
delay(500);
Serial.println("show");
}
// displays the previously random generated LED sequence
}
void butPres () {
Serial.println("begin");
lcd.setCursor(0,0);
lcd.print("BEGIN");
// prints BEGIN on the LCD screen
int result = 0;
// Variable used to break out of while loop
for (int i = 0; i< maxLevel; i++) {
result = 0;
while (result == 0) {
// loop used to pause the code until a button is pressed
if (digitalRead(13) == HIGH) {
Serial.println("press1");
digitalWrite(7, HIGH);
result = 1;
mySeq[i] = 7;
delay(500);
if (mySeq[i] != ledSeq[i] ) {
incorrectSeq();
return;
}
digitalWrite(7, LOW);
}
if (digitalRead(12) == HIGH) {
Serial.println("press2");
digitalWrite(6, HIGH);
result = 1;
mySeq[i] = 6;
delay(500);
if (mySeq[i] != ledSeq[i] ) {
incorrectSeq();
return;
}
digitalWrite(6, LOW);
}
if (digitalRead(11) == HIGH) {
Serial.println("press3");
digitalWrite(5, HIGH);
result = 1;
mySeq[i] = 5;
delay(500);
if (mySeq[i] != ledSeq[i] ) {
incorrectSeq();
return;
}
digitalWrite(5, LOW);
}
if (digitalRead(10) == HIGH) {
Serial.println("press4");
digitalWrite(4, HIGH);
result = 1;
mySeq[i] = 4;
delay(500);
if (mySeq[i] != ledSeq[i] ) {
incorrectSeq();
return;
}
digitalWrite(4, LOW);
}
if (digitalRead(9) == HIGH) {
Serial.println("press5");
digitalWrite(3, HIGH);
result = 1;
mySeq[i] = 3;
delay(500);
if (mySeq[i] != ledSeq[i] ) {
incorrectSeq();
return;
}
digitalWrite(3, LOW);
}
if (digitalRead(8) == HIGH) {
Serial.println("press6");
digitalWrite(2, HIGH);
result = 1;
mySeq[i] = 2;
delay(500);
if (mySeq[i] != ledSeq[i] ) {
incorrectSeq();
return;
}
digitalWrite(2, LOW);
}
}
/*code to receive the button press,
record it in an array, and if
the button press does not match
the LED displayed, then jumps to the
incorrect sequence method, otherwise
continues recording button presses*/
}
rightSeq();
/* if all button presses were correct, implements
the right sequence method. */
Serial.println("end");
}
void getSequence () {
Serial.println("get");
randomSeed(millis());
// initializes code to generate random sequence
for (int i = 0; i< maxLevel; i++) {
ledSeq[i] = random (2,8);
}
//generates random LED seqeuence and stores it in an array
}
void incorrectSeq () {
Serial.println("incorrect");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("WRONG ORDER");
tone (buz, NOTE_C4, 1000/4);
delay(500);
tone (buz, NOTE_D3, 1000/4);
delay(500);
tone (buz, NOTE_B0, 1000/4);
//buzzes a sad tone
speed = 1000;
gameTimes++;
// resets the speed and adds 1 gameTime
for (int j = 7; j> 1; j--) {
digitalWrite(j, HIGH);
}
delay (500);
for (int j = 7; j> 1; j--) {
digitalWrite(j, LOW);
}
delay(500);
for (int j = 7; j> 1; j--) {
digitalWrite(j, HIGH);
}
//blinks all LEDs on off on
}
void rightSeq()
{
Serial.println("correct");
for (int thisNote = 0; thisNote < 8; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(buz, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(buz);
}
//buzzes a happy tune on the piezo
for (int j = 7; j> 1; j--) {
digitalWrite(j, HIGH);
}
delay (500);
for (int j = 7; j> 1; j--) {
digitalWrite(j, LOW);
}
delay(500);
for (int j = 7; j> 1; j--) {
digitalWrite(j, HIGH);
}
delay(500);
for (int j = 7; j> 1; j--) {
digitalWrite(j, LOW);
}
delay(500);
for (int j = 7; j> 1; j--) {
digitalWrite(j, HIGH);
}
//blinks the LED on off on off on
speed-=200;
level++;
points += level;
/*decreases the speed by 200, increments the level,
*/
lcd.setCursor(0,0);
lcd.print("POINTS: " + points);
delay(500);
lcd.clear();
delay(1000);
//prints out the number of points then clears the LCD screen
}
void instructMethod () {
for (int i = 0; i <25; i+=2) {
lcd.setCursor(0,0);
lcd.print(array[i]);
lcd.setCursor(0,1);
lcdHelp = i+1;
lcd.print(array[lcdHelp]);
delay(1000);
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(" ");
}
}
I mainly start having an issue in the butPres() method, specifically when the word BEGIN is displayed, there is a ç before it. And afterwards, when lcd.clear() is supposed to clear BEGIN, it just stays on the display. And later, when I press the wrong button, and the incorrectSeq() method is implemented, the words WRONG ORDER are printed after the button, even though there is a lcd.clear() before it.
The following is a video showing my problem:
This is my first time posting, and I am an extreme beginner at coding, so I'm sorry in advance if most of my code is redundant or doesn't make sense. Thank you for the help!