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)