# Hall Effect Speed and Distance Sensor Code

I need some help trying to determine what input I need to put into my code to output the distance traveled by the magnet in my sensor setup. The existing code that I have is shown below. I would appreciate any help on this matter as I utterly lost at this point and need to complete this sensor for a school project. Thank you

//outputs speed of bicycle to LCD

//calculations
//circumference = pi2r =~85 inches
//max speed of 35mph =~ 616inches/second
//max rps =~7.25

#define reed A0//pin connected to read switch

//storage variables

int reedVal;
long timer = 0;// time between one full rotation (in ms)
float mph = 0.00;
float circumference;
boolean backlight;

int maxReedCounter = 100;//min time (in ms) of one rotation (for debouncing)
int reedCounter;

void setup(){

reedCounter = maxReedCounter;
pinMode(1,OUTPUT);//tx
pinMode(2,OUTPUT);//backlight switch
pinMode(reed, 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 reed switch
reedVal = digitalRead(reed);//get val of A0
if (reedVal){//if reed switch is closed
if (reedCounter == 0){//min time between pulses has passed
mph = (56.8*float(circumference))/float(timer);//calculate miles per hour
timer = 0;//reset timer
reedCounter = maxReedCounter;//reset reedCounter
}
else{
if (reedCounter > 0){//don’t let reedCounter go negative
reedCounter -= 1;//decrement reedCounter
}
}
}
else{//if reed switch is open
if (reedCounter > 0){//don’t let reedCounter go negative
reedCounter -= 1;//decrement reedCounter
}
}
if (timer > 2000){
mph = 0;//if no new pulses from reed switch- tire is still, set mph to 0
}
else{
timer += 1;//increment timer
}
}

void displayMPH(){
Serial.print("Speed = “);
Serial.print(mph);
Serial.println(” MPH ");
}

void loop(){
//print mph once a second
displayMPH();
delay(1000);
}

``````  OCR1A = 1999;// = (1/1000) / ((1/(16*10^6))*smiley-cool - 1
``````

?

That line should have been copied as follows

OCR1A = 1999;// = (1/1000) / ((1/(1610^6)) 8 ) - 1

``````//outputs speed of bicycle to LCD

//calculations
//circumference = pi*2*r =~85 inches
//max speed of 35mph =~ 616inches/second
//max rps =~7.25

#define reed A0//pin connected to read switch

//storage variables

int reedVal;
long timer = 0;// time between one full rotation (in ms)
float mph = 0.00;
float circumference;
boolean backlight;

int maxReedCounter = 100;//min time (in ms) of one rotation (for debouncing)
int reedCounter;

void setup(){

reedCounter = maxReedCounter;
pinMode(1,OUTPUT);//tx
pinMode(2,OUTPUT);//backlight switch
pinMode(reed, 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 reed switch
reedVal = digitalRead(reed);//get val of A0
if (reedVal){//if reed switch is closed
if (reedCounter == 0){//min time between pulses has passed
mph = (56.8*float(circumference))/float(timer);//calculate miles per hour
timer = 0;//reset timer
reedCounter = maxReedCounter;//reset reedCounter
}
else{
if (reedCounter > 0){//don't let reedCounter go negative
reedCounter -= 1;//decrement reedCounter
}
}
}
else{//if reed switch is open
if (reedCounter > 0){//don't let reedCounter go negative
reedCounter -= 1;//decrement reedCounter
}
}
if (timer > 2000){
mph = 0;//if no new pulses from reed switch- tire is still, set mph to 0
}
else{
timer += 1;//increment timer
}
}

void displayMPH(){
Serial.print("Speed = ");
Serial.print(mph);
Serial.println(" MPH ");
}

void loop(){
//print mph once a second
displayMPH();
delay(1000);
}
``````

Use the “#” CODE TAGS button to post your code so it looks like this.

I need some help trying to determine what input I need to put into my code to output the distance traveled by the magnet in my sensor setup.

Only you know anything about YOUR setup, and you're not telling us.

If you have the magnet mounted on a wheel, and the sensor on the frame, simple geometry will tell you the contact patch's travel distance per trigger of the sensor.

if you have one magnet on the wheel and the sensor on the frame, each time the magnet passes the wheel you will have traveled the same distance as the circumference of your wheel.

``````//outputs speed of bicycle to LCD

//calculations
//circumference = pi*2*r =~85 inches
//max speed of 35mph =~ 616inches/second
//max rps =~7.25

#define reed A0//pin connected to read switch

//storage variables

int reedVal;
long timer = 0;// time between one full rotation (in ms)
float mph = 0.00;
float circumference;
boolean backlight;

int maxReedCounter = 100;//min time (in ms) of one rotation (for debouncing)
int reedCounter;

void setup(){

reedCounter = maxReedCounter;
pinMode(1,OUTPUT);//tx
pinMode(2,OUTPUT);//backlight switch
pinMode(reed, 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 reed switch
reedVal = digitalRead(reed);//get val of A0
if (reedVal){//if reed switch is closed
if (reedCounter == 0){//min time between pulses has passed
mph = (56.8*float(circumference))/float(timer);//calculate miles per hour
timer = 0;//reset timer
reedCounter = maxReedCounter;//reset reedCounter
}
else{
if (reedCounter > 0){//don't let reedCounter go negative
reedCounter -= 1;//decrement reedCounter
}
}
}
else{//if reed switch is open
if (reedCounter > 0){//don't let reedCounter go negative
reedCounter -= 1;//decrement reedCounter
}
}
if (timer > 2000){
mph = 0;//if no new pulses from reed switch- tire is still, set mph to 0
}
else{
timer += 1;//increment timer
}
}

void displayMPH(){
Serial.print("Speed = ");
Serial.print(mph);
Serial.println(" MPH ");
}

void loop(){
//print mph once a second
displayMPH();
delay(1000);
}
``````

My magnet for my sensor is set up on a rotating part while my actual hall effect sensor is mounted onto the frame. I am able to calculate the speed at which the magnet (and therefore the rotating part) is moving and what I want to find out is what is the total distance that the magnet has traveled. I understand that when one revolution has been completed that the magnet will have traveled a total distance that is equivalent to the circumference of the rotating part, but I want to program the arduino to calculate exactly how far the magnet has traveled and then display this information onto an LCD display as is already the case with the speed measurement. I’m creating this sensor to mount onto a bicycle and hopefully with this setup I will be able to visually see the speed that I am traveling at and the total distance that I have traveled over a trip. I hope that this clears up any confusion.

Count the number of times the reed switch closes. When you want to output distance, multiply this count by the wheel circumference.

and for the speed you can also calculate it with a simple equation, something like:
Speed = Distance / Time

So if you know the circumference of the wheel and you masure how long it took it to make one turn, then you can also calculate the speed and display it on your lcd. I know which equations I would utilize to be able to determine the speed, but I need help on exactly how to program the arduino to use those equations. I know very little about coding and simply was able to get this far in this code because it was supplied to me as I inherited this arduino and the codes from a friend. Could someone give me an example code of how I would program the arduino to calculate and display distance?

Could someone give me an example code of how I would program the arduino to calculate and display distance?

What equation are you planning to implement? You already have variables with all the relevant values. Using them in an equation is not rocket science. You aren't learning a thing when you copy and paste code.