Im having some trouble with a project to monitor various environmental changes while im out and about.
eventually my aim is to have an e-bike, with a few arduinos for calculation and systems managment.
features such as, atmosphereic pressure, temp, g-force/accel, compass, lighting, speed meter, etc.
right now i have a light and a proximity sensor mounted on the front fork.
the sensor is a wtb12-3p2431, and its set up so i get high resolution by spoke readings.
here is my current sketch, i can seem to get a good speed reading, the values are usually too low or too high with respect of how fast
i spin the wheel by hand.
const int buttonPin = 9; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
const float spoke = 0.027777777777777777777777777777778;
const float pi = 3.1415926535897932384626433832795;
const float dist_spoke = 18.344444444444444444444444444444;
float dist = 0;
const float wheeldiam = 660.4; //mm
float lasttimestamp = 0;
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int laststate = 0; // variable for reading the pushbutton status
float starttime = millis();
float endtime;
int count=0;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop(){
buttonState = digitalRead(buttonPin);
if (buttonState != laststate && buttonState == HIGH) { // 1/36th revolution, 36 spokes
endtime = millis();
count++;
if (count >= 36) { //one full revolution
count = 0;
}
dist = dist_spoke;
float lasttimestamp_msec = (endtime-starttime);
//Serial.print(lasttimestamp_msec);
//Serial.print("=");
//Serial.print(count);
//Serial.print("=");
Serial.println((dist*0.001)/(lasttimestamp_msec/1000));
starttime = millis();
}
laststate = buttonState;
}
im trying to get km/h, m/s, mi/h.
according to most speedometers, they track via one rotation of the wheel.
i have a 26in wheel, with 36 spokes. so i convert 26 to mm which is 660.4mm.
then divide 660.4 by 36 to get 18.34.
i time how long it takes from one spoke to the next.
after that i divide distance by time, but when doing so MPH never come out right.
Serial.println(((dist*0.000001)*0.62137119223733396961743418436332)/(lasttimestamp_msec/3600000));
( *0.000001) = mm to km
(*0,621....) = km to mile
then 3600000 = msec to hour.
but when spinning the wheel very fast, i get serial readings of about 5.xx, and when i used to have an actual speedometer...
the tire would usually rate at about 15 mph.
any suggestions?
thanks in advance