# Trouble adapting Speed code (Reed Switch)

Below is a code used to calculate and display MPH on a pushbike, using a reedswitch. I have tried changing the code to fit in with my wind speed sensor (anemometer). Which has a radius of 0.87 Inches instead of 13.5 used below. I have changed this and i have also tried changing ‘maxReedCounter’ to a lower number. Considering my anemometer is going to be spinning faster. However upon implementing the code no matter how much i spin my wind speed sensor, the display in serial port view is always 0. Any ideas?
I have connected my wind-speed sensor to ground on the arduino board via a pull 10k ohm resistor and to Analogue pin 0.
Any help will be greatly appreciated.

``````//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;// time between one full rotation (in ms)
float mph;
float circumference;

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

void setup(){

reedCounter = maxReedCounter;
pinMode(reed, INPUT);

// TIMER SETUP- the timer interrupt allows precise timed measurements of the reed switch
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.println(mph);
}

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

Which has a radius of 0.87 Inches instead of 13.5 used below.

So, you took this code that works, made some changes that didn't work, and you want us to guess what changes you made, and where you screwed up. No, it doesn't work that way.

The only coding i have changed is the radius (0.87 instead of 13.5). As i want to calculate speed of my turning anemometer, instead a bike wheel. I'm fairly new to arduino programming and i'm just looking for a few pointers as to why that didn't work.

Whoever wrote this code, did not label everything. There is a magic number here,

mph = (56.8*float(circumference))/float(timer);

which is crucial to your calculations. You can't just change the radius without knowing what each part does and what each number represents.

What is the 56.8 and how does it work with the original 13.5?

So for those wondering about the 56.8 in the mph calculation: speed of 1in/ms = 56.8mi/h so the radius value won't effect this, this is just to convert from in per ms to miles per hour.

hope this helps clarify things, -S

It looks like you are using the code from this tutorial http://www.instructables.com/id/Arduino-Bike-Speedometer/?ALLSTEPS

Are you certain there is a reed switch(and not a hall sensor) in the anemometer. Is your reed switch wired like shown in the tutorial Step 1.

Have you confirmed Step 4, that you can indeed read 0 and 1023 as you turn the anemometer.