Mudanças entre as edições de "Medidor de Velocidade"

De Garoa Hacker Clube
Ir para navegação Ir para pesquisar
(→‎Código: Utilização do código aprimorado pelo ChatGPT)
Linha 8: Linha 8:
 
== Código==
 
== Código==
 
<pre>
 
<pre>
 
/*
 
 
Escala H0 1:87
 
 
Velocidade = Espaço / Tempo
 
v = dist [mm] / Time [micro_sec]
 
v = dist/Time*3600 [km/h]
 
v = dist/Time*3600*87[km/h em escala real]
 
 
*/
 
 
   
 
#include <LiquidCrystal.h>
 
#include <LiquidCrystal.h>
   
#define sensor_1 7 // Pino do sensor
+
#define SENSOR_1 7 // Pino do sensor 1
#define sensor_2 8 // Pino do sensor
+
#define SENSOR_2 8 // Pino do sensor 2
#define Esperando 13 // Pino do LED de leitura
+
#define LED_STATUS 13 // Pino do LED de leitura
   
float dist = 74; //em mm
+
const float DIST_MM = 74; // Distância em mm
   
 
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
unsigned long Time = 0; //micro sec
 
unsigned long delta_time = 0; //micro sec
 
   
float vel = 0; //em mm/s
+
unsigned long startTime = 0; // Tempo em microssegundos
  +
unsigned long elapsedTime = 0; // Tempo decorrido em microssegundos
  +
float speed_mm_per_us = 0; // Velocidade em mm/µs
  +
float speed_kmh = 0; // Velocidade em km/h
   
 
void setup() {
LiquidCrystal lcd_1(12, 11, 5, 4, 3, 2);
 
  +
lcd.begin(16, 2); // Inicializa o LCD com 16 colunas e 2 linhas
 
 
pinMode(SENSOR_1, INPUT_PULLUP);
 
 
pinMode(SENSOR_2, INPUT_PULLUP);
void setup(){
 
  +
pinMode(LED_STATUS, OUTPUT);
lcd_1.begin(16, 2); // Set up the number of columns and rows on the LCD.
 
 
 
 
lcd.print("hello world!");
pinMode(sensor_1, INPUT_PULLUP);
 
pinMode(sensor_2, INPUT_PULLUP);
 
// Print a message to the LCD.
 
lcd_1.print("hello world!");
 
 
delay(500);
 
delay(500);
lcd_1.setCursor(0, 0);
+
lcd.clear();
lcd_1.print("vel(cm/s)maquete");
+
lcd.print("Vel(cm/s) Maquete");
lcd_1.setCursor(0, 1);
+
lcd.setCursor(0, 1);
lcd_1.print("Vel(Km/h): real");
+
lcd.print("Vel(Km/h): Real");
 
delay(2000);
 
delay(2000);
lcd_1.clear();
+
lcd.clear();
lcd_1.print("Esperando");
+
lcd.print("Esperando");
 
 
}
 
}
   
void loop(){
+
void loop() {
if (!digitalRead(sensor_1)) {
+
if (!digitalRead(SENSOR_1)) {
Time = micros();
+
startTime = micros();
lcd_1.clear();
+
lcd.clear();
lcd_1.print("Fazendo leitura");
+
lcd.print("Fazendo leitura");
digitalWrite(Esperando,HIGH);
+
digitalWrite(LED_STATUS, HIGH);
 
while (digitalRead(sensor_2)) {
+
while (digitalRead(SENSOR_2)) {
  +
// Espera até o SENSOR_2 ser acionado
 
}
 
}
delta_time = micros() - Time;
 
 
 
 
elapsedTime = micros() - startTime;
//APARTIR DAQUI DEVERIA SER UMA FUNÇÂO//
 
  +
calculateSpeed();
//// Eu não sei tipagem ._. ////
 
  +
displaySpeed();
////// Vai dar merda ... //////
 
 
vel = dist/delta_time; //em mm/us
 
lcd_1.clear();
 
lcd_1.print("vel(cm/s): ");
 
lcd_1.print(vel*pow(10, 5));
 
lcd_1.setCursor(0, 1);
 
lcd_1.print("Vel(Km/h): ");
 
lcd_1.print(vel*3600*87);
 
 
delay(5000);
 
delay(5000);
lcd_1.clear();
+
lcd.clear();
lcd_1.print("Esperando");
+
lcd.print("Esperando");
digitalWrite(Esperando,LOW);
+
digitalWrite(LED_STATUS, LOW);
 
}
 
}
 
if (!digitalRead(SENSOR_2)) {
//IGNORAR DAQUI PARA BAIXO
 
  +
startTime = micros();
if (!digitalRead(sensor_2)) {
 
Time = micros();
+
lcd.clear();
 
lcd.print("Fazendo leitura");
while (digitalRead(sensor_1)) {
 
digitalWrite(Esperando,HIGH);
+
digitalWrite(LED_STATUS, HIGH);
 
 
while (digitalRead(SENSOR_1)) {
  +
// Espera até o SENSOR_2 ser acionado
 
}
 
}
  +
digitalWrite(Esperando,LOW);
 
delta_time = micros() - Time;
+
elapsedTime = micros() - startTime;
Serial.print("Time: ");
+
calculateSpeed();
Serial.println(Time);
+
displaySpeed();
Serial.print("Delta: ");
 
Serial.println(delta_time);
 
Serial.print("Velocidade: ");
 
Serial.print(dist/delta_time);
 
Serial.println("mm/us");
 
Serial.print("Velocidade: ");
 
Serial.print(dist*3600*87/delta_time);
 
Serial.println("Km/h");
 
Serial.println();
 
 
delay(5000);
 
delay(5000);
 
lcd.clear();
 
lcd.print("Esperando");
 
digitalWrite(LED_STATUS, LOW);
 
}
 
}
 
}
 
}
   
  +
