User:Fabien Labeyrie/Jacques a dit: Difference between revisions
No edit summary |
No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
__NOEDITSECTION__ | __NOEDITSECTION__ | ||
==<span style="color:# | |||
The idea is to work with inputs and outputs of Arduino to make a Simon says kind of game. The code is | <div style="width: 600px; font-family:Arial"> | ||
==<span style="color:#0B0080">Jacques a dit</span>== | |||
<hr style="height:5px; margin-top:-15px; background-color:#FFF"> | |||
<br /> | |||
The idea is to work with inputs and outputs of Arduino to make a Simon says kind of game. | |||
<br /> | |||
==<div style="margin-top:30px">How does it work ?</div>== | |||
<hr style="height:5px; margin-top:-15px; background-color:#FFF"> | |||
====Technologies involved==== | |||
[http://en.wikipedia.org/wiki/Arduino Arduino] | |||
====Steps==== | |||
<div style="margin-left:-55px; margin-top:10px;"> | |||
# We need a board with a push button and a speaker. | |||
# The code uses two ''while'' loops, constantly checking if the button is pressed, for how long, and launching a timeout during the waiting loop, in order to play the sequence after 5 seconds of wait. | |||
</div> | |||
<br /> | |||
==<div style="margin-top:30px">Source code</div>== | |||
<hr style="height:5px; margin-top:-15px; background-color:#FFF"> | |||
<source lang="C"> | <source lang="C"> | ||
const int buttonPin = 7; | const int buttonPin = 7; | ||
int buttonState = 0; | int buttonState = 0; | ||
int | int value1; | ||
int | int value2; | ||
int | int interval; | ||
int | int pattern[100]; | ||
int i = 0; | int i = 0; | ||
int j = 0; | int j = 0; | ||
Line 18: | Line 39: | ||
void setup() { | void setup() { | ||
// 9600 is the frequency that will be used by the monitor | |||
Serial.begin(9600); | Serial.begin(9600); | ||
pinMode(buttonPin, INPUT); | pinMode(buttonPin, INPUT); | ||
} | } | ||
Line 25: | Line 46: | ||
void loop(){ | void loop(){ | ||
while (1) { | |||
while (1) { | |||
// State : WAIT FOR PRESS | |||
wfp_start = millis(); | |||
while (digitalRead(buttonPin) == LOW && (millis() - wfp_start < 5000 )) { | |||
// Do nothing the button isn't pressed | |||
} | |||
// Act as a timeout. Above 5 sec, we exit the WAIT loop and PLAY | |||
if ((millis() - wfp_start >= 5000 )) break; | |||
// State : WAIT FOR RELEASE | |||
value1 = millis(); | |||
while (digitalRead(buttonPin) == HIGH) { | |||
// Play tone when the button is pressed | |||
tone(8, 100, 5); | |||
} | |||
value2 = millis(); | |||
interval = value2 - value1; | |||
pattern[i]= interval; | |||
Serial.println(pattern[i]); | |||
// This will be used to simplify the values stored in the array | |||
if (pattern[i] < 100) pattern[i] = 100; | |||
else if (pattern[i] > 99 && pattern [i] < 200) pattern[i] = 200; | |||
else if (pattern[i] > 199 && pattern [i] < 300) pattern[i] = 300; | |||
else pattern[i] = 400; | |||
Serial.println(" pattern : "); | |||
Serial.println(pattern[i]); | |||
i++; | |||
} | |||
// | // State : PLAY | ||
while (j < i) { | while (j < i) { | ||
tone(8, 100, | tone(8, 100, pattern[j]); | ||
Serial.println("Pattern plays"); | Serial.println("Pattern plays"); | ||
j++; | j++; | ||
} | } | ||
} | } | ||
</source> | </source> | ||
</div> |
Latest revision as of 22:13, 6 April 2011
Jacques a dit
The idea is to work with inputs and outputs of Arduino to make a Simon says kind of game.
How does it work ?
Technologies involved
Steps
- We need a board with a push button and a speaker.
- The code uses two while loops, constantly checking if the button is pressed, for how long, and launching a timeout during the waiting loop, in order to play the sequence after 5 seconds of wait.
Source code
const int buttonPin = 7;
int buttonState = 0;
int value1;
int value2;
int interval;
int pattern[100];
int i = 0;
int j = 0;
int wfp_start;
void setup() {
// 9600 is the frequency that will be used by the monitor
Serial.begin(9600);
pinMode(buttonPin, INPUT);
}
void loop(){
while (1) {
// State : WAIT FOR PRESS
wfp_start = millis();
while (digitalRead(buttonPin) == LOW && (millis() - wfp_start < 5000 )) {
// Do nothing the button isn't pressed
}
// Act as a timeout. Above 5 sec, we exit the WAIT loop and PLAY
if ((millis() - wfp_start >= 5000 )) break;
// State : WAIT FOR RELEASE
value1 = millis();
while (digitalRead(buttonPin) == HIGH) {
// Play tone when the button is pressed
tone(8, 100, 5);
}
value2 = millis();
interval = value2 - value1;
pattern[i]= interval;
Serial.println(pattern[i]);
// This will be used to simplify the values stored in the array
if (pattern[i] < 100) pattern[i] = 100;
else if (pattern[i] > 99 && pattern [i] < 200) pattern[i] = 200;
else if (pattern[i] > 199 && pattern [i] < 300) pattern[i] = 300;
else pattern[i] = 400;
Serial.println(" pattern : ");
Serial.println(pattern[i]);
i++;
}
// State : PLAY
while (j < i) {
tone(8, 100, pattern[j]);
Serial.println("Pattern plays");
j++;
}
}