Hi everyone.
I literally purchased my first Arduino Nano this week and want to add some functionality on top of what I have. NOOB ALERT!
I have made a stopwatch from the project hub:
I have the stopwatch project working perfectly.
What I'd like to do.
-
Id like to have a second Arduino running the same stopwatch project. Then:
-
Id like to link the two together in some sort of way and add coding to trigger a one of two lights depending on which stopwatch finishes first...
Summary
In simple terms, i want to run a 2 lane drag race with 2 stop watch projects and light up whichever lane finishes first. The reason im running 2 projects is so i can separate the 2 units to run single lane races in separate locations and not have the code affected by running 1 unit. The 'win' light will need to remain on until the reset button is pressed. (the current code already has a reset button coded and wired)
Keeping in mind i only have D7, 12, 13 available on my Arduino NANO
I didn't write the below code, it came from the project hub. It currently works as intended.
Thanks!
#include <Bounce2.h>
/*
* Arduino Code for Stopwatch using arduino nano or arduino uno.
* coded by Shashwat Raj.
* Arduino Project Hub ID :- shashwatraj98765
*/
#include <LiquidCrystal.h>
#include <Bounce2.h>
LiquidCrystal lcd(6,5,4,8,9,10,11);
const byte ledPin = 13;
const byte startButton = 2;
Bounce startDebouncer1 = Bounce();
const byte resetButton = 3;
Bounce resetDebouncer2 = Bounce();
void setup()
{
pinMode(startButton, INPUT_PULLUP);
startDebouncer1.attach(startButton);
startDebouncer1.interval(5);
pinMode(resetButton, INPUT_PULLUP);
resetDebouncer2.attach(resetButton);
resetDebouncer2.interval(5);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Stop");
lcd.setCursor(0, 1);
lcd.print("Watch");
delay(4000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Black-Start/Stop");
lcd.setCursor(0, 1);
lcd.print("Red -Reset");
}
bool startState = LOW;
bool resetState = LOW;
unsigned long startMillis;
unsigned long currentMillis;
unsigned long elapsedMillis;
void loop() {
// Update the Bounce instances :
startDebouncer1.update();
if ( startDebouncer1.fell() ) { // Call code if button transitions from HIGH to LOW
startState = !startState; // Toggle start button state
startMillis = millis();
}
if (startState)
{
currentMillis = millis();
elapsedMillis = (currentMillis - startMillis);
lcd.setCursor(0, 0);
lcd.print("SW (hh:mm:ss:ms)");
lcd.setCursor(0, 1);
lcd.print(" ");
unsigned long durMS = (elapsedMillis%1000); //Milliseconds
unsigned long durSS = (elapsedMillis/1000)%60; //Seconds
unsigned long durMM = (elapsedMillis/(60000))%60; //Minutes
unsigned long durHH = (elapsedMillis/(3600000)); //Hours
durHH = durHH % 24;
String durMilliSec = timeMillis(durHH, durMM, durSS,durMS);
lcd.setCursor(0, 1);
lcd.print(durMilliSec);
delay(150);
}
resetDebouncer2.update();
if (resetDebouncer2.fell())
{
resetState = HIGH;
}
if (resetState)
{
// clear screen for the next loop:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Black-Start/Stop");
lcd.setCursor(0, 1);
lcd.print("Red -Reset");
delay(100);
resetState = LOW;
}
}
String timeMillis(unsigned long Hourtime,unsigned long Mintime,unsigned long Sectime,unsigned long MStime)
{
String dataTemp = "";
if (Hourtime < 10)
{
dataTemp = dataTemp + "0" + String(Hourtime)+ "h:";
}
else{
dataTemp = dataTemp + String(Hourtime)+ "h:";
}
if (Mintime < 10)
{
dataTemp = dataTemp + "0" + String(Mintime)+ "m:";
}
else{
dataTemp = dataTemp + String(Mintime)+ "m:";
}
if (Sectime < 10)
{
dataTemp = dataTemp + "0" + String(Sectime)+ "s:";
}
else{
dataTemp = dataTemp + String(Sectime)+ "s:";
}
dataTemp = dataTemp + String(MStime);
return dataTemp;
}
