User:Fabien Labeyrie/Jacques a dit

From XPUB & Lens-Based wiki


Jacques a dit

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 cleaned and commented soon.

const int buttonPin = 7;
const int ledPin = 13;

int buttonState = 0;
int valeur1;
int valeur2;
int intervalle;
int motif[100];
int i = 0;
int j = 0;
int wfp_start;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT); 
}

void loop(){
 
// Mettre dans une grande loop WHILE 1
while (1) {
  
  // Wait for press
  wfp_start = millis();
  while (digitalRead(buttonPin) == LOW && (millis() - wfp_start < 5000 )) {
    digitalWrite(ledPin, LOW);
  }

  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);
    
  }
  valeur2 = millis();
  intervalle = valeur2 - valeur1;  
  motif[i]= intervalle;
  Serial.println(motif[i]);
    
  if (motif[i] < 100) motif[i] = 100;
  else if (motif[i] > 99 && motif [i] < 200) motif[i] = 200;
  else if (motif[i] > 199 && motif [i] < 300) motif[i] = 300;
  else motif[i] = 400;

    
  Serial.println(" motif : ");
  Serial.println(motif[i]);  
  i++;
  
}

// play

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