// Pin del sensore infrarosso #define SENSORE A0 // Numero tacche sulla ruota #define TACCHE_PER_GIRO 12 // Soglia bianco / nero. Solitamente bianco ~200 e nero ~900 #define SOGLIA 500 // TRUE = nero, FALSE = bianco bool old_read; bool now_read; byte tacche = 0; int giri = 0; int lettura; void setup() { Serial.begin(9600); old_read = true; } void loop() { lettura = analogRead(SENSORE); if ( lettura > SOGLIA ) { now_read = true; //Serial.println("NERO"); } else { now_read = false; //Serial.println("BIANCO"); } // Se cambia colore, vuol dire che la ruota ha ruotato di una tacca if ( now_read != old_read ) { old_read = now_read; tacche++; if( tacche > TACCHE_PER_GIRO ) { giri++; tacche = 0; Serial.println(giri); } } // AZZERA I GIRI if( Serial.available() && Serial.read() == 'Z') { tacche = 0; giri = 0; } } /* ************** INIZIO CALIBRAZIONE **************** Serial.println("OK per calibrare il bianco"); while (1) { if ( Serial.available() && Serial.read() == 'K' ) break; } for ( byte i = 0; i < NUM_CAMPIONI; i++) { soglia_bianca += analogRead(SENSORE); delay(2); } soglia_bianca /= NUM_CAMPIONI; Serial.println("Soglia bianca calibrata!"); Serial.print("Soglia = "); Serial.println(soglia_bianca); Serial.println("OK per calibrare il nero"); while (1) { if ( Serial.available() && Serial.read() == 'K' ) break; } for ( byte i = 0; i < NUM_CAMPIONI; i++) { soglia_nera += analogRead(SENSORE); delay(2); } soglia_nera /= NUM_CAMPIONI; Serial.println("Soglia nera calibrata!"); Serial.print("Soglia = "); Serial.println(soglia_nera); soglia = (soglia_nera + soglia_bianca) /2; *************** FINE CALIBRAZIONE **************** */