Electrical to mechanical speedo

Hi there Im brand new to Arduino and to programming really. Im trying to build some code to take the signal from a magnitic switch (Bike speedo) and outputing it to a PWM output to evenutally turn a motor (Im using a LED at the moment for test purposes for output). Ive been using 'time = pulseIn(SWITCH,HIGH)' for the switch but with little success. Please see the code below. Some of the equasions ect may be incorrect but The serial output does not vary from 0. Ive double checked the inputs and everything should work, the transistor will operate for the LED and the witch gets 5 v closed and 0 v accross the input resistor.

Any sugestions is pulseIn a bad choice for this type of code?

Thanks,

Rob.

const int LED = 9; //pin 9 for Led
const int SWITCH = 7; //input for the magnetic switch
unsigned long time;
float sped;
int level;

int diamiter = 2237; //wheel diamiter 28 in in mm
void setup()
{
pinMode(LED, OUTPUT);
//pinMode(SWITCH, INPUT);
Serial.begin(9600);
}
void loop()
{
time = pulseIn(SWITCH,HIGH);
delay(10);
Serial.print(time);
sped = 2.24*(diamiter/(time*.001));//speed in mph 2.24 converts ms-1 to mph
level = sped*10;
if (level >255)
{ level = 255;}
analogWrite(LED,level);

}

speedo1.ino (568 Bytes)

You can place your code between the code tags. Use the '#' button (above the smileys when you are writing a message).
You can also copy an URL in the text, like this: http://arduino.cc/en/Reference/pulseIn

My first thought was to measure the number of pulses during a certain time, but measuring the pulse length is a smart idea. You get the speed immediate for every rotation of the wheel. If it is accurate enough ...
But I think that the diameter of the wheel does not influence the pulse length.

But I have many questions:

  • If your sketch is not working, so what is not working ? Please be more precise.
  • How is the magnetic switch connected to the arduino ? can you upload a schematic ?
  • Is the magnetic switch some kind of reed contact, and not some fancy hall sensor ?
  • You checked the inputs ? are you sure the magnetic switch is working ?
  • Can you turn the led on and off with code ?
  • Is the variable 'time' chaning if you rotate the wheel with different speeds ?