Question :
I’m trying to set up a computer cycle with Arduino that displays the offset and speed on a 16×2 LCD display, but I’m having trouble calculating speed. The displacement traveled 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! Here is the schedule:
# 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");
}
}
}
Answer :
For this type of application you should consider using an interrupt pin 2 or 3 of arduino each pin step interrupts the program and counts a variable.
You can use millis only to know when time has passed for a period of time such as seconds or minutes.
Calibrate the sensor to know when it has reached 1 meter this will give you a reference
You can add average speeds and other types of measures.
Start from this code
// teste de velocidade
unsigned long contaPulso;
float kmh = 0;
unsigned long distancia;
unsigned long time = 1000;
int enable = 1;
void setup(){
Serial.begin(9600);
pinMode(2, INPUT);
attachInterrupt(0, incpulso, FALLING); //Configura o pino 2(Interrupção 0) interrupção
}
void loop (){
if (enable == 1){contaPulso = 0; sei(); enable = 0;}
if (millis() => time){
cli();
Serial.println(time);
time = millis() + 1000;
enable =1;
distancia = contaPulso + [seu valor de calibração];
kmh = distancia * 3,6;
Serial.print(distancia);
Serial.println("distancia ");
Serial.print(kmh);
Serial.println("KMH ");
}
}
void incpulso ()
{contaPulso++;}