Can someone please help me with this project
switch_caseLEDCIRCLE.ino (4.51 KB)
Can someone please help me with this project
switch_caseLEDCIRCLE.ino (4.51 KB)
Delta_G politely converted your .ino file into a readable web format for you and subsequently asked the root of your issue.
In the future, repay him the favor by using the "</>" button in order for him to assist you code changes:
code] Place your code here [/code
+1
Hi everyone,
Simon is my student in my software design class. He is trying to create a simon says game on the esplora with an lcd screen attached. He needs to be able create an array to store the value of the first colour then match the keypress then if it's correct add another colour to the sequence and continue with the matching the keypresses with the colours generated. This will continue until 10 colours are correctly matched and the game ends. I am guessing he needs to create a dynamically increasing array. If anyone can help he's appreciate it.
#include <SPI.h>
#include <TFT.h>
#include <Esplora.h>
String redLight = "Red";
String greenLight = "Green";
String yellowLight = "yellow";
String blueLight = "Blue";
void setup() {
// initialize the screen
EsploraTFT.begin();
// make the background black
EsploraTFT.background(0,0,0);
delay(1000);
// set the stroke color to blue
EsploraTFT.stroke(255,0,0);
// set the fill color to blue
EsploraTFT.fill(255,0,0);
// draw a circle at the bottom of screen
EsploraTFT.circle(80,100,15);
// set the stroke color to green
EsploraTFT.stroke(0,255,0);
// set the fill color to green
EsploraTFT.fill(0,255,0);
// draw a circle in the center of screen
EsploraTFT.circle(40,60,15);
// set the stroke color to red
EsploraTFT.stroke(0,0,255);
// set the fill color to red
EsploraTFT.fill(0,0,255);
// draw a circle in the center of screen
EsploraTFT.circle(80,20,15);
// set the stroke color to yellow
EsploraTFT.stroke(0,255,255);
// set the fill color to yellow
EsploraTFT.fill(0,255,255);
// draw a circle
EsploraTFT.circle(120,60,15);
//Global Variables
int btn1, btn2, btn3, btn4;
int num1, num2, num3, num4;
int input;
}
void loop() {
int input;
unsigned int index;
int num;
int colourNum[9];
for(int index = 0; index < 10; index++) {
delay(500);
randomSeed(analogRead(3));
num = random(1,5);
Serial.print("num is ");
Serial.println(num);
switch(num){
case 1:
Esplora.writeRGB(0, 0, 50);
// set the stroke color to black
EsploraTFT.stroke(0,0,0);
// set the fill color to black
EsploraTFT.fill(0,0,0);
// draw a circle at the bottom of screen
EsploraTFT.circle(80,100,15);
delay(500);
// set to blue
EsploraTFT.stroke(255,0,0);
// set the fill color to blue
EsploraTFT.fill(255,0,0);
// draw a circle at the bottom of screen
EsploraTFT.circle(80,100,15);
delay(500);
colourNum[index] = num;
Serial.print("colourNum[index] is");
Serial.println(colourNum[index]);
checkButton();
return;
if (colourNum[index] = input){
Serial.print("Input returned");
}
break;
case 2:
Esplora.writeRGB(0, 50, 0);
// set the stroke color to black
EsploraTFT.stroke(0,0,0);
// set the fill color to black
EsploraTFT.fill(0,0,0);
// draw a circle at the bottom of screen
EsploraTFT.circle(40,60,15);
delay(500);
// set to green
EsploraTFT.stroke(0,255,0);
// set the fill color to green
EsploraTFT.fill(0,255,0);
// draw a circle in the center of screen
EsploraTFT.circle(40,60,15);
delay(500);
colourNum[index] = num;
Serial.print("colourNum[index] is");
Serial.println(colourNum[index]);
checkButton();
return;
if (colourNum[index] = input){
Serial.print("Input returned");
}
break;
case 3:
Esplora.writeRGB(50, 0, 0);
//Serial.println(redLight);
// set the stroke color to black
EsploraTFT.stroke(0,0,0);
// set the fill color to black
EsploraTFT.fill(0,0,0);
// draw a circle at the bottom of screen
EsploraTFT.circle(80,20,15);
delay(500);
//set to red
EsploraTFT.stroke(0,0,255);
// set the fill color to red
EsploraTFT.fill(0,0,255);
// draw the circle at the bottom of screen
EsploraTFT.circle(80,20,15);
delay(500);
colourNum[index] = num;
Serial.print("colourNum[index] is");
Serial.println(colourNum[index]);
checkButton();
return;
if (colourNum[index] = input){
Serial.print("Input returned");
}
break;
case 4:
Esplora.writeRGB(50, 50, 0);
//Serial.println(yellowLight);
// set the stroke color to black
EsploraTFT.stroke(0,0,0);
// set the fill color to black
EsploraTFT.fill(0,0,0);
// draw a circle at the bottom of screen
EsploraTFT.circle(120,60,15);
delay(500);
// set to yellow
EsploraTFT.stroke(0,255,255);
// set the fill color to yellow
EsploraTFT.fill(0,255,255);
// draw a circle in the center of screen
EsploraTFT.circle(120,60,15);
delay(500);
colourNum[index] = num;
Serial.print("colourNum[index] is ");
Serial.println(colourNum[index]);
checkButton();
return;
if (colourNum[index] = input){
Serial.print("Input returned");
}
break;
default:
Serial.println("NA");
delay(2000);
}
}
}
int checkButton() {
int input;
int btn1 = Esplora.readButton(1); //read switch 1
int btn2 = Esplora.readButton(2); //read switch 2
int btn3 = Esplora.readButton(3); //read switch 3
int btn4 = Esplora.readButton(4); //read switch 4
if (btn1 == LOW){
input = 1;
Esplora.tone(400);
Serial.print("input is " );
Serial.println(input);
}
else if (btn2 == LOW){
input = 2;
Esplora.tone(400);
Serial.print("input is " );
Serial.println(input);
}
else if (btn3 == LOW){
input = 3;
Esplora.tone(400);
Serial.print("input is " );
Serial.println(input);
}
else if (btn4 == LOW){
input = 4;
Esplora.tone(400);
Serial.print("input is " );
Serial.println(input);
}
else {
Esplora.noTone(); // Turn tone off when not pressed
}
return input;
}
[\code]
We have added some debugging output statements but it is no longer recording the input from the buttons which he needs to check against the random colour that is generated. Thanks for the array info we/he have added that to the code and it appears to be working. We would be grateful for any further help.
We tried most of what you suggested. Some worked and some didn't for various reasons. My knowledge of c is limited and esplora is limited. I teach the basics to our year 9's and 10's. This is my first time using the lcd screen. It was my idea to make the simon game. Here is where we're up to after today's lesson. The project is due after in about 3 weeks but we have a 2 week holiday starting at the end of the week.
#include <SPI.h>
#include <TFT.h>
#include <Esplora.h>
//String redLight = "Red";
//String greenLight = "Green";
//String yellowLight = "yellow";
//String blueLight = "Blue";
void setup() {
// initialize the screen
EsploraTFT.begin();
// make the background black
EsploraTFT.background(0,0,0);
delay(1000);
// set the stroke color to blue
EsploraTFT.stroke(255,0,0);
// set the fill color to blue
EsploraTFT.fill(255,0,0);
// draw a circle at the bottom of screen
EsploraTFT.circle(80,100,15);
// set the stroke color to green
EsploraTFT.stroke(0,255,0);
// set the fill color to green
EsploraTFT.fill(0,255,0);
// draw a circle in the center of screen
EsploraTFT.circle(40,60,15);
// set the stroke color to red
EsploraTFT.stroke(0,0,255);
// set the fill color to red
EsploraTFT.fill(0,0,255);
// draw a circle in the center of screen
EsploraTFT.circle(80,20,15);
// set the stroke color to yellow
EsploraTFT.stroke(0,255,255);
// set the fill color to yellow
EsploraTFT.fill(0,255,255);
// draw a circle
EsploraTFT.circle(120,60,15);
//Global Variables
//int btn1, btn2, btn3, btn4;
int num1, num2, num3, num4;
int input;
}
void loop() {
unsigned int index = 0;
int num;
int colourNum[3];
//int thisSelectionInput = checkButton();
for(int index = 0; index < 4; index++) {
delay(500);
randomSeed(analogRead(3));
num = random(1,5);
Serial.print("num is ");
Serial.println(num);
//colourNum[index] = {num};
//Serial.print("colourNum[index] is ");
//Serial.println(colourNum[index]);
switch(num){
case 1:
Esplora.writeRGB(0, 0, 50);
// set the stroke color to black
EsploraTFT.stroke(0,0,0);
// set the fill color to black
EsploraTFT.fill(0,0,0);
// draw a circle at the bottom of screen
EsploraTFT.circle(80,100,15);
delay(500);
// set to blue
EsploraTFT.stroke(255,0,0);
// set the fill color to blue
EsploraTFT.fill(255,0,0);
// draw a circle at the bottom of screen
EsploraTFT.circle(80,100,15);
delay(500);
colourNum[index] = num;
Serial.print("colourNum[index] is ");
Serial.println(colourNum[index]);
return;
break;
case 2:
Esplora.writeRGB(0, 50, 0);
// set the stroke color to black
EsploraTFT.stroke(0,0,0);
// set the fill color to black
EsploraTFT.fill(0,0,0);
// draw a circle at the bottom of screen
EsploraTFT.circle(40,60,15);
delay(500);
// set to green
EsploraTFT.stroke(0,255,0);
// set the fill color to green
EsploraTFT.fill(0,255,0);
// draw a circle in the center of screen
EsploraTFT.circle(40,60,15);
delay(500);
colourNum[index] = num;
Serial.print("colourNum[index] is ");
Serial.println(colourNum[index]);
return;
break;
case 3:
Esplora.writeRGB(50, 0, 0);
//Serial.println(redLight);
// set the stroke color to black
EsploraTFT.stroke(0,0,0);
// set the fill color to black
EsploraTFT.fill(0,0,0);
// draw a circle at the bottom of screen
EsploraTFT.circle(80,20,15);
delay(500);
//set to red
EsploraTFT.stroke(0,0,255);
// set the fill color to red
EsploraTFT.fill(0,0,255);
// draw the circle at the bottom of screen
EsploraTFT.circle(80,20,15);
delay(500);
colourNum[index] = num;
Serial.print("colourNum[index] is ");
Serial.println(colourNum[index]);
return;
break;
case 4:
Esplora.writeRGB(50, 50, 0);
//Serial.println(yellowLight);
// set the stroke color to black
EsploraTFT.stroke(0,0,0);
// set the fill color to black
EsploraTFT.fill(0,0,0);
// draw a circle at the bottom of screen
EsploraTFT.circle(120,60,15);
delay(500);
// set to yellow
EsploraTFT.stroke(0,255,255);
// set the fill color to yellow
EsploraTFT.fill(0,255,255);
// draw a circle in the center of screen
EsploraTFT.circle(120,60,15);
delay(500);
colourNum[index] = num;
Serial.print("colourNum[index] is ");
Serial.println(colourNum[index]);
return;
break;
default:
Serial.println("NA");
delay(2000);
}
int selection = checkButton();
Serial.print("The value of the button was returned");
}
}
int checkButton() {
int index;
index = 0;
int yourSequence[3];
int input;
int btn1 = Esplora.readButton(1); //read switch 1
int btn2 = Esplora.readButton(2); //read switch 2
int btn3 = Esplora.readButton(3); //read switch 3
int btn4 = Esplora.readButton(4); //read switch 4
if (btn1 == LOW){
input = 1;
Esplora.tone(400);
yourSequence[index]= input;
Serial.print("input is " );
Serial.println(input);
index++;
}
else if (btn2 == LOW){
input = 2;
Esplora.tone(400);
yourSequence[index]= input;
Serial.print("input is " );
Serial.println(input);
index++;
}
else if (btn3 == LOW){
input = 3;
Esplora.tone(400);
yourSequence[index]= input;
Serial.print("input is " );
Serial.println(input);
index++;
}
else if (btn4 == LOW){
input = 4;
Esplora.tone(400);
yourSequence[index]= input;
Serial.print("input is " );
Serial.println(input);
index++;
}
else {
Esplora.noTone(); // Turn tone off when not pressed
}
return;
}
So we tried as many of the suggestions as possible.
CyberSam
Yes I was frustrating both of us and have been getting some tuition from a colleague on how the Arduino code works. I am a lot closer to a solution than I was before as I now have a clearer idea of what is happening. I have always felt that c++ was difficult but I am starting to have a better understanding.
Here is the code as it stands at the moment. The pattern is generated and now stops flashing when it has done 4 colours. It then waits for you to push 4 buttons and then tells you well done on the serial monitor.
The only thing left is to make sure that the sequence is matching before it says well done. Personally this is enough of a challenge at the moment.
#include <Esplora.h>
#include <SPI.h>
#include <TFT.h>
//Global variables
unsigned int index = 0;
int num;
//int max_size = 3; // will use this later so that the array size can be increased
int sequence[3]; //This will be an array of size 0 to 3. i.e. 4 items.
int yourSequence[3]; //This will be an array of size 0 to 3. i.e. 4 items.
int num1, num2, num3, num4;
int btn1, btn2, btn3, btn4;
int input;
int level = 1;
int count;
void setup() {
initialiseScreen();
Serial.begin(9400); //open the serial port at 9400bps
// The open and closing curly brackets define scope…
// A variable intended for global scope must be define outside of all opening and curly
// brackets. The above lines are only seen within the setup() function scope
// That is, created every time the setup() function is called, and deleted (destroyed)
// when setup() function is exited.
}
void loop() {
initialiseArrayVariables();
for(int index = 0; index < 4; index++) {
delay(500);
if (level == 1){ //if the level is 1 then get the numbers
getRandomNumber();
showSequence(num);
sequence[index] = num; //returned from showsequence
if (index == 3) {
level = 2;
}
}
} //end for loop
yourGuess(); //I have moved this outside of the for loop as I need to get the 4 numbers before I can enter 4 numbers to be compared
yourSequence[index] = input; //not sure if the input is getting returned here but it is a global variable.
for(int index = 0; index <4; index++){
if (sequence[index] == yourSequence[index]) { // Is this the best way to test this?
count++;
}
}
if (count == 4){
Serial.print("well done");
}
} // End of infinite loop() function
void showSequence(int num) {
switch(num){
case 1:
Esplora.writeRGB(0, 0, 50);
// set the stroke color to black
EsploraTFT.stroke(0,0,0);
// set the fill color to black
EsploraTFT.fill(0,0,0);
// draw a circle at the bottom of screen
EsploraTFT.circle(80,100,15);
delay(500);
// set to blue
EsploraTFT.stroke(255,0,0);
// set the fill color to blue
EsploraTFT.fill(255,0,0);
// draw a circle at the bottom of screen
EsploraTFT.circle(80,100,15);
delay(500);
sequence[index] = num;
Serial.print("sequence[index] is ");
Serial.println(sequence[index]);
return;
break;
case 2:
Esplora.writeRGB(0, 50, 0);
// set the stroke color to black
EsploraTFT.stroke(0,0,0);
// set the fill color to black
EsploraTFT.fill(0,0,0);
// draw a circle at the bottom of screen
EsploraTFT.circle(40,60,15);
delay(500);
// set to green
EsploraTFT.stroke(0,255,0);
// set the fill color to green
EsploraTFT.fill(0,255,0);
// draw a circle in the center of screen
EsploraTFT.circle(40,60,15);
delay(500);
sequence[index] = num;
Serial.print("sequence[index] is ");
Serial.println(sequence[index]);
return;
break;
case 3:
Esplora.writeRGB(50, 0, 0);
//Serial.println(redLight);
// set the stroke color to black
EsploraTFT.stroke(0,0,0);
// set the fill color to black
EsploraTFT.fill(0,0,0);
// draw a circle at the bottom of screen
EsploraTFT.circle(80,20,15);
delay(500);
//set to red
EsploraTFT.stroke(0,0,255);
// set the fill color to red
EsploraTFT.fill(0,0,255);
// draw the circle at the bottom of screen
EsploraTFT.circle(80,20,15);
delay(500);
sequence[index] = num;
Serial.print("sequence[index] is ");
Serial.println(sequence[index]);
return;
break;
case 4:
Esplora.writeRGB(50, 50, 0);
//Serial.println(yellowLight);
// set the stroke color to black
EsploraTFT.stroke(0,0,0);
// set the fill color to black
EsploraTFT.fill(0,0,0);
// draw a circle at the bottom of screen
EsploraTFT.circle(120,60,15);
delay(500);
// set to yellow
EsploraTFT.stroke(0,255,255);
// set the fill color to yellow
EsploraTFT.fill(0,255,255);
// draw a circle in the center of screen
EsploraTFT.circle(120,60,15);
delay(500);
sequence[index] = num;
Serial.print("sequence[index] is ");
Serial.println(sequence[index]);
return;
break;
default:
Serial.println("NA");
delay(500);
} // end of for switch
}
int yourGuess() {
level = 2;
int yourSequence[3];
for(int index = 0; index < 4; index++) {
btn1 = Esplora.readButton(1); //read switch 1
btn2 = Esplora.readButton(2); //read switch 2
btn3 = Esplora.readButton(3); //read switch 3
btn4 = Esplora.readButton(4); //read switch 4
if (btn1 == LOW){
input = 1;
//Esplora.tone(400);
yourSequence[index]= input;
Serial.print("input is " );
Serial.println(input);
index++;
}
else if (btn2 == LOW){
input = 2;
//Esplora.tone(400);
yourSequence[index]= input;
Serial.print("input is " );
Serial.println(input);
index++;
}
else if (btn3 == LOW){
input = 3;
//Esplora.tone(400);
yourSequence[index]= input;
Serial.print("input is " );
Serial.println(input);
index++;
}
else if (btn4 == LOW){
input = 4;
//Esplora.tone(400);
yourSequence[index]= input;
Serial.print("input is " );
Serial.println(input);
index++;
}
else {
Esplora.noTone(); // Turn tone off when not pressed
}
//level = 1; (after I removed this line it finally stopped flashing
return; // should be returning an integer value????
}
}
void getRandomNumber() {
randomSeed(analogRead(3));
num = random(1,5);
Serial.print("num is ");
Serial.println(num);
}
void initialiseScreen() {
EsploraTFT.begin(); // initialise the screen
EsploraTFT.background(0,0,0); // make the background black
//delay(1000); // no idea why I was delaying here???
// set the stroke color to blue
EsploraTFT.stroke(255,0,0);
// set the fill color to blue
EsploraTFT.fill(255,0,0);
// draw a circle at the bottom of screen
EsploraTFT.circle(80,100,15);
// set the stroke color to green
EsploraTFT.stroke(0,255,0);
// set the fill color to green
EsploraTFT.fill(0,255,0);
// draw a circle in the center of screen
EsploraTFT.circle(40,60,15);
// set the stroke color to red
EsploraTFT.stroke(0,0,255);
// set the fill color to red
EsploraTFT.fill(0,0,255);
// draw a circle in the center of screen
EsploraTFT.circle(80,20,15);
// set the stroke color to yellow
EsploraTFT.stroke(0,255,255);
// set the fill color to yellow
EsploraTFT.fill(0,255,255);
// draw a circle
EsploraTFT.circle(120,60,15);
}
void initialiseArrayVariables() {
unsigned int index = 0;
//int thisSelectionInput = checkButton();
}