09 Feb 2010

DIY Simon with Arduino w/ Source and Diagram

No Comments Examples, Physical Computing, project


So i finally had time to tinker around with this Simon Sez project again. Turns out there wasn’t an issue after all with my circuits, it was an issue in my code. I also did some house keeping on the code, and created a nice little diagram using Fritzing, an awesome app that was recommend to our class by Tom Igoe.

I tried to keep both programming and circuitry as slim as I could, and I think that worked out pretty well. Not sure if i will add anything else to this, but if anyone has any ideas, asides from my coworker’s recent tweet, then Iet me know.

This source code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
 /*
   Simon Sez
 
 
  created Feb 10 2010
  by Alex Britez
 
  http://blog.unthinkmedia.com/?p=475
 
  */
 
#include <Tone.h>
 
Tone audioFeedback;
 
//DEFINE MOTOR
int transistorPin = 12; 
 
int speakerPin = 6; 
 
//DEFINE LEDS
int ledWhite = 11;
int ledGreen = 10;
int ledRed = 9;
int ledYellow = 8;
 
char leds[] = {
  ledWhite,ledGreen,ledRed,ledYellow};
 
//DEFINE SWITCHES 
int switchWhite = 5;
int switchGreen = 4;
int switchRed = 3;
int switchYellow = 2;
 
//AUDIO
int notes[] = {
  NOTE_D4,NOTE_E4,NOTE_F4,NOTE_G4};
//this plays if you win
int winNotes[] = {
  NOTE_D4, NOTE_E4,NOTE_C4,NOTE_C3,NOTE_G3 };
int durations[] = {
  500, 500, 500, 500, 1000};
 
 
 
//
int playbackCount = 0;//autoplay selection count
int selectionCount= 0;//player selection count
 
int currentLevel = 0;//current game level
 
boolean watchMode = true; //Simon's turn, false means it is the players turn 
 
//store which switch is currently active?
int pressAry[] = {
  0,0,0,0};
 
//Storing the squence that gets randomly generated, Num in brackets states how long sequence is [#]
int sequenceAry[7];
int sequenceAryLegnth = sizeof(sequenceAry) / sizeof(int);
 
void setup()
{
  pinMode(ledWhite, OUTPUT);
  pinMode(ledGreen, OUTPUT);
  pinMode(ledRed, OUTPUT);     
  pinMode(ledYellow, OUTPUT);
  pinMode(switchWhite, INPUT);
  pinMode(switchGreen, INPUT);
  pinMode(switchRed, INPUT);     
  pinMode(switchYellow, INPUT);
  pinMode(transistorPin, OUTPUT);
 
  randomSeed(analogRead(0));//Add this so sequence is truely random
  audioFeedback.begin(speakerPin);
  createRandomSequence();
}
 
void loop()
{
  /************
   *  Check to see if simon/player mode
   *************/
  if(watchMode == true){
   /************
   *  SIMON SEZ: leds automatically blink in random sequence
   *************/
    //have you won?
    if(playbackCount <= sequenceAryLegnth){
      //has simon finshed showing current sequence?
      if(playbackCount < currentLevel){
        //Blink proper LED from leds array
        int activeLED_index = sequenceAry[playbackCount];
        digitalWrite(leds[activeLED_index], HIGH);
        //Play corrisponding note
        audioFeedback.play(notes[activeLED_index]);
        delay(200);
        digitalWrite(leds[activeLED_index], LOW);
        audioFeedback.stop();
        delay(200);
        playbackCount++;
      }
      else{
        //simon is done, now it is players turn
        watchMode = false;
      }
    }
    else{
       /************
       *  PLAYER WINS!
       *************/
      playerWins();
    }
  }
  else{
 
    //Read States of all Switches
    int whiteState = digitalRead(switchWhite);
    int greenState = digitalRead(switchGreen);
    int redState = digitalRead(switchRed);
    int yellowState = digitalRead(switchYellow);
 
    int statesAry[] = {
      whiteState,greenState,redState,yellowState};
 
    //loop across all 4 switch states
    for(int x=0; x<4; x++){
      int switchState = statesAry[x];
      if(switchState == 1){
        audioFeedback.play(notes[x]);
        digitalWrite(leds[x], HIGH);
        delay(200);
        audioFeedback.stop();
        digitalWrite(leds[x], LOW);
 
        if(pressAry[x]==0){
          if(sequenceAry[selectionCount]==x){
            selectionCount++;
            pressAry[x] = 1;
          }
          else{
            resetGame();
          }
 
        }
 
      }
      else{
        pressAry[x] = 0;
        // turn LED off:
        digitalWrite(leds[x], LOW); 
      }
 
      //Check to see if we could incrament level
      if(selectionCount == currentLevel){
        advanceLevel();
      }
    }
 
  }
 
}
 
 
void createRandomSequence(){
   for(int x=0; x<sequenceAryLegnth; x++){
      sequenceAry[x] = random(0, 4);
   } 
 
}
 
/************
 *  Play winning melody
 *************/
void playerWins(){
  for (int thisNote = 0; thisNote < 5; thisNote ++) {
    // play the next note:
    audioFeedback.play(winNotes[thisNote]);
    // hold the note:
    delay(durations[thisNote]);
    // stop for the next note:
    audioFeedback.stop();
  }
  // hold before repeating:
  delay(3000);
}
 
/************
 *  Incrament player one level
 *************/
void advanceLevel(){
  resetValues();
  currentLevel++;
  delay(200);
} 
 
/************
 *  Player pressed wrong button
 *************/
void resetGame(){
  //DISPATCH HAPTIC FEEDBACK 
  digitalWrite(transistorPin, HIGH);
  delay(1000);
  digitalWrite(transistorPin, LOW);
  delay(1000);
 
  resetValues();
  currentLevel = 0;
  delay(1000);
}
 
/************
 *  Reset all values so simon knows to start from the first LED in the sequenceAry
 *************/
void resetValues(){
  selectionCount = 0;
  playbackCount = 0;
  watchMode = true;
}


Popularity: 3% [?]

Tags: , , ,
written by
I am a designer, developer & dad who interested in educational technology and using media for social goodness.
Related Posts
No Responses to “DIY Simon with Arduino w/ Source and Diagram”

Leave a Reply

Get Adobe Flash playerPlugin by wpburn.com wordpress themes