Integrating 2 code sets

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:

https://create.arduino.cc/projecthub/shashwatraj98765/how-to-make-stopwatch-with-arduino-6e09db?ref=user&ref_id=1535387&offset=4

I have the stopwatch project working perfectly.

What I'd like to do.

  1. Id like to have a second Arduino running the same stopwatch project. Then:

  2. 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;

}

For me the issue is that you need to understand some coding , so you can understand what that code does , be able to modify it or write your own .

Please don’t take it as Criticism but if you are just starting out , trying to do fairly complex tasks as a beginner will cause you frustration. Take some time to play , look at examples etc and learn some stuff . Use code on the net for inspiration .

10/10 for using code tags !

Good luck and enjoy it .

2 Likes

Consider doing this with one micro that will save you a lot of grief. You can use the analog inputs as digital inputs/outputs if you like. A4 and A5 are also the I2C communications. You could use an I2C adapter for the LCD, they cost about a buck. You also get A7 and A8 which is not available on the UNO.

+1 @gilshultz for just using one microprocessor.

Any need to have a single lane mode can be a simple matter, well simple enough, of some code to detect and use a single lane mode.

I can practically guarantee that it will be easier to do than figuring out the way two separate copies of all the hardware can be programmed to deal with each other in a two lane mode.

In fact, written well it would be fairly easy to have a three lane mode…

Just sayin'.

a7

2 Likes

Thanks for the replies everyone. :slight_smile:

The only reason i want to use two is so they end result is modular. Each unit needs to work as an individual and as a pair.

As far as the code goes, you're all right i do need to learn what it all means and does.

Thinking about what im trying to achieve seems simple enough in my head > detect the difference between 2 inputs (from 2 different controllers) and the output a light. The push button input already exists from the stopwatch project, so that could be doubled split to a second input. Then detected on time. This code would then slot in after then mills timer stuff is complete. Resulting in a elapsed stopwatch time and a win light on either unit

1 Like

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