First time poster here and I was hoping on getting some help on this code I've been working on for a while. For my student project, I have chosen to write code to simulate a game of 'Simon Says' on my Rpi in #C.
I have defined all my LEDs and SWITCHES and I have reached the point where the Rpi is showing the pattern of LEDs, one iteration at a time up to 10 by using this code:
#define SWITCH_RED 25
#define LED_RED 3
#define SWITCH_GREEN 24
#define LED_GREEN 2
#define SWITCH_BLUE 23
#define LED_BLUE 0
#define SWITCH_YELLOW 22
#define LED_YELLOW 7
int Generate_Random_Sequence(int random)
{
if (random == 0)
{
return 3;
}
else if (random == 1)
{
return 2;
}
else if (random == 2)
{
return 0;
}
else if (random == 3)
{
return 7;
}
}
int Show_Sequence()
{
int pattern[10]; //Pattern will go to maximum of 10 sequence
int i = 0;
int gamestate;
int count = 1;
srand(time(NULL));
for (i = 0; i < 10; i++)
{
pattern[i] = Generate_Random_Sequence(rand()%4);
}
while (count <= 10)
{
i = 1;
while (i <= count)
{
digitalWrite(pattern[i], HIGH);
delay(500);
digitalWrite(pattern[i], LOW);
delay(500);
i++;
}
count++;
}
Example output:
1) Red Light
2) Red Light - Blue Light
3) Red Light - Blue Light - Green Light
and so forth until the pattern is at a length of 10...
What I am trying to do now is implement a function 'User_Response' that would provide the user the opportunity to enter the correct switch. If they enter the correct switch, the loop continues. If not, it returns out of the function. (Usually people have it loop back to the beginning of the pattern, but for all intents and purposes I would rather have mine just end the game/break out of the loop). Also, I am unsure on where to place this function once it has been made in my nested while loop...
I do know about (DigitalRead(SWITCH_COLOR) == HIGH/LOW)
, but I am unsure how to properly use it in this situation.
If anyone has any advice to offer, I would gladly appreciate it. :)
Aucun commentaire:
Enregistrer un commentaire