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

From XPUB & Lens-Based wiki
(Created page with "__NOTOC__ __NOEDITSECTION__ ==<span style="color:#00FF00">Jacques a dit</span>== The idea is to work with inputs and outputs of Arduino to make a Simon says kind of game. The cod...")
 
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
__NOEDITSECTION__
__NOEDITSECTION__
==<span style="color:#00FF00">Jacques a dit</span>==
 
The idea is to work with inputs and outputs of Arduino to make a Simon says kind of game. The code is got to be commented and updated soon.
<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;">
#&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 />
 
==<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;
const int ledPin = 13;


int buttonState = 0;
int buttonState = 0;
int valeur1;
int value1;
int valeur2;
int value2;
int intervalle;
int interval;
int motif[100];
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(ledPin, OUTPUT);
   pinMode(buttonPin, INPUT);  
   pinMode(buttonPin, INPUT);  
}
}
Line 25: Line 46:
void loop(){
void loop(){
   
   
// Mettre dans une grande loop WHILE 1
  while (1) {
while (1) {
   
 
    // State : WAIT FOR PRESS
  // Wait for press
    wfp_start = millis();
  wfp_start = millis();
 
  while (digitalRead(buttonPin) == LOW && (millis() - wfp_start < 5000 )) {
    while (digitalRead(buttonPin) == LOW && (millis() - wfp_start < 5000 )) {
     digitalWrite(ledPin, LOW);
      // Do nothing the button isn't pressed
  }
     }


  if ((millis() - wfp_start >= 5000 )) break;
    // Act as a timeout. Above 5 sec, we exit the WAIT loop and PLAY
 
    if ((millis() - wfp_start >= 5000 )) break;
  // Wait for release state, it's gonna be played only if we are in the timeout
  valeur1 = millis();
 
  while (digitalRead(buttonPin) == HIGH) {
    digitalWrite(ledPin, HIGH);
    tone(8, 100, 5);
      
      
  }
    // State : WAIT FOR RELEASE
  valeur2 = millis();
    value1 = millis();
  intervalle = valeur2 - valeur1; 
  motif[i]= intervalle;
  Serial.println(motif[i]);
      
      
  if (motif[i] < 100) motif[i] = 100;
    while (digitalRead(buttonPin) == HIGH) {
  else if (motif[i] > 99 && motif [i] < 200) motif[i] = 200;
      // Play tone when the button is pressed
  else if (motif[i] > 199 && motif [i] < 300) motif[i] = 300;
      tone(8, 100, 5);  
  else motif[i] = 400;
    }


    value2 = millis();
    interval = value2 - value1; 
    pattern[i]= interval;
    Serial.println(pattern[i]);
      
      
  Serial.println(" motif : ");
    // This will be used to simplify the values stored in the array   
  Serial.println(motif[i]);
    if (pattern[i] < 100) pattern[i] = 100;
  i++;
    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;


// play
    Serial.println(" pattern : ");
    Serial.println(pattern[i]); 
    i++;
   
  }


// WE HAVE TO LOOK WHY IT STOPS AND INPUT MORE OUTPUTS 
    // State : PLAY
     while (j < i) {
     while (j < i) {
       tone(8, 100, motif[j]);
       tone(8, 100, pattern[j]);
       Serial.println("Pattern plays");     
       Serial.println("Pattern plays");     
       j++;   
       j++;   
     }
     }
   
}
}


</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++;  
    }  
}