Datman
12
#include <LiquidCrystal.h>
LiquidCrystal lcd(7,8,9,10,11,12); // RS,EN,D4,D5,D6,D7
uint16_t attesa;
uint32_t t_attesa;
uint32_t t_start;
uint32_t t_stop;
enum stati {ST_ATTENZIONE, ST_ATTESA, ST_START, ST_STOP};
enum stati stato = ST_ATTENZIONE;
uint16_t tempo = 0;
uint16_t tempo_min = 10000;
uint16_t tempo_max = 0;
void setup() {
pinMode(2, INPUT_PULLUP);
pinMode(3, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
lcd.begin(16, 2);
digitalWrite(3, HIGH);
lcd.print(" PROVA RIFLESSI ");
lcd.setCursor(0,1);
lcd.print(" Premi per iniz.");
while(digitalRead(2)) {} // Questa attesa produce un millis() iniziale sempre diverso.
lcd.clear();
while(!digitalRead(2)) {} // Attende il rilascio del pulsante.
digitalWrite(3, LOW);
}
void loop()
{
switch(stato)
{
case ST_ATTENZIONE:
digitalWrite(3, LOW);
lcd.setCursor(0,0); lcd.print(" ATTENZIONE... ");
randomSeed(analogRead(A0)+millis());
attesa = random(2500, 7001); // ATTESA da 2,5 a 7 secondi.
t_attesa = millis();
stato = ST_ATTESA;
break;
case ST_ATTESA: // secondo Maurotec
if(millis()-t_attesa >= attesa)
{
stato = ST_START;
lcd.setCursor(0,0); lcd.print("**** PREMI! ****");
// lcd.setCursor(0,1); lcd.print("****************");
digitalWrite(3, HIGH);
t_start = millis();
}
else if (!digitalRead(2)) // Aggiunto da Maurotec. Grazie!
{
lcd.setCursor(0,0); lcd.print("*** NON FARE ***");
lcd.setCursor(0,1); lcd.print("*** IL FURBO ***");
stato = ST_ATTENZIONE;
delay(2000);
lcd.setCursor(0,1); lcd.print(" ");
}
break;
case ST_START:
if(!digitalRead(2))
{
t_stop = millis();
lcd.clear();
lcd.setCursor(5,0); lcd.print(tempo = t_stop-t_start); lcd.print(" ms");
if(tempo<tempo_min) tempo_min=tempo;
if(tempo>tempo_max) tempo_max=tempo;
lcd.setCursor(0,1); lcd.print("min="); lcd.print(tempo_min);
lcd.setCursor(8,1); lcd.print("max=");
if(tempo_max<10000) {lcd.print(tempo_max);}
else {lcd.setCursor(11,1); lcd.print(">=10s");}
delay(2000);
stato = ST_STOP;
}
break;
case ST_STOP:
if(digitalRead(2)) stato = ST_ATTENZIONE;
break;
}
}