7
I’m trying to build a computer cycle with Arduino that displays displacement and speed on a 16x2 LCD display, but I’m having trouble calculating speed. The travel is working perfectly, but the speed is only 0 km/h. I am a layman in programming, if anyone can help me thank you very much! Follow below the programming:
# include <LiquidCrystal.h>
LiquidCrystal lcd(4, 5, 6, 7, 8, 9);
int hallsensor = 12;
int contraste = 10; // PINO 3 LCD
int iluminacao = 11; // PINO 15 LC
int vcontraste = 80; // PWM CONTRASTE DISPLAY LCD
int viluminacao = 255; // PWM ILUMINAÇÃO DISPLAY LCD
float distancia;
float kilometros;
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
pinMode(hallsensor, INPUT);
pinMode(contraste, OUTPUT);
pinMode(iluminacao, OUTPUT);
}
void loop()
{
int estado = digitalRead(hallsensor);
analogWrite(contraste, vcontraste);
analogWrite(iluminacao, viluminacao);
if (estado < 1) // COLOQUEI NA ENTRADA ANALOGICA PARA QUE QUALQUER VALOR MENOR QUE 1 SEJA "SENTIDO" ( O IMA PODERA FICAR UM POUCO MAIS DISTANTE DO SENSOR)
{
distancia = distancia + 2.073656;
kilometros = kilometros + 0.002073656;
float tempoanterior = millis();
float volta = millis() - tempoanterior; // CALCULA O TEMPO DE UMA VOLTA
float tempoh = volta * 0000000.277777777; // CONVERTE O TEMPO DE UMA VOLTA PARA HORAS
unsigned long velocidade = (0.002073656/tempoh); // CALCULA A VELOCIDADE MEDIA DE UMA VOLTA EM KM/H
if (distancia<1000)
{
lcd.setCursor(0, 0);
lcd.print("Dis: ");
lcd.print(distancia);
lcd.print(" m");
Serial.print("Dis: ");
Serial.print(distancia);
Serial.println(" m");
lcd.setCursor(0, 1);
lcd.print("Vel: ");
lcd.print(velocidade);
lcd.print(" Km/h");
}
if (distancia>=1000)
{
lcd.setCursor(0, 0);
lcd.print("Dis: ");
lcd.print(kilometros);
lcd.print(" Km ");
Serial.print("Dis: ");
Serial.print(kilometros);
Serial.println(" m");
lcd.setCursor(0, 1);
lcd.print("Vel: ");
lcd.print(velocidade);
lcd.print(" Km/h");
}
}
}
Isn’t it kind of dangerous to let a simple IF determine the turn? If the magnet lingers on top of the sensor, won’t it count more than one turn? Maybe we needed a scheme like flip-flop that when it is less than 1, it must wait to return to 1 again to count new round. And preferably by a good margin of error.
– Bacco
thanks a lot for the help, I’ll try here
– Anthoni Silva
Ah, by the way, the user (and moderator) @bfavaretto had posted a suggestion to reverse the position of the lines
float volta = millis() - tempoanterior;
andfloat tempoanterior = millis();
, because as it is you will always have zero in "back". Test if it improves something, and comment if it gave difference, if possible.– Bacco
Thank you very much, I had to reverse even if I didn’t get zero. But it had more errors for example: float tempoh = turn * 0000000.2777777; // CONVERT THE TIME FROM ONE TURN TO HOURS - the right one was 0.0000002 - read that the float accepts only 6 ~ 7 decimal places of precision
– Anthoni Silva
I will post how the code stayed with the changes, I put the need for change of state so that it disappears once again around as you had mentioned
– Anthoni Silva
Related: https://github.com/carmolim/cycloduino http://forum.arduino.cc/index.php?topic=136786.0 http://scuola.arduino.cc/courses/lessons/view/kKa1Z3K
– Avelino
tempoh = (turn * 0.2777777) / 1000000
– Avelino
@Anthonisilva, are you thinking of riding an odometer? if yes, the ideal is to use a library such as to measure the movement of an Encoder, see this one: https://www.pjrc.com/teensy/td_libs_Encoder.html
– Delfino