Hello guys, i little bit confusion, i have code to make lap timer for race mini 4wd 3 lane, everything is good and running well but my track is have interchange and im confused with that.
and i want make error handling for example the car (mini 4wd) is out off track, from my code to determined finish lane, is when sensor 1 or 2 or 3 has finished 3 laps then win.
here's my Code:
#include <Wire.h>
#include <Time.h>
#include <LiquidCrystal_I2C.h>
#define WAKTU_TAMPIL_JAM 10 //detik
#define WAKTU_TAMPIL_KALENDAR 5 //detik
// Set the LCD address (you may need to adjust this based on your specific LCD)
const int lcdAddress = 0x27;
// Set the IR sensor pin
const int irSensorPin = 2; // Adjust the pin based on your setup
// Set the button pin
const int buttonPin = 6;
// Initialize the LCD
LiquidCrystal_I2C lcd(lcdAddress, 16, 2); // Adjust the dimensions (16, 2) based on your LCD size
char lineBuff[20];
char lineBuff2[20];
char modesel[20];
int lineA;
int lineB;
int lineC;
int mulai;
int seconds;
int cs;
long start_time = 0;
int lapa,lapb,lapc;
int modelan = 3; // Default mode is 3 lane
void setup(void)
{
Wire.begin();
// Initialize the LCD
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Engine On....");
sprintf(modesel, "Mode : ", modelan);
lcd.setCursor(0, 1);
lcd.print(modesel);
delay(2000);
lcd.clear();
pinMode(buttonPin, INPUT); // Set button pin as input
}
void loop(void)
{
// Check button state to change mode
if (digitalRead(buttonPin) == HIGH) {
delay(200); // debounce
modelan = (modelan % 3) + 1; // Cycle through modes 1, 2, 3
}
//============================================
lineA = digitalRead(2);
lineB = digitalRead(3);
lineC = digitalRead(4);
if((lineA == 0)&&(lineB == 1)&&(lineC == 1)&&(mulai == 1)){
lapa++;
delay(500);
}
if((lineA == 1)&&(lineB == 0)&&(lineC == 1)&&(mulai == 1)){
lapb++;
delay(500);
}
if((lineA == 1)&&(lineB == 1)&&(lineC == 0)&&(mulai == 1)){
lapc++;
delay(500);
}
if((lineA == 0)&&(lineB == 0)&&(lineC == 1)&&(mulai == 1)){
lapa++;
lapb++;
delay(500);
}
if((lineA == 0)&&(lineB == 1)&&(lineC == 0)&&(mulai == 1)){
lapa++;
lapc++;
delay(500);
}
if((lineA == 1)&&(lineB == 0)&&(lineC == 0)&&(mulai == 1)){
lapb++;
lapc++;
delay(500);
}
if((lineA == 0)&&(lineB == 0)&&(lineC == 0)&&(mulai == 1)){
lapa++;
lapb++;
lapc++;
delay(500);
}
//=============================================
//finish code
if(lapa > modelan){
lcd.clear();
sprintf(lineBuff2, "WIN", lapa, lapb, lapc);
lcd.setCursor(0, 1); // Set cursor to the second line
lcd.print(lineBuff2); // Print the formatted time
delay(3000);
lcd.clear();
hasila();
}
if(lapb > modelan){
lcd.clear();
sprintf(lineBuff2, "WIN", lapa, lapb, lapc);
lcd.setCursor(0, 1); // Set cursor to the second line
lcd.print(lineBuff2); // Print the formatted time
delay(3000);
lcd.clear();
hasila();
}
if(lapc > modelan){
lcd.clear();
sprintf(lineBuff2, "WIN", lapa, lapb, lapc);
lcd.setCursor(0, 1); // Set cursor to the second line
lcd.print(lineBuff2); // Print the formatted time
delay(3000);
lcd.clear();
hasila();
}
//=============================================
lineA = digitalRead(2);
if((lineA == 0)&&(mulai == 0)){
delay(200);
mulai = 1;
start_time = millis();
}
if(mulai == 1){
seconds = (millis() - start_time) / 1000;
cs = ((millis() - start_time) / 10) % 100;;
}
//===========================================================================
sprintf(lineBuff, "%d:%d ", seconds, cs);
lcd.setCursor(0, 0); // Set cursor to the first line
lcd.print(lineBuff); // Print the formatted time
sprintf(lineBuff2, "Lap : %d/%d/%d ", lapa, lapb, lapc);
lcd.setCursor(0, 1); // Set cursor to the first line
lcd.print(lineBuff2); // Print the formatted time
//===========================================================================
}
void hasila(){
sprintf(lineBuff, "%d:%d ", seconds, cs);
lcd.setCursor(0, 0); // Set cursor to the first line
lcd.print(lineBuff); // Print the formatted time
sprintf(lineBuff2, "Win Lane A", lapa, lapb, lapc);
lcd.setCursor(0, 1); // Set cursor to the first line
lcd.print(lineBuff2); // Print the formatted time
hasila();
}
void hasilb(){
sprintf(lineBuff, "%d:%d ", seconds, cs);
lcd.setCursor(0, 0); // Set cursor to the first line
lcd.print(lineBuff); // Print the formatted time
sprintf(lineBuff2, "Win Lane B", lapa, lapb, lapc);
lcd.setCursor(0, 1); // Set cursor to the first line
lcd.print(lineBuff2); // Print the formatted time
hasilb();
}
void hasilc(){
sprintf(lineBuff, "%d:%d ", seconds, cs);
lcd.setCursor(0, 0); // Set cursor to the first line
lcd.print(lineBuff); // Print the formatted time
sprintf(lineBuff2, "Win Lane C", lapa, lapb, lapc);
lcd.setCursor(0, 1); // Set cursor to the first line
lcd.print(lineBuff2); // Print the formatted time
hasilc();
}