Arduino with Millis function - computer cycle

Asked

Viewed 1,192 times

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");  
    }  
  }  
}  
  • 1

    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.

  • thanks a lot for the help, I’ll try here

  • Ah, by the way, the user (and moderator) @bfavaretto had posted a suggestion to reverse the position of the lines float volta = millis() - tempoanterior; and float 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.

  • 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

  • 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

  • Related: https://github.com/carmolim/cycloduino http://forum.arduino.cc/index.php?topic=136786.0 http://scuola.arduino.cc/courses/lessons/view/kKa1Z3K

  • tempoh = (turn * 0.2777777) / 1000000

  • @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

Show 3 more comments

1 answer

1

For this type of application you should consider using a earth-shattering pin 2 or 3 of Adian each pin pass interrupts the program and counts a variable.

You can use the Millis just to know when time has gone through a time period like seconds or minutes.

https://www.arduino.cc/en/Reference/Interrupts

Calibrate the sensor to know when it reached 1 meter this will give you a reference

You can thereby 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++;}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.