Bonjour,
J'essaye d'insérer dans mon code un chronomètre mais rien n'y fait...
Le programme est simple :
On lance le programme, et celui ci attend que la boucle soit lancée.
On appuie sur le bouton, la boucle démarre avec, allumage de toutes les cibles, puis le logiciel attend de détecter un changement de valeur sur les capteurs piezo.
S'il y a détection, la cible s’éteint, et ainsi de suite pour toutes les cibles.
Quand toutes les cibles sont éteint, la partie est terminée.
Mon chrono doit démarrer après un temps "x" une fois que le bouton de départ soit déclenché.
Et il doit s'arrêter à l'extinction de la dernière cible.
Vous trouverez dedans des bouts de code d'un topic de 2011, car oui j'ai cherché ^^
Ne faites pas attention à la plupart des serial.Print(), ils sont là pour tester mon programme sans avoir fait tout le montage.
J'attends les capteurs piezo ![]()
Merci d'avance pour votre aide !
const int buttonPin = 2;
// these constants won't change:
const int ledPin0 = 10; // led connected to digital pin 10
const int ledPin1 = 11; // led connected to digital pin 11
const int ledPin2 = 12; // led connected to digital pin 12
const int ledPin3 = 13; // led connected to digital pin 13
const int knockSensor0 = A0; // the piezo is connected to analog pin 0
const int knockSensor1 = A1; // the piezo is connected to analog pin 1
const int knockSensor2 = A2; // the piezo is connected to analog pin 2
const int knockSensor3 = A3; // the piezo is connected to analog pin 3
const int threshold0 = 100; // threshold value to decide when the detected sound is a knock or not 0
const int threshold1 = 100; // threshold value to decide when the detected sound is a knock or not 1
const int threshold2 = 100; // threshold value to decide when the detected sound is a knock or not 2
const int threshold3 = 100; // threshold value to decide when the detected sound is a knock or not 3
// these variables will change:
int sensorReading0 = 0; // variable to store the value read from the sensor pin 0
int sensorReading1 = 0; // variable to store the value read from the sensor pin 1
int sensorReading2 = 0; // variable to store the value read from the sensor pin 2
int sensorReading3 = 0; // variable to store the value read from the sensor pin 3
int ledState0 = LOW; // variable used to store the last LED status, to toggle the light 0
int ledState1 = LOW; // variable used to store the last LED status, to toggle the light 1
int ledState2 = LOW; // variable used to store the last LED status, to toggle the light 2
int ledState3 = LOW; // variable used to store the last LED status, to toggle the light 3
int buttonState = 0; // variable for reading the pushbutton status
unsigned long previousMillis1 = 0; // variable to store last time LED was updated
unsigned long startTime = 0 ; // start time for stop watch
unsigned long elapsedTime = 0 ; // elapsed time for stop watch
unsigned long centitot1 = 0; // centieme to display
unsigned long secotot1 =0; // seconds to display
unsigned long minutot1 =0;
int addr = 0; // adress for memory storage
int val; // for eprom recording of intermediate chrono
int nbrenr = 0; //number of recording in eprom
// int array example
//int l = lap[100];
void setup() {
pinMode(ledPin0, OUTPUT); // declare the ledPin0 as as OUTPUT
pinMode(ledPin1, OUTPUT); // declare the ledPin1 as as OUTPUT
pinMode(ledPin2, OUTPUT); // declare the ledPin2 as as OUTPUT
pinMode(ledPin3, OUTPUT); // declare the ledPin3 as as OUTPUT
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
Serial.begin(9600); // use the serial port
Serial.println(" Appuyer sur le bouton pour demarrer ");
}
void loop() {
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
Serial.println(" Debut dans 5 secondes ");
delay(1000);
Serial.println(" Debut dans 4 secondes ");
delay(1000);
Serial.println(" Debut dans 3 secondes ");
delay(1000);
Serial.println(" Debut dans 2 secondes ");
delay(1000);
Serial.println(" Debut dans 1 seconde ");
delay(1000);
Serial.println(" Go !!! ");
digitalWrite(ledPin0, HIGH);
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
if (previousMillis1 ==0) { // to correct the delay comming from Processor and start button 0 push
previousMillis1 = startTime;
}
while( digitalRead(ledPin0) != LOW || digitalRead(ledPin1) != LOW || digitalRead(ledPin2) != LOW || digitalRead(ledPin3) != LOW){ // tant que l'expression est vraie // instructions ? effectuer
// read the sensor and store it in the variable sensorReading:
sensorReading0 = analogRead(knockSensor0);
// if the sensor reading is greater than the threshold:
if (sensorReading0 >= threshold0) {
// toggle the status of the ledPin:
ledState0 = !ledState0;
// update the LED pin itself:
digitalWrite(ledPin0, LOW);
// send the string "Knock!" back to the computer, followed by newline
Serial.println("Cible 1 Ok"); }
delay(10); // delay to avoid overloading the serial port buffer
// read the sensor and store it in the variable sensorReading:
sensorReading1 = analogRead(knockSensor1);
// if the sensor reading is greater than the threshold:
if (sensorReading1 >= threshold1) {
// toggle the status of the ledPin:
ledState1 = !ledState1;
// update the LED pin itself:
digitalWrite(ledPin1, LOW);
// send the string "Knock!" back to the computer, followed by newline
Serial.println("Cible 2 Ok"); }
delay(10); // delay to avoid overloading the serial port buffer
// read the sensor and store it in the variable sensorReading:
sensorReading2 = analogRead(knockSensor2);
// if the sensor reading is greater than the threshold:
if (sensorReading2 >= threshold2) {
// toggle the status of the ledPin:
ledState2 = !ledState2;
// update the LED pin itself:
digitalWrite(ledPin2, LOW);
// send the string "Knock!" back to the computer, followed by newline
Serial.println("Cible 3 Ok"); }
delay(10); // delay to avoid overloading the serial port buffer
// read the sensor and store it in the variable sensorReading:
sensorReading3 = analogRead(knockSensor3);
// if the sensor reading is greater than the threshold:
if (sensorReading3 >= threshold3) {
// toggle the status of the ledPin:
ledState3 = !ledState3;
// update the LED pin itself:
digitalWrite(ledPin3, LOW);
// send the string "Knock!" back to the computer, followed by newline
Serial.println("Cible 3 Ok"); }
delay(10); // delay to avoid overloading the serial port buffer
if ( digitalRead(ledPin0) == LOW && digitalRead(ledPin1) == LOW && digitalRead(ledPin2) == LOW && digitalRead(ledPin3) == LOW){ // tant que l'expression est vraie // instructions ? effectuer
elapsedTime = (millis() - (previousMillis1)); // store elapsed time starttime
previousMillis1 = millis();
//
centitot1 = (elapsedTime%1000)/10 ; //centieme to display
secotot1 = (elapsedTime/1000)%60 ; // second to display
minutot1 = (elapsedTime/1000)/60 ; // minute to display
if (minutot1 <10) { //If less then 10 minutes display need ajustement
Serial.print("0"); // Add a zero for clean presentation
}
Serial.print(minutot1); // Display minutes
Serial.print(":");
if (secotot1 <10) { //If less then 10 seconds display need ajustement
Serial.print("0"); // Add a zero for clean presentation
}
Serial.print(secotot1); // Display Second
Serial.print(".");
if (centitot1<10) { // If less then 10 hundreds display need ajustement
Serial.print("0"); // Add a zero for clean presentation
}
Serial.print(centitot1);//Display Hundred
Serial.println("Bravo !");
Serial.println("Partie terminee !");
Serial.println("Nouvelle partie dans 5 secondes");
delay(5000);
Serial.println("Appuyer sur le bouton pour demarrer");
break;
}
}
}
}