Hey folks... Trying to digest this code while creating a working tool. The following works for a normally low switch, but when I change to a hall sensor it goes out the window. I know I need to reverse the logic as the hall sensor is normally high. One questions I have in understanding the inc dec loop is the initial "if" statement. "if (pulseVal){//if reed switch is closed" where does it go from here. I always thought the if statement needed to be a question i.e. if pulseVal == 1 or == 0. I don't understand this and as such I'm having difficulty making this switch. Any help would be much appreciated.
//outputs speed of treadmill to LCD
//calculations
//wheel radius = 7.0 / 3 pulses per rev = 1.167 inches
//circumference = PI x D / 3 pulses per rev = 7.33 inches
//max speed =
//max rps =
#include <Wire.h>
#include <LiquidCrystal.h>
#include <TKLCD.h>
TKLCD_Serial lcd = TKLCD_Serial(); // when connecting to TKLCD over Serial
#define pulse A0//pin connected to read hall sensor
//storage variables
float radius = 1.167;// wheel radius in inches
int pulseVal;
long timer = 0;// time between one full rotation (in ms)
float mph = 0;
float circumference;
boolean backlight;
int maxpulseCounter = 100;//min time (in ms) of one rotation (for debouncing)
int pulseCounter;
void setup(){
pulseCounter = maxpulseCounter;
circumference = 2*3.14*radius;
pinMode(1,OUTPUT);//tx
pinMode(2,OUTPUT);//backlight switch
pinMode(pulse, INPUT);
// TIMER SETUP- the timer interrupt allows preceise timed measurements of the reed switch
//for mor info about configuration of arduino timers see http://arduino.cc/playground/Code/Timer1
cli();//stop interrupts
//set timer1 interrupt at 1kHz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;
// set timer count for 1khz increments
OCR1A = 1999;// = (1/1000) / ((1/(16*10^6))*8) - 1
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS11 bit for 8 prescaler
TCCR1B |= (1 << CS11);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei();//allow interrupts
//END TIMER SETUP
Serial.begin(9600);
}
ISR(TIMER1_COMPA_vect) {//Interrupt at freq of 1kHz to measure hall sensor
pulseVal = digitalRead(pulse);//get val of A0
if (pulseVal){//if reed switch is closed
if (pulseCounter == 0){//min time between pulses has passed
mph = (56.8*float(circumference))/float(timer);//calculate miles per hour
timer = 0;//reset timer
pulseCounter = maxpulseCounter;//reset reedCounter
}
else{
if (pulseCounter > 0){//don't let pulseCounter go negative
pulseCounter -= 1;//decrement pulseCounter
}
}
}
else{//if hall sensor is open
if (pulseCounter > 0){//don't let pulseCounter go negative
pulseCounter -= 1;//decrement pulseCounter
}
}
if (timer > 1000){
mph = 0;//if no new pulses from reed switch- tire is still, set mph to 0
}
else{
timer += 1;//increment timer
}
}
void displayMPH(){
if(int(mph) < 10){
Serial.print("Speed = ");
Serial.print(" ");
Serial.print(mph,1);
Serial.println(" MPH ");
}
else{
Serial.print("Speed = ");
Serial.print(mph,1);
Serial.println(" MPH ");
}
}
void loop(){
//print mph twice a second
displayMPH();
delay(0500);
}