Hello All,
I am new to this forum, I am in a robotics class where we are learning to use microcontrollers. For my main project I have chosen to make my own Simon says door lock game.
I am having an issue right now where my userIn() function is NOT awaiting the user input and is rather just ignoring the if parameters and turning off and on the lights.
Can anyone assist me in shedding some light on where I am going wrong??
const int BLUE = 3; //blue goes to pin 3
const int RED = 4; // red goes to pin 4
const int GREEN = 5; // green goes to pin 5
const int YELLOW = 6; // yellow goes to pin 6
int time = 1000; //this sets the delay to 1 sec, used as variable incase global scope maniuplation is needed.
int blueButton = 22; // button corresponding to the blue input button on pin 7
int redButton = 24; // button for red light input on pin 8
int greenButton = 26; // button for green button input on pin 9
int yellowButton = 28; // yellow input button on pin 10
int BB = 0;
int RB = 0;
int GB = 0;
int YB = 0;
/* Since we are using an Arduino MEGA 2560 board there is about 3x the number of avaible features compared to the standard board. 9 and 10 on arduino regular are for the servo motor
this might still be the case on the MEGA. If so the button for either green or yuellow will be moved to a new digital pin.*/
int user[4]= {}; // user array input
int seq[4]= {}; // generated array
int rng = 0; //random number genreator holding variable
int test = 0; // used to track the size of the array math and make sure size is correct
boolean valid = false; // this is used at the end when comparing the user inpiut array to the generated one and complete the end of the game
boolean flag = false; // used to pause Loop();
int buttonState = 1; // 1 is unoccupied 0 is used in RandomSeed so didnt want potential conflict
//(sizeof(seq) / sizeof(seq[4])) storeage
void setup() {
Serial.begin(9600);
/* Set all the LEDs as output devices */
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(YELLOW, OUTPUT);
/* Set the digital buttons as input devices */
pinMode(blueButton , INPUT_PULLUP);
pinMode(redButton , INPUT_PULLUP);
pinMode(greenButton , INPUT_PULLUP);
pinMode(yellowButton , INPUT_PULLUP);
BB = digitalRead(blueButton);
RB = digitalRead(redButton);
GB = digitalRead(greenButton);
YB = digitalRead(yellowButton);
randomSeed(analogRead(0)); // this randomizes everytime the program is ran
delay(2000);
/* This block below tests all the LEDs on start up to make sure the wiring is correct. It holds the on position for 1 sec */
digitalWrite(BLUE, HIGH);
delay(time);
digitalWrite(BLUE, LOW);
Serial.println("BLUE is working");
digitalWrite(RED, HIGH);
delay(time);
digitalWrite(RED, LOW);
Serial.println("RED is working");
digitalWrite(GREEN, HIGH);
delay(time);
digitalWrite(GREEN, LOW);
Serial.println("GREEN is working");
digitalWrite(YELLOW, HIGH);
delay(time);
digitalWrite(YELLOW, LOW);
Serial.println("YELLOW is working");
delay(2000); // wait 2 seconds before going into the loop to make sure light are in the correct order.
}
/* ---------------------------------------- */
/* ---------------------------------------- */
/* ---------------------------------------- */
void loop()
{
rngLv1();
while(flag == false)
{
}
} // end of Loop()
/* ---------------------------------------- */
/* ---------------------------------------- */
/* ---------------------------------------- */
void rngLv1()
{
for( int i = 0; i < 4; i++)
{
rng = random(0,4);
seq[i] = rng;
}
checkSize(seq);
} //end of rng function
void checkSize(int seq[])
{
for(int i = 0; i < 4; i++)
{
Serial.println(seq[i]);
}
playColors(seq);
} // end of checkSize
/* ---------------------------------------- */
/* ---------------------------------------- */
/* ---------------------------------------- */
void playColors(int seq[])
{
for( int x = 0; x < 4; x++)
{
if(seq[x] == 0)
{
BlueLight();
}
else if(seq[x] == 1)
{
RedLight();
}
else if(seq[x] == 2)
{
GreenLight();
}
else if(seq[x] == 3)
{
YellowLight();
}
else{
Serial.println("Something is busted, FeelsBadMan");
}
}
userIn();
}
/* ---------------------------------------- */
/* ---------------------------------------- */
/* ---------------------------------------- */
void userIn() // this area is broken
{
for (int i = 0; i < 4;)
while(buttonState !=4)
{
{
if(BB == LOW)
{
digitalWrite(BLUE, HIGH);
Serial.println("Blue pressed");
user[i] = 0;
Serial.println("Blue pressed 2");
delay(time);
Serial.println("Blue pressed 3");
digitalWrite(BLUE, LOW);
delay(time);
Serial.println("Blue pressed 4");
}
i++;
if(RB == LOW)
{
digitalWrite(RED, HIGH);
Serial.println("RED pressed");
user[i] = 1;
Serial.println("RED pressed 2");
delay(time);
Serial.println("RED pressed 3");
digitalWrite(RED, LOW);
delay(time);
Serial.println("RED pressed 4");
}
i++;
}
buttonState++;
}
} // end of user input function
/* ---------------------------------------- */
/* ---------------------------------------- */
/* ---------------------------------------- */
boolean compareUser(int seq[] , int user[])
{
int i = 0;
for (int y = 0; y < 4; y++)
{
if(seq[y] == user[y])
{
Serial.println("Match good job");
i++;
}
if( i == 4 || i == 3)
{
valid = true;
}
if(seq[y] != user[y])
{
fail();
valid = false;
}
}
}; // end of comparing function
/* ---------------------------------------- */
/* ---------------------------------------- */
/* ---------------------------------------- */
void fail()
{
Serial.println("Sequence is not matching, better luck next time");
}
/* ---------------------------------------- */
/* ---------------------------------------- */
/* ---------------------------------------- */
void BlueLight()
{
digitalWrite(BLUE, HIGH);
delay(time);
digitalWrite(BLUE, LOW);
delay(time);
};
void RedLight()
{
digitalWrite(RED, HIGH);
delay(time);
digitalWrite(RED, LOW);
delay(time);
}
void GreenLight()
{
digitalWrite(GREEN, HIGH);
delay(time);
digitalWrite(GREEN, LOW);
delay(time);
}
void YellowLight()
{
digitalWrite(YELLOW, HIGH);
delay(time);
digitalWrite(YELLOW, LOW);
delay(time);
}