void calculateSpeed() {
  +
// Calcula a velocidade em mm/µs e km/h
  +
speed_mm_per_us = DIST_MM / elapsedTime;
  +
speed_kmh = speed_mm_per_us * 3600 * 87; // 87 é o fator de escala
  +
}
  +
  +
void displaySpeed() {
  +
lcd.clear();
 
lcd.print("Vel(cm/s): ");
  +
lcd.print(speed_mm_per_us * 1e5); // Convertendo para cm/s
 
lcd.setCursor(0, 1);
 
lcd.print("Vel(Km/h): ");
  +
lcd.print(speed_kmh); // Velocidade em km/h
  +
}
   
 
</pre>
 
</pre>

Edição das 10h47min de 8 de setembro de 2024

Circuito

Bem simples, apenas um Arduino com o GND ligado a um dos trilhos e deois sensores que são lâminas de papel aluminio por cima deste trilho, quando o trem passa ele fecha contato e detecta que o momento, quando passa pelo segundo o Arduino calcula a velocidade e manda via serial.

TODO

  • colocar um display para exibir a velocidade
  • melhorar o código com uma função para fazer o Seria.print

Código


#include <LiquidCrystal.h>

#define SENSOR_1    7  // Pino do sensor 1
#define SENSOR_2    8  // Pino do sensor 2
#define LED_STATUS   13  // Pino do LED de leitura

const float DIST_MM = 74; // Distância em mm

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

unsigned long startTime = 0; // Tempo em microssegundos
unsigned long elapsedTime = 0; // Tempo decorrido em microssegundos
float speed_mm_per_us = 0; // Velocidade em mm/µs
float speed_kmh = 0; // Velocidade em km/h

void setup() {
  lcd.begin(16, 2); // Inicializa o LCD com 16 colunas e 2 linhas
  pinMode(SENSOR_1, INPUT_PULLUP);
  pinMode(SENSOR_2, INPUT_PULLUP);
  pinMode(LED_STATUS, OUTPUT);
  
  lcd.print("hello world!");
  delay(500);
  lcd.clear();
  lcd.print("Vel(cm/s) Maquete");
  lcd.setCursor(0, 1);
  lcd.print("Vel(Km/h): Real");
  delay(2000);
  lcd.clear();
  lcd.print("Esperando");
}

void loop() {
  if (!digitalRead(SENSOR_1)) {
    startTime = micros();
    lcd.clear();
    lcd.print("Fazendo leitura");
    digitalWrite(LED_STATUS, HIGH);
    
    while (digitalRead(SENSOR_2)) {
      // Espera até o SENSOR_2 ser acionado
    }
    
    elapsedTime = micros() - startTime;
    calculateSpeed();
    displaySpeed();
    delay(5000);
    lcd.clear();
    lcd.print("Esperando");
    digitalWrite(LED_STATUS, LOW);
  }
  if (!digitalRead(SENSOR_2)) {
    startTime = micros();
    lcd.clear();
    lcd.print("Fazendo leitura");
    digitalWrite(LED_STATUS, HIGH);
    
    while (digitalRead(SENSOR_1)) {
      // Espera até o SENSOR_2 ser acionado
    }
    
    elapsedTime = micros() - startTime;
    calculateSpeed();
    displaySpeed();
    delay(5000);
    lcd.clear();
    lcd.print("Esperando");
    digitalWrite(LED_STATUS, LOW);
  }
}

void calculateSpeed() {
  // Calcula a velocidade em mm/µs e km/h
  speed_mm_per_us = DIST_MM / elapsedTime;
  speed_kmh = speed_mm_per_us * 3600 * 87; // 87 é o fator de escala
}

void displaySpeed() {
  lcd.clear();
  lcd.print("Vel(cm/s): ");
  lcd.print(speed_mm_per_us * 1e5); // Convertendo para cm/s
  lcd.setCursor(0, 1);
  lcd.print("Vel(Km/h): ");
  lcd.print(speed_kmh); // Velocidade em km/h
}