Spot the noob!

Hello folks - am just starting at this and was wondering if any of you fine people could help. I have the basic code working - it all looks lovely However the complicated stuff, like setting up 2 timers, is way beyond me. Was wondering if any of you fine people (shameless flattery) can give a little advise.

I have had to use a lcd keypad shield, as I solder as well as a baboon with a hangover.

Trying to set up a domination cube for work, here is my code (what there is of it)

#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int readkey;
void setup() {
  lcd.begin(16, 2);
  lcd.print("Trigger Happy");
}
void loop() {
  
  lcd.setCursor(0, 1);
  lcd.print("Domination");
  lcd.setCursor(12,1);           
  lcd.print(millis()/1000);      
  readkey=analogRead(0);
  if (readkey<50) {
  lcd.clear();
  lcd.print("Red Team");
  }
  else if(readkey<790) {
    lcd.clear();
    lcd.print("Blue Team");
  }
}

Hoping anyone can provide some help…

The hungover baboon

No idea what you're talking about. What are the two timers supposed to do?

basically just count how long each button is pressed for.

Below loop() implementation can give you some ideas; you might want to modify the logic.

void loop() {

  // start time when buttons were pressed
  static unsigned long starttimeRed = 0;
  static unsigned long starttimeBlue = 0;

  // total time that buttons were pressed
  unsigned long totaltimeRed;
  unsigned long totaltimeBlue;
  

  lcd.setCursor(0, 1);
  lcd.print("Domination");
  lcd.setCursor(12, 1);
  lcd.print(millis() / 1000);
  readkey = analogRead(0);

  // if red pressed
  if (readkey < 50)
  {
    lcd.clear();
    lcd.print("Red Team");

    // if no starttime set, set it to current time (millis())
    if (starttimeRed == 0)
    {
      starttimeRed = millis();
    }
  }
  // if blue pressed
  else if (readkey < 790)
  {
    lcd.clear();
    lcd.print("Blue Team");

    // if no starttime set, set it to current time (millis())
    if (starttimeBlue == 0)
    {
      starttimeBlue = millis();
    }
  }
  // nothing or anything else pressed
  else
  {
    // calculate total time
    totaltimeRed = millis() - starttimeRed;
    totaltimeBlue = millis() - starttimeBlue;

    // reset start times
    starttimeRed = 0;
    starttimeBlue = 0;
  }
}

Wow that looks like it will do the job - how do i display each timer?

Some lcd.print? Time is in milliseconds, so you might want to divide by 1000 for seconds.

Thank you so much for the help, all i need to do now is:

Dont start the clock, until the first button is pressed,

and when one button is pressed stop the other clock…

Is this doable?

//The code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int readkey;
void setup() {
  lcd.begin(16, 2);
  lcd.print("Trigger Happy");
  lcd.clear();
}
void loop() {

  // start time when buttons were pressed
  static unsigned long starttimeRed = 0;
  static unsigned long starttimeBlue = 0;

  // total time that buttons were pressed
  unsigned long totaltimeRed;
  unsigned long totaltimeBlue;
  lcd.setCursor(0, 0);
  lcd.print("Blue");
  lcd.setCursor(9, 0);
  lcd.print("Red");  

  readkey = analogRead(0);

  // if red pressed
  if (readkey < 50)
  {
   // if no starttime set, set it to current time (millis())
      starttimeRed = millis();
    }
  
  // if blue pressed
  else if (readkey < 790)
  {
    // if no starttime set, set it to current time (millis())
    if (starttimeBlue == 0)
    {
      starttimeBlue = millis();
    }
  }
  // nothing or anything else pressed
  else
  {
    // calculate total time
    totaltimeRed = millis() - starttimeRed;      
    totaltimeBlue = millis() - starttimeBlue;
     lcd.setCursor(0, 1);
    lcd.print(totaltimeRed / 100);
     lcd.setCursor(9, 1);
    lcd.print(totaltimeBlue / 100);

  }
}

Yes, that is doable. But you must be more clear in your description.

If red presses button, must red time stsrt or must blue time start. When do you want to display.

Once you have defined that for yourself, you should be able to figure out what to do where. The 'else' block that I added initially might no longer be required.

Yea, basically I want one team to press a button that starts their time and pauses the other, a little bit like a chess clock.

Ok so close to what I need. I have the display working nicely, and have two counters thanks to sterretje, I have my buttons working for the two counters, however…

The clocks both start counting straight away - I need one clock start with one button push, then when the other button is pressed, stop the first clock and start the second…

#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int readkey;
void setup() {
  lcd.begin(16, 2);
  lcd.print("Trigger Happy");
  lcd.clear();
}
void loop() {

  // start time when buttons were pressed
  static unsigned long starttimeRed = 0;
  static unsigned long starttimeBlue = 0;

  // total time that buttons were pressed
  unsigned long totaltimeRed;
  unsigned long totaltimeBlue;
  lcd.setCursor(0, 0);
  lcd.print("Blue");
  lcd.setCursor(9, 0);
  lcd.print("Red");  

  readkey = analogRead(0);

  // if red pressed
  if (readkey < 50)
  {
   // if no starttime set, set it to current time (millis())
      starttimeRed = millis();
    }
  
  // if blue pressed
  else if (readkey < 790)
  {
    // if no starttime set, set it to current time (millis())
    if (starttimeBlue == 0)
    {
      starttimeBlue = millis();
    }
  }
  // nothing or anything else pressed
  else
  {
    // calculate total time
    totaltimeRed = millis() - starttimeRed;      
    totaltimeBlue = millis() - starttimeBlue;
     lcd.setCursor(0, 1);
    lcd.print(totaltimeRed / 1000);
     lcd.setCursor(9, 1);
    lcd.print(totaltimeBlue / 1000);

  }
}

If I can get this working I will then put a delay on each button - so they will have to hold it in for ten seconds before it switches - Basically I will have three of these on site - so we can play a game of domination - just like in call of duty.