Lap Timer mini 4WD with interchange Arduino uno + Senor Proximity

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();  
}

Hi, @dingky
Welcome to the forum.

How do you mean, can you post a simple track diagram?
A photograph of a hand draw diagram would be fine.

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

sorry for my bad hand draw hahaha...
i updated my code, i think the code can work well, maybe you can help me to verify, thanks before.

in my updated code, i used condition by lap count to add 1 to lapa or lapb or lapc and first lap i used lapall =0

#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 lapall = 0;
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);

//lap 1
if ((lapall == 0) && (mulai == 1)){
  if((lineA == 0)&&(lineB == 1)&&(lineC == 1)&&(mulai == 1)){
  lapc++;
  delay(500);
  }

  if((lineA == 1)&&(lineB == 0)&&(lineC == 1)&&(mulai == 1)){
  lapa++;
  delay(500);
  }

  if((lineA == 1)&&(lineB == 1)&&(lineC == 0)&&(mulai == 1)){
  lapb++;
  delay(500);
  }

  if((lineA == 0)&&(lineB == 0)&&(lineC == 1)&&(mulai == 1)){
  lapc++;
  lapa++;
  delay(500);
  }

  if((lineA == 0)&&(lineB == 1)&&(lineC == 0)&&(mulai == 1)){
  lapc++;  
  lapb++;
  delay(500);
  }

  if((lineA == 1)&&(lineB == 0)&&(lineC == 0)&&(mulai == 1)){
  lapa++;
  lapb++;
  delay(500);
  }

  if((lineA == 0)&&(lineB == 0)&&(lineC == 0)&&(mulai == 1)){
  lapc++;
  lapa++;
  lapb++;
  delay(500);
  }
}

//lap 2
if ((lapa == 1) || (lapb == 1) || (lapc == 1) && (mulai == 1)){
  if((lineA == 0)&&(lineB == 1)&&(lineC == 1)&&(mulai == 1)){
  lapb++;
  delay(500);
  }

  if((lineA == 1)&&(lineB == 0)&&(lineC == 1)&&(mulai == 1)){
  lapc++;
  delay(500);
  }

  if((lineA == 1)&&(lineB == 1)&&(lineC == 0)&&(mulai == 1)){
  lapa++;
  delay(500);
  }

  if((lineA == 0)&&(lineB == 0)&&(lineC == 1)&&(mulai == 1)){
  lapb++;
  lapc++;
  delay(500);
  }

  if((lineA == 0)&&(lineB == 1)&&(lineC == 0)&&(mulai == 1)){
  lapb++;
  lapa++;
  delay(500);
  }

  if((lineA == 1)&&(lineB == 0)&&(lineC == 0)&&(mulai == 1)){
  lapc++;
  lapa++;
  delay(500);
  }

  if((lineA == 0)&&(lineB == 0)&&(lineC == 0)&&(mulai == 1)){
  lapa++;
  lapb++;
  lapc++;
  delay(500);
  }
}

//lap 3
if ((lapa == 2) || (lapb == 2) || (lapc == 2) && (mulai == 1)){
  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);
lineB = digitalRead(3);
lineC = digitalRead(4);

if((lineA == 0)&&(mulai == 0)){
  delay(200);
  mulai = 1;
  start_time = millis();  
}

if((lineB == 0)&&(mulai == 0)){
  delay(200);
  mulai = 1;
  start_time = millis();  
} 

if((lineC == 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();  
}

Hi,
So you don't have three tracks, just one track inter-twined.
You will need some way of identifying each 4WD vehicle because they will for a start be able to overtake each other.

Tom.. :grinning: :+1: :coffee: :australia:

i have track with 3 lane like my draw, for overtake i think no
because if 1 car faster than other, the faster car will crash with slower 1

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.