Hello,
Karting is my hobby. For organizing kartraces people asked me to make a pit timer systeem. I thought it would be a nice and manageble Arduino project. I have selected the different components and asked someone from Fiver to program the code (im not good in that). But now i'm stuck and cannot finish it. Can somebody help me please?
I have added a presentation (see attachment) in which i explain the problem i'm facing. It describes what is happening now (with the code, connections and components) and what should happen. The code can be found below.
Thank you in advance!
Greetings Frits
The code is:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
unsigned long rpm_time = millis();
int timer = 5; //set to 5sec,
unsigned long timer_var = 0;
unsigned long timer_demo = 0;
unsigned long printing = 0;
//unsigned long update_time = millis();
//unsigned long calibration = 4000;
//int x = 0;
int kart_check = 0;
int flg = 0;
const int Rgreen = 7;
const int Rred = 6;
const int inc = 3;
const int dec = 4;
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = HIGH; // the previous reading from the input pin
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
int check = 0;
// Variables will change:
int ledState1 = HIGH; // the current state of the output pin
int buttonState1; // the current reading from the input pin
int lastButtonState1 = HIGH; // the previous reading from the input pin
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime1 = 0; // the last time the output pin was toggled
unsigned long debounceDelay1 = 50; // the debounce time; increase if the output flickers
int check1 = 0;
void setup()
{
Serial.begin(9600);
Serial.println(" Kart Simple Timer Check");
attachInterrupt(0, RPMCount, RISING);
pinMode(Rgreen, OUTPUT);
pinMode(Rred, OUTPUT);
digitalWrite(Rgreen,LOW); //green light ON in starting
digitalWrite(Rred,HIGH); //red light OFF in starting
pinMode(inc, INPUT_PULLUP); // enabling internal pull-up resistor
pinMode(dec, INPUT_PULLUP); // enabling internal pull-up resistor
lcd.begin();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Kart System");
}
void loop()
{
debounce();
debounce1();
if(millis()>= printing + 1000){
printing = millis();
Serial.print("Default Timer(sec): ");
Serial.println(timer);
lcd.setCursor(0,0);
lcd.print("Timer: ");
lcd.print(timer);
}
if(millis() <= (timer_var)){
digitalWrite(Rred,LOW); //red light ON
digitalWrite(Rgreen,HIGH); //green light OFF
if(millis() >= printing + 1000){
printing = millis();
Serial.println("Red Signal ON ");
Serial.print("Time Left(sec): ");
Serial.println((timer_var - millis())/1000);
}
}
else{
digitalWrite(Rgreen,LOW); //green light ON
digitalWrite(Rred,HIGH); //red light OFF
if(millis() >= printing + 1500){
printing = millis();
Serial.println("Green Signal ON ");
}
//x = 0;
kart_check = 0;
flg = 0;
Serial.println("Flag Reset");
}
// if(millis() >= (calibration + update_time)){
// update_time = millis();
if((kart_check == 1) && (flg == 0)){
Serial.println("New Kart Entry ");
// delay(3000); //addition of 3sec delay
rpm_time = millis();
timer_var = rpm_time + (timer*1000) ;
// if (timer_demo > rpm_time ){
// timer_var = (((rpm_time + (timer*1000)) - (timer_demo - rpm_time)) + (rpm_time + (timer*1000))); //((set time - time left)+ set time) ) ;
// }
timer_demo = timer_var;
Serial.print("Total Time(sec): ");
Serial.println((timer_var - rpm_time)/1000);
//x = 0;
flg = 1;
}
//}
}
void RPMCount()
{
Serial.println("Y");
// x = 1;
kart_check = 1 ;
}
void debounce(){
// read the state of the switch into a local variable:
int reading = digitalRead(inc);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
check = !check;
timer = timer + 1;
Serial.print("New Timer(sec): ");
Serial.println(timer);
lcd.setCursor(0,0);
lcd.print("New Timer: ");
lcd.print(timer);
ledState = !ledState;
}
}
}
//Serial.print("check: "); Serial.println(check);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
}
void debounce1(){
// read the state of the switch into a local variable:
int reading1 = digitalRead(dec);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading1 != lastButtonState1) {
// reset the debouncing timer
lastDebounceTime1 = millis();
}
if ((millis() - lastDebounceTime1) > debounceDelay1) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading1 != buttonState1) {
buttonState1 = reading1;
// only toggle the LED if the new button state is HIGH
if (buttonState1 == HIGH) {
check1 = !check1;
ledState1 = !ledState1;
timer = timer - 1;
Serial.print("New Timer(sec): ");
Serial.println(timer);
lcd.setCursor(0,0);
lcd.print("New Timer: ");
lcd.print(timer);
}
}
}
Serial.print("check1: "); Serial.println(check1);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState1 = reading1;
}
Timer pits 2019.pdf (1.52 MB)
Pittimer.ino (6.56 KB)