void setup() { pinMode(8, INPUT); cli(); TCCR2A = 0; TCCR2B = 0; TCNT2 = 0; OCR2A = 99; TCCR2A |= (1 << WGM21); TCCR2B |= (1 << CS21); TIMSK2 |= (1 << OCIE2A); sei(); } void loop() { } ISR(TIMER2_COMPA_vect) { val = analogRead(analogPin); x++; i++; y++; if(y==8000) { maxi = 0; mini = 999; maxig = 0; minig = 0; y = 0; } for(x=0; x<6; x++) { value_array[x] = value_array[x+1]; } value_array[6] = val; avg1 = (value_array[0]+value_array[1]+value_array[2]+value_array[3]+value_array[4]+value_array[5])/6; avg2 = (value_array[1]+value_array[2]+value_array[3]+value_array[4]+value_array[5]+value_array[6])/6; if((t_holdoff > 0) && (holdoff_enable == 1)) { t_holdoff--; } if((T_Periode_HIGH_enable == 1) && (T_Periode_HIGH < 2500)) { T_Periode_HIGH++; } if((T_Periode_LOW_enable == 1) && (T_Periode_LOW < 2500)) { T_Periode_LOW++; } switch( state ) { case 0: if(avg1 < avg2) { // signal increases state = 1; // go to first state } if(avg2 < avg1) { // signal falls state = 3; // go to third state } case 1: // signal increases if (val > maxi) { maxi = val; } if (avg2 < maxi) { maxig = 1; holdoff_enable = 1; if(t_holdoff == 0) { holdoff_enable = 0; t_holdoff = 100; state = 2; } } case 2: // signal falls if (val < mini) { mini = val; } if (val > avg2) { minig = 1; holdoff_enable = 1; if(t_holdoff == 0) { holdoff_enable = 0; t_holdoff = 100; state = 5; } } case 3: // signal falls if (val < mini) { mini = val; } if (avg2 > mini) { minig = 1; holdoff_enable = 1; if(t_holdoff == 0) { holdoff_enable = 0; t_holdoff = 100; state = 4; } } case 4: // signal increases if (val > maxi) { maxi = val; } if (val < avg2) { maxig = 1; holdoff_enable = 1; if(t_holdoff == 0) { holdoff_enable = 0; t_holdoff = 100; state = 5; } } case 5: diff = maxi - mini; amplitude_on = diff * 0.5; on_treshold = mini + amplitude_on; tolerance = diff * 0.2; tolerance_HIGH = on_treshold + tolerance; tolerance_LOW = on_treshold - tolerance; a = diff * 0.08; b = maxi - a; c = mini + a; holdoff_enable = 1; if(t_holdoff == 0) { holdoff_enable = 0; t_holdoff = 100; state = 6; } case 6: if(val > tolerance_HIGH) { digitalWrite(8, HIGH); T_Periode_LOW_enable = 0; if((T_Periode_LOW_enable == 0) && (T_Periode_HIGH_enable == 0)){ T_Periode_ms = ((T_Periode_HIGH * 0.25) + (T_Periode_LOW * 0.25)); rpm = (60000/T_Periode_ms); T_Periode_HIGH = 0; } T_Periode_HIGH_enable = 1; state = 7; } case 7: if(val < tolerance_LOW) { digitalWrite(8, LOW); T_Periode_HIGH_enable = 0; if((T_Periode_HIGH_enable == 0) && (T_Periode_LOW_enable == 0)) { T_Periode_ms = ((T_Periode_HIGH * 0.25) + (T_Periode_LOW * 0.25)); rpm = (60000/T_Periode_ms); T_Periode_LOW = 0; } T_Periode_LOW_enable = 1; state = 0; } } }