Ive made a simple RMP meter using a hall trip counter. It outouts to a PWM line then feeds a RC filter and bar graph.
My problem is when the trip count falls to zero, the PWM output still reflects the last calculated trip.
How do i make it set to zero when the trip is zero?
int hall_pin = 2;
// set number of hall trips for RPM reading (higher improves accuracy)
float hall_thresh = 10.0;
const int outPin1 = 6;
//TCCR4B = TCCR4B & B11111000 | B00000001; // set timer 4 prescaler to 1 for PWM frequency of 31372.5 Hz
void setup() {
Serial.begin(9600);
pinMode(hall_pin, INPUT);
pinMode (outPin1,OUTPUT);
}
void loop() {
float hall_count = 1.0;
float start = micros();
bool on_state = false;
// counting number of times the hall sensor is tripped
// but without double counting during the same trip
while(true){//keeps running while true but what makes it true?
if (digitalRead(hall_pin)==0){//every time hall is switched to 0 run if
if (on_state==false){//already set to false so make true
on_state = true;
hall_count+=1.0;//count = (hall_count + 1.0) + new value of hall_count
}
} else{
on_state = false;
}
if (hall_count>=hall_thresh){
break;
}
}
// print information about Time and RPM
float end_time = micros();
float time_passed = ((end_time - start)/1000000.0); //micros now - start micros/1second
Serial.print("Time Passed: ");// in seconds
Serial.print(time_passed);
Serial.println("s");
int rpm_val = (hall_count/time_passed)*60.0;
Serial.println(rpm_val);
delay(1); // delay in between reads for stability
analogWrite (outPin1, (0.0255 * rpm_val));//255/10000 max scale * rpm. USE THIS
delay(100); //allows all serial sent to be received together
}