Go Down

Topic: Simon Says project (Read 454 times) previous topic - next topic

Lilrex

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



Code: [Select]


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


wolframore

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

Code: [Select]
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
Bad Boys Rate Our Young Girls But Violet Goes willingly - Get Some Now :) - ELI the ICE man

Lilrex

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

wolframore

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.
Bad Boys Rate Our Young Girls But Violet Goes willingly - Get Some Now :) - ELI the ICE man

Lilrex

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

wolframore

what does the serial output give you?
Bad Boys Rate Our Young Girls But Violet Goes willingly - Get Some Now :) - ELI the ICE man

Lilrex

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.

wolframore

#7
Mar 25, 2019, 04:42 pm Last Edit: Mar 25, 2019, 04:43 pm by wolframore
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.
Bad Boys Rate Our Young Girls But Violet Goes willingly - Get Some Now :) - ELI the ICE man

Lilrex

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

Lilrex

#9
Mar 26, 2019, 01:26 am Last Edit: Mar 26, 2019, 01:27 am by Lilrex
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.



Lilrex

Here is the serial output

cattledog

Quote
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
Code: [Select]
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.

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

Lilrex

Can you confirm the button wiring and the state of these variables
Code: [Select]
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

Quote
"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.

cattledog



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.

Lilrex

omg, i didnt know that.


Ty. let me try that

Go Up