int hallSensorPin = 2; int low_state = 400; int high_state = 500; int hall_thresh = 100.0; int val = 0; void setup() { Serial.begin(9600); pinMode(hallSensorPin, INPUT); } void loop() { int hall_count = 1; //float start = micros(); bool state = false; while(true) { val = analogRead(hallSensorPin); if (low_state < val && val < high_state) { if (state == false){ state = true; hall_count+=1; Serial.write(hall_count); delay(50); } } else { state = false; } if (hall_count>=hall_thresh) { break; } } delay(1); }