// Simon Says with joystick
// By: Kenny Golding
const int buzzPin = 6;
const int redPin = 8;
const int greenPin = 9;
const int yelwPin = 10;
const int bluePin = 11;
const int buttonPin = 7;
const int xPin = A4;
const int yPin = A5;
int xPos = 0;
int yPos = 0;
int rounds = 0;
int buttonState = 0;
int sequence[100];
boolean playGame = true;
void setup() {
pinMode(buzzPin, OUTPUT),(redPin, OUTPUT), (greenPin, OUTPUT), (yelwPin, OUTPUT), (bluePin, OUTPUT);
pinMode(buttonPin, INPUT);
randomSeed(analogRead(0));
}
void loop() {
buttonState = digitalRead(buttonPin);
if((playGame == false)&&(buttonState == HIGH)){
playGame = true;
rounds = 0;
}
while(playGame){
delay(1000);
sequence[rounds] = random(0, 4);
for(int i=0; i<=rounds; i++){
lightColor(sequence[i]);
}
for(int i=0; i<=rounds; i++){
int input = readInput();
while(input == 5){
input = readInput();
}
if (input == sequence[i]){
lightColor(input);
}else{
lightColor(input);
delay(200);
for(int i=0; i<=rounds; i++){
lightUpAll();
}
playGame = false;
break;
}
}
rounds++;
if(rounds == 25){ //Change this number to determine how many rounds to win.
for(int i=0; i<5; i++){
for(int j=0; j<4; j++){
digitalWrite(buzzPin, HIGH);
digitalWrite(j+8, HIGH);
delay(100);
if(j >= 2){
digitalWrite(buzzPin, LOW);
}
}
for(int j=0; j<4; j++){
digitalWrite(buzzPin, HIGH);
digitalWrite(j+8, LOW);
delay(100);
if(j >= 2){
digitalWrite(buzzPin, LOW);
}
}
}
playGame = false;
break;
}
}
}
int readInput(){
xPos = analogRead(xPin);
yPos = analogRead(yPin);
if(yPos <= 123){
return 0;
}else if(yPos >= 900){
return 2;
}else if(xPos <= 123){
return 3;
}else if(xPos >= 900){
return 1;
}else{
return 5;
}
}
void lightColor(int color) {
if(color == 0){
lightUp(8);
}else if(color == 1){
lightUp(9);
}else if(color == 2){
lightUp(10);
}else {
lightUp(11);
}
}
void lightUp(int pin){
digitalWrite(pin, HIGH);
digitalWrite(buzzPin, HIGH);
delay(200);
digitalWrite(pin, LOW);
digitalWrite(buzzPin, LOW);
delay(500);
}
void lightUpAll(){
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, HIGH);
digitalWrite(yelwPin, HIGH);
digitalWrite(bluePin, HIGH);
digitalWrite(buzzPin, HIGH);
delay(200);
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(yelwPin, LOW);
digitalWrite(bluePin, LOW);
digitalWrite(buzzPin, LOW);
delay(500);
}
Comments