Devices & Components
Arduino Uno Rev3
Male/Female Jumper Wires
Resistor 220 ohm
Breadboard (generic)
Jumper wires (generic)
5 mm LED: Green
LED, Blue
Buzzer
Analog joystick (Generic)
5 mm LED: Red
5 mm LED: Yellow
Pushbutton switch 12mm
Project description
Code
Simon_Says_Game
c_cpp
1// Simon Says with joystick 2// By: Kenny Golding 3 4const int buzzPin = 6; 5const int redPin = 8; 6const int greenPin = 9; 7const int yelwPin = 10; 8const int bluePin = 11; 9const int buttonPin = 7; 10const int xPin = A4; 11const int yPin = A5; 12 13int xPos = 0; 14int yPos = 0; 15int rounds = 0; 16int buttonState = 0; 17int sequence[100]; 18 19boolean playGame = true; 20 21void setup() { 22 pinMode(buzzPin, OUTPUT),(redPin, OUTPUT), (greenPin, OUTPUT), (yelwPin, OUTPUT), (bluePin, OUTPUT); 23 pinMode(buttonPin, INPUT); 24 randomSeed(analogRead(0)); 25} 26 27void loop() { 28 buttonState = digitalRead(buttonPin); 29 30 if((playGame == false)&&(buttonState == HIGH)){ 31 playGame = true; 32 rounds = 0; 33 } 34 35 while(playGame){ 36 delay(1000); 37 sequence[rounds] = random(0, 4); 38 39 for(int i=0; i<=rounds; i++){ 40 lightColor(sequence[i]); 41 } 42 43 for(int i=0; i<=rounds; i++){ 44 int input = readInput(); 45 46 while(input == 5){ 47 input = readInput(); 48 } 49 50 if (input == sequence[i]){ 51 lightColor(input); 52 }else{ 53 lightColor(input); 54 delay(200); 55 for(int i=0; i<=rounds; i++){ 56 lightUpAll(); 57 } 58 59 playGame = false; 60 break; 61 } 62 } 63 64 65 rounds++; 66 67 if(rounds == 25){ //Change this number to determine how many rounds to win. 68 69 for(int i=0; i<5; i++){ 70 for(int j=0; j<4; j++){ 71 digitalWrite(buzzPin, HIGH); 72 digitalWrite(j+8, HIGH); 73 delay(100); 74 75 if(j >= 2){ 76 digitalWrite(buzzPin, LOW); 77 } 78 } 79 for(int j=0; j<4; j++){ 80 digitalWrite(buzzPin, HIGH); 81 digitalWrite(j+8, LOW); 82 delay(100); 83 84 if(j >= 2){ 85 digitalWrite(buzzPin, LOW); 86 } 87 } 88 } 89 playGame = false; 90 break; 91 } 92 } 93} 94 95int readInput(){ 96 xPos = analogRead(xPin); 97 yPos = analogRead(yPin); 98 99 if(yPos <= 123){ 100 return 0; 101 }else if(yPos >= 900){ 102 return 2; 103 }else if(xPos <= 123){ 104 return 3; 105 }else if(xPos >= 900){ 106 return 1; 107 }else{ 108 return 5; 109 } 110 111} 112 113void lightColor(int color) { 114 if(color == 0){ 115 lightUp(8); 116 }else if(color == 1){ 117 lightUp(9); 118 }else if(color == 2){ 119 lightUp(10); 120 }else { 121 lightUp(11); 122 } 123} 124 125void lightUp(int pin){ 126 digitalWrite(pin, HIGH); 127 digitalWrite(buzzPin, HIGH); 128 delay(200); 129 digitalWrite(pin, LOW); 130 digitalWrite(buzzPin, LOW); 131 delay(500); 132} 133 134void lightUpAll(){ 135 digitalWrite(redPin, HIGH); 136 digitalWrite(greenPin, HIGH); 137 digitalWrite(yelwPin, HIGH); 138 digitalWrite(bluePin, HIGH); 139 digitalWrite(buzzPin, HIGH); 140 delay(200); 141 digitalWrite(redPin, LOW); 142 digitalWrite(greenPin, LOW); 143 digitalWrite(yelwPin, LOW); 144 digitalWrite(bluePin, LOW); 145 digitalWrite(buzzPin, LOW); 146 delay(500); 147} 148
Downloadable files
Schematics
IMPORTANT: Connect the white wires to the joystick module. The x pin on the module should be connected to pin A4 on the uno and y pin should be connected to pin A5.
Schematics

Comments
Only logged in users can leave comments