Hi fellow experimentalists!
I'm trying to develop a device for a motorcycle course, that starts a 12 seconds timer when riding in one way and measures the speed of the motorcycle when passing in the other direction.
The passing is captured by 2 IR sensors.
The problem i'm facing is that the timer and speedtrap works...as long there only 1 interruption of the sensors...
Since motorcycles have 2 wheels...the problem starts.
The timer gets interrupted and I can't figure out how to fix this.
I'm looking and trying to find a way that the arduino looks only at the first signal, when the front wheel passes, so it ignores the second passage of the rear wheel.
...but without further results...
maybe someone can help me figure this out?
Thank you!
Not sure, even after reading the forum, if i need to paste my code just underneath this text.
#include <Arduino.h>
int RelayPin = 7; //relay on pin 7
int SpeedPin = 8; //drukknop activatie snelheidsmeting op 8
int TimerPin = 6; //drukknop activatie 12" op 6
#include <Wire.h> // Library for I2C communication
#include <LiquidCrystal_I2C.h> // Library for LCD
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2); // Change to (0x27,16,2) for 16x2 LCD.
//const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; //toewijzing pinnen lcd
//LiquidCrystal lcd(rs, en, d4, d5, d6, d7); //iets voor LCD
int Timer_buttonState = 0; // current state of the button
int Speed_buttonState = 0; // current state of the button
int lastTimer_buttonState = 0; // previous state of the button
int RelayState = 0; // remember current relai state
int loadState_1 = 1;
int count = 12; //initial value counter
int switch_var = 0; //switch varon 0
void setup() {
lcd.init();
//lcd.backlight();
pinMode(RelayPin, OUTPUT); //output relay
pinMode(TimerPin, INPUT); //input sensor start 12"
pinMode(SpeedPin, INPUT); //input sensor speedtrap
lcd.clear(); //prepare display
lcd.setCursor(1,0);
lcd.print("Snelheidsmeter");
lcd.setCursor(5,1);
lcd.print("klaar");
}
void loop() {
Timer_buttonState = ! digitalRead(TimerPin); // read the pushbutton input pin
Speed_buttonState = ! digitalRead(SpeedPin);
int photo_light_1; //variabele light_1
int photo_light_2; //variabele light_2
unsigned int time_1; //variabele time_1
unsigned int time_2; //variabele time_2
float elapsedTime; //variabele elapsed time
float spd; //variabele speed
unsigned int afstandSensor; //variabele distance between sensors
time_1 = 0; //set initial timing to 0
time_2 = 0;
afstandSensor = 805; //set distance sensors
//sensor that gets activated first
if (Timer_buttonState == 1)
{
switch_var = 1; //start timer
}
else if (Speed_buttonState == 1)
{
switch_var = 2; //start speedtrap
}
switch (switch_var)
{
case 1:
if (Timer_buttonState == 1 && lastTimer_buttonState == 0)
{
loadState_1++;
if (loadState_1 > 2) loadState_1 = 1; // 2 states as it is now
RelayState = 1 - RelayState; //RelayState will alternate between 1 and 0
digitalWrite(RelayPin, RelayState); //Variabele activeren RelayState
//delay(100);
digitalWrite(RelayPin,HIGH); //activeren relai bij activeren 12"
delay(100);
digitalWrite(RelayPin,LOW); // relai uitschakelen
}
//delay(50); //noise cancelling button
lastTimer_buttonState = Timer_buttonState; //onthouden stand van knop
if (RelayState == 1)
{ //wanner knop hoog wordt mag de 12" timer beginnen
lcd.clear();
lcd.setCursor(5,0);
lcd.print("Timer :"); //op eerste lijn "timer" zichtbaar maken
lcd.setCursor(8,1); //lcd 2de rij aanspreken
lcd.print("Sec"); //Plaats "Sec" op tweede rij
lcd.setCursor(5, 1); //activeren waar seconden zichtbaar gemaakt worden
lcd.print(count); //zichtbaar maken seconden
delay(1000); //1 seconde wachten
count = count -1; //aftellen
if (count == 0) { //wanneer teller op 0 led laten/zoemer branden
digitalWrite(RelayPin,HIGH); //activatie led
//delay(2000);
//digitalWrite(RelayPin, LOW); // led uitschakelen
Timer_buttonState = 0 ;
}
if (count < 0)
{ //Teller tot 0 laten aftellen
RelayState = 0;
//digitalWrite(RelayPin,HIGH); //activatie led
//delay(100);
digitalWrite(RelayPin, LOW); // led uitschakelen
count = 12; //resetten teller
delay(3000);
lcd.clear();
delay(1000);
lcd.setCursor(1,0);
lcd.print("Snelheidsmeter");
lcd.setCursor(5,1);
lcd.print("klaar");
}
}
break;
case 2:
if (Speed_buttonState == HIGH)
{
photo_light_1 = ! digitalRead(SpeedPin);
if (photo_light_1 == HIGH)
{
time_1 = millis();
Serial.print("time 1");
Serial.println(time_1);
while(true)
{
photo_light_2 = ! digitalRead(TimerPin);
if (photo_light_2 == HIGH)
{
time_2 = millis();
Serial.print("time 2");
Serial.println(time_2);
elapsedTime = (time_2 - time_1);
Serial.print("elapsed time ");
Serial.print(elapsedTime);
Serial.println(" sec");
delay(200);
spd = (afstandSensor/elapsedTime);
Serial.print("Snelheid ");
Serial.println(spd);
delay(200);
lcd.clear();
lcd.setCursor(4,0);
lcd.print("Snelheid");
lcd.setCursor(3,1);
lcd.print(spd);
lcd.print(" km/h");
delay(5000);
lcd.clear();
delay(1000);
lcd.setCursor(1,0);
lcd.print("Snelheidsmeter");
lcd.setCursor(5,1);
lcd.print("klaar");
// break;
}
}
}
}
//break;
}
}
12_seconden_timer_v2.cpp (5.69 KB)
12_seconden_timer_v2.cpp (5.69 KB)