Simon Says project

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);
  
}

Is this a mistake? It is outside of your function and seems like it's floating out there...

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

no its not a mistake. If it proves to be a problem I will fix that after.

Just take it out for now and see if your sketch continues... it doesn't look right to me... it clearly says end of function before this.

the problem is not the comparing function, the problem is the UserIn() function. compare does not even get used yet.

what does the serial output give you?

The serial outputs all the serial print lines for all the if statements. It is just ignoring the if condition and processing the data within. The lights turn off and on, and the number is added to the array. but at no point did i ever press a button.

would you mind posting it? I know you know what you're seeing and talking about but I haven't a clue.... Can you post a schematics ... I'm interested in how the buttons are wired.

I am at work atm, but as soon as i get home i can.

ok I downloaded Fritzing, this is my very first time ever using it, so please forgive me if its potato look. But all the wiring connections are how i have it set up.

Here is the serial output

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??

Can you confirm the button wiring and the state of these variables

BB = digitalRead(blueButton);
  RB = digitalRead(redButton);
  GB = digitalRead(greenButton);
  YB = digitalRead(yellowButton);

With INPUT_PULLUP, the buttons should not be reading LOW. When I run your code with no buttons but INPUT_PULLUP on the pins I don't enter the conditional statements and my output is limited.

BLUE is working
RED is working
GREEN is working
YELLOW is working
2
0
3
2

cattledog:
Can you confirm the button wiring and the state of these variables

BB = digitalRead(blueButton);

RB = digitalRead(redButton);
  GB = digitalRead(greenButton);
  YB = digitalRead(yellowButton);




With INPUT_PULLUP, the buttons should not be reading LOW. When I run your code with no buttons but INPUT_PULLUP on the pins I don't enter the conditional statements and my output is limited.

The buttons are wired correctly, they are wired the same as i have showed in the Fritz diagram.

and i am unsure what you mean by

"With INPUT_PULLUP, the buttons should not be reading LOW.

I am sorry, i am very very new to microcontrollers I swear i am not trying to sound ignorant.

Wire diagonally across the buttons, or rotate the buttons 90 degrees. Those 4 leg buttons have two legs which are connected and two legs which are switched. I believe you have picked up the connected legs,and your buttons read LOW when not pressed.

omg, i didnt know that.

Ty. let me try that

ok so my buttons are working... for some reason the leds are staying on and turning off when i press the button down but either way it is working.

but when i call the userIn() function the code just ignores my checks and turns on the lights, prints the serial line and then progresses down.

   for (int i = 0;  i < 4;)

  while(buttonState !=4)
  {
  {
    if(buttonState != 4 && BB == LOW)

I am trying to "pause" the program and await the users button selection, and then add those button selections to an array so i can compare them afterwards.

I know arduino cannot be paused thats why it is in " " but i need something similar.

but when i call the userIn() function the code just ignores my checks and turns on the lights, prints the serial line and then progresses down.

When you enter the function, add serial print outs of the variables used in the conditional tests.

if(buttonState != 4 && BB == LOW)

What is the value of BB? If the switch is wired correctly, and the input pin is set to INPUT_PULLUP mode, the value of BB can not be low if it is not pressed.

I think you have some extra brackets in userIn(), but I don't think that is what is causing your issue.

ok so my buttons are working... for some reason the leds are staying on and turning off when i press the button down but either way it is working.

Why are the led's wired so that one leg is common with a button pin leg? I would suggest moving the leds to a different place on the breadboard where they are independent of the buttons..

So much easier when we can see the wiring... he's right you need the get the LED's away from the same row as the buttons.