User:Fabien Labeyrie/Jacques a dit: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
 
Line 1: Line 1:
__NOTOC__
__NOTOC__
__NOEDITSECTION__
__NOEDITSECTION__
==<span style="color:#00FF00">Jacques a dit</span>==
 
<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.
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">


<br />
==How does it work ?==
====Technologies involved====
====Technologies involved====
[http://en.wikipedia.org/wiki/Arduino Arduino]
[http://en.wikipedia.org/wiki/Arduino Arduino]


====Steps====
====Steps====
#We need a board with a push button and a speaker.
<div style="margin-left:-55px; margin-top:10px;">
#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.
#&nbsp;&nbsp;&nbsp;&nbsp;We need a board with a push button and a speaker.
#&nbsp;&nbsp;&nbsp;&nbsp;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 />


<br />
==<div style="margin-top:30px">Source code</div>==
==Source code==
<hr style="height:5px; margin-top:-15px; background-color:#FFF">


<source lang="C">
<source lang="C">
Line 82: Line 92:


</source>
</source>
</div>

Latest revision as of 23: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

Arduino

Steps

  1.     We need a board with a push button and a speaker.
  2.     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++;  
    }  
}