reed switch controlling servo

i have a project where a reed switch senses the wheel speed of a bicycle. the arduino then tells the servo to move a certain angle depending on wheel speed.
this is for a bicycle speedometer, but with a needle on a gauge. not the digital read out type.
the servo would move the needle on the gauge.
i need the code for this.
an array would work i imagine.
i have the servo control on output 9, and the reed switch input on input A0.
please help, thanks.

How much experience have you got in using the Arduino ?

Read the number of reed switch closures over a period of time to work out the RPM of the wheel using millis() to do the timing not delay() as that would block operation of the program. You do not need to use an analogue pin for this as you will be using digitalRead() but you can use A0 as a digital pin if you need to. Once you have the RPM use the map() function with the RPM as its input and an output range to suit the servo. This will usually be from 0 to 180 but will depend on the servo and may need inverting.

How much of that do you understand ?

Probably best to search the forum for speedometer and tachometer to see previous postings

hi UKHeliBob ,
i understand most of it.
do you have any code that would tell the servo to move with respect to rpm/wheel speed?
the servo only needs to move 180 degrees and it would be a linear relationship.
the bicycle would have max speed of 25mph.
probably easier to do this with respect to rpm, not mph. less calcs.

The code to move the servo between 0 and 180 degrees given the RPM is easy. Look at how the map() function works.


does anyone have the actual code,( not how to do it).The code to move the servo between 0 and 180 degrees given the RPM.
the max RPM is 380.

byte servoPos = map(RPM, 0, 380, 0, 180);