First time user here so please excuse any mistakes I may have made in regards to the post itself.
I'm doing a student project for my final in my intro class to computer programming. This semester we learned about #C. For my project, I decided to make a Simon Says game on my Raspberry Pi using switches/lights. The lights would blink and then you would have to press the correct corresponding switches to increase the pattern. The length of the pattern will be 10. After 10, the game turns off/ends.
Example:
1) Rpi shows Red LED -- User presses Red Switch = Increases pattern by one LED.
2) Rpi shows Red Light, Yellow Light -- User presses Red Switch, Yellow Switch = increases pattern by one LED.
and so forth...
Where I am having trouble is my "Show_Sequence" function, which shows the pattern to the user. It carries an array of 10 elements. However, this function is not fully correct either because it blinks the whole pattern straight off the bat. I need it to show 1/10th of the array first, then if the user enters the correct switch, show 2/10th of the array and so forth.
My main questions that I need any advice on are:
1) Writing the code to create a function called 'Read_Sequence', where the user can press the switches to match the pattern, and then increase the pattern's sequence by 1 every time they get it correct.
2) The Show_Sequence function right now shows the whole pattern. I need to edit it to the point where it will show only 1/10 of the pattern, then 2/10 of the pattern, and so forth when the user enters the correct switch. As of right now, it shows 10/10 of the pattern.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <wiringPi.h>
#include <time.h>
#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
void All_Off() // Function that shuts off all LEDs
{
digitalWrite(3, LOW);
digitalWrite(2, LOW);
digitalWrite(0, LOW);
digitalWrite(7, LOW);
delay(1000);
return;
}
int All_Blink() // Function that blinks all the LEDs
{
int gamestate;
int i;
for (i = 0; i < 4; i++)
{
digitalWrite(3, HIGH);
digitalWrite(2, HIGH);
digitalWrite(0, HIGH);
digitalWrite(7, HIGH);
delay(200);
digitalWrite(3, LOW);
digitalWrite(2, LOW);
digitalWrite(0, LOW);
digitalWrite(7, LOW);
delay(200);
}
gamestate = 1; // This gamestate = 1 will initialize the game
return gamestate;
}
void Setup_Game() // This function is essentially the startscreen. It will blink repeatedly until all buttons are pressed and held down.
{
while ( (digitalRead(SWITCH_RED) == LOW) || (digitalRead(SWITCH_GREEN) == LOW) || (digitalRead(SWITCH_BLUE) == LOW) || (digitalRead(SWITCH_YELLOW) == LOW) )
{
digitalWrite(3, HIGH);
delay(1000);
digitalWrite(2, HIGH);
delay(1000);
digitalWrite(0, HIGH);
delay(1000);
digitalWrite(7, HIGH);
delay(1000);
All_Off();
}
return;
}
int Generate_Random_Sequence(int random) // This function will return which LED will be lit
{
if (random == 0)
{
return 3;
}
else if (random == 1)
{
return 2;
}
else if (random == 2)
{
return 0;
}
else if (random == 3)
{
return 7;
}
}
void Show_Sequence() */ This function will show the sequence.
It needs some tinkering with, as it shows all the whole pattern off the bat.
I'm looking for a way that it'll only show one pattern at a time and then
increases the pattern by 1 if the user presses the corresponding switch.*/
{
int pattern[10]; //Pattern length will go up to 10
int i = 0;
srand(time(NULL));
for (i = 0; i < 10; i++)
{
pattern[i] = Generate_Random_Sequence(rand()%4);
}
for (i = 0; i < 10; i++)
{
digitalWrite(pattern[i], HIGH);
delay(500);
digitalWrite(pattern[i], LOW);
delay(500);
}
return;
}
void Read_Sequence() */ The function that will allow the user to press the switches.
If they hit the corresponding LED, Show_Sequence() will show the next pattern.*/
{
}
int main (void)
{
wiringPiSetup(); // Sets the correct pinmode.
pinMode(LED_RED, OUTPUT);
pinMode(SWITCH_RED, INPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(SWITCH_GREEN, INPUT);
pinMode(LED_BLUE, OUTPUT);
pinMode(SWITCH_BLUE, INPUT);
pinMode(LED_YELLOW, OUTPUT);
pinMode(SWITCH_YELLOW, INPUT);
int leds[4] = {3, 2, 0, 7};
int buttons[4] = {25, 24, 23, 22};
int gamestate = 0; // Gamestate is initially zero.
Setup_Game();
All_Blink();
if (gamestate = 1) //If the gamestate = 1, starts the game.
{
printf("\nGame starting!\n");
Show_Sequence();
Read_Sequence();
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire