rascupanamuha
Member
Hello. I did electric scooter myself, and i have one problem with speed measurement
Recently i successfully programmed speed sensor on my scooter with reed relay. Max speed is 30kmph, so relay is fast enough (0.26m is diameter of the wheel). It worked very well until glass of that relay broke down.
Now i dont have exactly the same code, but here is what i have and how i think it should work:
and this:
So on every rising edge, time and time_last should be stored, and speed printed out.
When i short 2 pins that are usually connected to reed relay, it does work. Every "tap" i get new speed. But when i connect my reed relay it usually show "Speed: 0" or 2880.
I have tested relay with multimeter if it works, and it does what it should. I really don't understand what is happening here so please help me.
Here is the full code is someone needs to help me, controler is arduino uno
Recently i successfully programmed speed sensor on my scooter with reed relay. Max speed is 30kmph, so relay is fast enough (0.26m is diameter of the wheel). It worked very well until glass of that relay broke down.
Now i dont have exactly the same code, but here is what i have and how i think it should work:
Code:
/SPEED
if (time > 0) {
period = (time)/1000;
speedmps = 0.8/period;
speedkmph = speedmps*3.6;
lcd.setCursor(0,1); lcd.print ("Speed:");
// if (speedkmph < 1) lcd.print (" ");
// if (speedkmph < 10) lcd.print (" ");
// if (speedkmph < 100) lcd.print (" ");
lcd.print (speedkmph);
}
else {lcd.setCursor(0,1); lcd.print ("Speed: 0"); }
delay (10);
Code:
void fan_interrupt()
{
time = (millis() - time_last);
time_last = millis();
}
So on every rising edge, time and time_last should be stored, and speed printed out.
When i short 2 pins that are usually connected to reed relay, it does work. Every "tap" i get new speed. But when i connect my reed relay it usually show "Speed: 0" or 2880.
I have tested relay with multimeter if it works, and it does what it should. I really don't understand what is happening here so please help me.
Here is the full code is someone needs to help me, controler is arduino uno
Code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <math.h>
Servo esc;
int throttlePin = 0;
int LED = 8;
unsigned int rpmilli;
float speedmps = 0;
float speedkmph = 0;
double Thermistor(int RawADC) {
long Resistance; double Temp;
Resistance=((10240000/(1024-RawADC)) - 10000); // 10k Thermistor, in lower part
Temp = log(Resistance);
Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
Temp = Temp - 273.15;
return Temp;
}
volatile float time = 0;
volatile float time_last = 0;
volatile float period = 0;
LiquidCrystal_I2C lcd(0x20, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
void setup()
{
pinMode(LED, OUTPUT);
esc.attach(3);
Serial.begin(9600); // Used to type in characters
attachInterrupt(0, fan_interrupt, RISING);
#define ThermistorPIN 1 // Analog Pin 0
double temp;
lcd.begin(20,4);
lcd.backlight();
lcd.setCursor(2,1); lcd.print("Electric Scooter");
lcd.setCursor(4,2); lcd.print("Version: 1.0");
esc.write(0);
delay(200);
lcd.clear();
}
void fan_interrupt()
{
time = (millis() - time_last);
time_last = millis();
//speedkmph++;
}
void loop()
{
digitalWrite(LED, 0);
lcd.home ();
//THROTTLE
int throttle = analogRead(throttlePin);
throttle = map(throttle, 0, 1023, 43, 128);
esc.write(throttle);
int throttlePercent = analogRead(throttlePin);
throttlePercent = map(throttle, 57, 115, 0, 100);
lcd.setCursor(0,2); lcd.print ("Throttle:");
if (throttlePercent < 1) lcd.print (" ");
if (throttlePercent < 10) lcd.print (" ");
if (throttlePercent < 100) lcd.print (" ");
lcd.print (throttlePercent);
//SPEED
if (time > 0) {
period = (time)/1000;
speedmps = 0.8/period;
speedkmph = speedmps*3.6;
lcd.setCursor(0,1); lcd.print ("Speed:");
// if (speedkmph < 1) lcd.print (" ");
// if (speedkmph < 10) lcd.print (" ");
// if (speedkmph < 100) lcd.print (" ");
lcd.print (speedkmph);
}
else {lcd.setCursor(0,1); lcd.print ("Speed: 0"); }
//TEMPERATURE
double temp=Thermistor(analogRead(ThermistorPIN));
lcd.setCursor(0,0); lcd.print ("Temp: ");
lcd.print (temp); lcd.print ("C");
delay (10);
}