Hey y'all,
I'm hoping to pick some of your brains and get some help with my project and learn some things from you coding wizards if you can spare a few minutes. I am new to programming and this is my third project for this class and I bit off more than I could chew with this project. So forgive my ignorance and my inexperience, and my hope is to learn from your vast knowledge experience.
Alright for my project. I decided to to try and make a Simon Says like game with a Arduino Uno board. I can get the code to to run through the startup sequence and begin the game, but it fails and boots me back to the beginning. Here is my code.
//Project 2 memgame v1.11
//Jesse Owens
//https://www.tinkercad.com/things/9RB0pqE4dom-project-2-memgame-v12/editell
//changelog
//adjusted loop and functions
//constants
byte led1 = 10;
byte led2 = 11;
byte led3 = 12;
byte led4 = 13;
byte butt1 = 7;
byte butt2 = 6;
byte butt3 = 5;
byte butt4 = 4;
//variables
bool mem1;
int level = 0;
const int levelmax = 100;
int pattern[levelmax];
int yourpattern[levelmax];
unsigned long startMillis = 0;
unsigned long startMillisflash = 0;
unsigned long startMilliswait = 0;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
int startOnTime = 1200;
int startOffTime = 2000;
int stateled1 = LOW;
int dflash = 800;
int fflash = 500;
int speed = 1000;
void setup()
{
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(butt1, INPUT);
pinMode(butt2, INPUT);
pinMode(butt3, INPUT);
pinMode(butt4, INPUT);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
Serial.begin(9600); //print setup
}
void loop()
{
bool mem1 = digitalRead(butt1);
Serial.println(mem1);
currentMillis = millis(); //curent timer
digitalRead(level);
//start game
if (level == 0)
{
digitalRead(mem1);
if (mem1 == 1)
{
flashall();
level = 1;
}
else
{
startup();
}
}
if (level >= 1)
{
generatepattern();
displaypattern();
readpattern();
}
}
void startup() //modified "blinkwithoutdelay" function on the IDE found online
{
if ((stateled1 == LOW) && (currentMillis - previousMillis >= startOffTime))
{
stateled1 = HIGH; // turn on led1
previousMillis = currentMillis; // Remember the time
digitalWrite(led1, stateled1);
}
else if ((stateled1 == HIGH) && (currentMillis - previousMillis >= startOnTime))
{
stateled1 = LOW; // Turn off led1
previousMillis = currentMillis; // Remember the time
digitalWrite(led1, stateled1);
}
}
void flashall() //function to flash lights
{
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
delay(dflash);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
}
void generatepattern() //function to generate random pattern
{
randomSeed(millis());
for (int i = 0; i < levelmax; i++)
{
pattern[i] = random(10,13);
}
}
void readpattern() //function to read input from player and check against generated pattern.
{
int verify = 0; //this flag indicates if the sequence is correct
for (int i = 0; i < level; i++)
{
verify = 0;
while(verify == 0)
{
if (digitalRead(7) == LOW)
{
digitalWrite(led1, HIGH);
yourpattern[i] = 10;
verify = 1;
delay(200);
if (yourpattern[i] != pattern[i])
{
fail();
return;
}
digitalWrite(led1, LOW);
}
if (digitalRead(6) == LOW)
{
digitalWrite(led2, HIGH);
yourpattern[i] = 11;
verify = 1;
delay(200);
if (yourpattern[i] != pattern[i])
{
fail();
return;
}
digitalWrite(led2, LOW);
}
if (digitalRead(5) == LOW)
{
digitalWrite(led3, HIGH);
yourpattern[i] = 12;
verify = 1;
delay(200);
if (yourpattern[i] != pattern[i])
{
fail();
return;
}
digitalWrite(led3, LOW);
}
if (digitalRead(4) == LOW)
{
digitalWrite(led4, HIGH);
yourpattern[i] = 13;
verify = 1;
delay(200);
if (yourpattern[i] != pattern[i])
{
fail();
return;
}
digitalWrite(led4, LOW);
}
}
}
pass();
}
void displaypattern() //function to display code from generatepattern() function
{
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
for (int i = 0; i < level; i++)
{
digitalWrite(pattern[i], HIGH);
delay(speed);
digitalWrite(pattern[i], LOW);
delay(200);
}
}
void pass() //function to proceed to next level when correct input is entered.
{
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
flashall();
if (level < levelmax);
{
level++;
}
}
void fail() //function to reset game when incorrect input is entered.
{
for (int i = 0; i < 3; i++)
flashall();
delay (fflash);
flashall();
level = 0;
}
Included in the title block of my code is a link to my tinkercad circuit, but I'll attach a photo as well.
There are many things I am unfamiliar with and learning with this project, like the millis() function, and the randomseed() function, so bare with me if my code is awful.
Thank you for your time and for your help and suggestions. Hope you all have a great night!
Jesse