Scoreboard Project

So ive been developing a scoreboard project which so far ive gotten on well. however im now suck,

ok 3x buttons

1 to set score on team 1
1 to set score on team 2
1 to set target score.

ok so all buttons work, and when you get to the defined limit the program displays the winner on the lcd.

however this is currently set in the program, what id like to do is use the target score button to be able to set the score for each team to count up/down until team x have won

here is my code

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int resetBtn = 22;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\
// TEAM 1 BUTTON + BUTTON STATE //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\
const byte Team1 = A11;
//const byte Team1remove = A12;
byte Team1ButtonState;
byte Team1lastState = LOW;
byte Team1count = 255;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\
// TEAM 2 BUTTON + BUTTON STATE //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\
const byte Team2 = A13;
byte Team2ButtonState;
byte Team2lastState = LOW;
byte Team2count = 255;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\
//          Set Target          //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\
const byte Target1 = A12;
byte TargetButtonState;
byte TargetlastState = LOW;
byte Targetcount = 255;

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  //  pinMode(LED, OUTPUT);
  pinMode(Team1, INPUT_PULLUP);
//  pinMode(Team1remove, INPUT_PULLUP);
  pinMode(Team2, INPUT_PULLUP);
  pinMode(Target1, INPUT_PULLUP);
  lcd.setCursor(0, 0); // set lcd to print to line 1
  lcd.println(" Skip Ball Goal ");
  lcd.setCursor(0, 1); // set lcd to print to line 1
  lcd.println("     Keeper     ");
  delay(5000);
  lcd.clear();
}

void loop() {
  
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\
  //                TEAM 1 COUNTER                        //
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\
  Team1ButtonState = digitalRead(Team1);

  if (Team1ButtonState && Team1ButtonState != Team1lastState) // button latch, no debounce needed.
  {
    if (Team1count < 255) // This will check to see if the count is within a range of 0 - 255, and anything over that, it will reset count back to 0. Of course, this will happen anyways because count is a BYTE, and not an int or any other type.
      Team1count += 1; // same as count = count + 1;
    else
      Team1count = 0;

    lcd.setCursor(0, 0); // set lcd to print to line 1

    lcd.print("Team 1        ");
    lcd.print(Team1count);
    Serial.println(Team1count);
  }
  Team1lastState = Team1ButtonState;
  if (Team1count > 25)
    winner1();

  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\
  //                TEAM 2 COUNTER                        //
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\
  Team2ButtonState = digitalRead(Team2);

  if (Team2ButtonState && Team2ButtonState != Team2lastState) // button latch, no debounce needed.
  {
    if (Team2count < 255) // This will check to see if the count is within a range of 0 - 255, and anything over that, it will reset count back to 0. Of course, this will happen anyways because count is a BYTE, and not an int or any other type.
      Team2count += 1; // same as count = count + 1;
    else
      Team2count = 0;
    lcd.setCursor(0, 1); // set lcd to print to line 1
    lcd.print("Team 2        ");
    lcd.print(Team2count);
    Serial.println(Team2count);
  }
  Team2lastState = Team2ButtonState;
  if (Team2count > 25)
    winner2();

  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\
  //                     SET TARGET                       //
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\
  TargetButtonState = digitalRead(Target1);

  if (TargetButtonState && TargetButtonState != TargetlastState) // button latch, no debounce needed.
  {
    if (Targetcount < 255) // This will check to see if the count is within a range of 0 - 255, and anything over that, it will reset count back to 0. Of course, this will happen anyways because count is a BYTE, and not an int or any other type.
      Targetcount += 1; // same as count = count + 1;
    else
      Targetcount = 0;

    lcd.setCursor(0, 0); // set lcd to print to line 1

//    lcd.print("SET TARGET    ");
//    lcd.print(Targetcount);
    Serial.println(Targetcount);
  }
  TargetlastState = TargetButtonState;
  delay(100);
  
}

  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\
  //                      WINNER 1                        //
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\
void winner1() {

  lcd.setCursor(0, 0);
  lcd.print("   TEAM 1 ARE   ");
  lcd.setCursor(0, 1);
  lcd.print("  THE  WINNERS  ");
  delay(10000);
  Team1count = 0;
  Team2count = 0;
  lcd.clear();
  lcd.setCursor(0, 0); // set lcd to print to line 1
  lcd.print("Team 1        ");
  lcd.print(Team1count);
  Serial.println(Team1count);
  lcd.setCursor(0, 1); // set lcd to print to line 1
  lcd.print("Team 2        ");
  lcd.print(Team2count);
  Serial.println(Team2count);
}
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\
  //                      WINNER 2                        //
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\
void winner2() {
  lcd.setCursor(0, 0);
  lcd.print("   TEAM 2 ARE   ");
  lcd.setCursor(0, 1);
  lcd.print("  THE  WINNERS  ");
    delay(10000);
  Team1count = 0;
  Team2count = 0;
  lcd.clear();
  lcd.setCursor(0, 0); // set lcd to print to line 1
  lcd.print("Team 1        ");
  lcd.print(Team1count);
  Serial.println(Team1count);
  lcd.setCursor(0, 1); // set lcd to print to line 1
  lcd.print("Team 2        ");
  lcd.print(Team2count);
  Serial.println(Team2count);
}

Basically i dont know how to set the code so that Team1count is more then Targetcount then next function

dont worry i just worked it out.

i used

  if (Team1count > Targetcount)
    winner1();

Fist of all remove back slashes from your comments \

Instead of this:

if (Team1count > 25)
 winner1();

if (Team2count > 25)
 winner2();

try this:

if (Team1count > Targetcount)
 winner1();

if (Team2count > Targetcount)
 winner2();

Well done for getting it working. Now, how about improving it ?

It would be neater to use arrays in the program to avoid repeating code and you don’t need two winner functions either as they are basically the same.

See trap #14 http://www.gammon.com.au/forum/?id=12153

LarryD: Fist of all remove back slashes from your comments \

Didn't even notice that when I read it! As Larry said, get rid. \ in comments cause line folding, meaning your next line is commented out.

TargetButtonState = digitalRead(Target1);

The above line is commented out because of this. As are others.

so a comment like this is this ok?

  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  //                      WINNER 2                        //
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

Yes. Knock yourself out with as many // as you like, a comment like this if fine too:

////////////////////////// Im a comment! //////////////////////////

Or

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // W I N N E R 2 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Thanks guys!!! I LOVE THIS FORUM!!!

I have now changed all of the code, the next part of my project is to ask for Target count before anything starts,

but i think I will probably invest in a 4-line LCD display or maybe a touch screen display.

Thanks once again :)

ive got one more question sorry.

ok so basically ive written the following but its not working as it should, im basically at the beging getting the arduino to check a value, if the value is 0 run x function,

x function would then ask user to use button to increase input.

void loop() {
 if (Targetcount = 0) // This will check to see if the count is within a range of 0 - 255, and anything over that, i
 SetTarget(); // goto this function
[code]

this is the fucntion 

[code]
void SetTarget() {
    if (Targetcount = 0) // This will check to see if the count is within a range of 0 - 255, and anything over that, it will reset count back to 0. Of course, this will happen anyways because count is a BYTE, and not an int or any other type.
      Targetcount += 1; // same as count = count + 1;
    else
      Targetcount = 0;
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("SET POINTS    ");
  lcd.print(Team2count);
  lcd.print(Targetcount);
}

i know its something stupid, ive been looking at this since last night thou :(

also @HelibobUK

with the winner function, i know there the same but how would i print the winner if i just used 1 function?

and what arrays could i use ? i still dont fully understand arrays even though ive done alot of reading ? they confuse the hell out of me.

if (Targetcount = 0)

This sets Targetcount to zero. I assume that you meant

if (Targetcount == 0)

with the winner function, i know there the same but how would i print the winner if i just used 1 function?

Write the function so that it is passed a parameter to determine which details it prints.

void winner(byte teamNumber)
{
  lcd.setCursor(0, 0);
  lcd.print("   TEAM ");
  lcd.print(teamNumber);
  lcd.print( "ARE   ");
  lcd.setCursor(0, 1);
  lcd.print("  THE  WINNERS  ");
  delay(10000);
  Team1count = 0;
  Team2count = 0;
  lcd.clear();
  lcd.setCursor(0, 0); // set lcd to print to line 1
  lcd.print("Team 1        ");
  lcd.print(Team1count);
  Serial.println(Team1count);
  lcd.setCursor(0, 1); // set lcd to print to line 1
  lcd.print("Team 2        ");
  lcd.print(Team2count);
  Serial.println(Team2count);
}

and what arrays could i use ?

Arrays allow you to group similar data together. First you declare how many elements are required. You have 2 teams so would require 2 elements. An example

byte scoreCount[2];

would give you 2 variables, one named scoreCount[0] and a second one named scoreCount[1] in which to save scores. NOTE that the 2 levels are numbered 0 and 1 rather than 1 and 2. The trick is that you can replace the 0 and 1 with a variable so you can write things like

for (int x = 0; x < 2; x++)
{
  Serial.println(scoreCount[x]);
}

This would print the number in each level of the array.

In your program you basically repeat the same code for each team counter. If you make the variables in that code into arrays you can use a for loop as above to run the same code twice but use different levels of the arrays for each team’s data.

so if i use

if (Targetcount == 0)

dose this mean if Targetcount = 0) then do function

where as

if (Targetcount = 0)

this will set the var to 0 ???

mathewbuer: so if i use

if (Targetcount == 0)

dose this mean if Targetcount = 0) then do function

where as

if (Targetcount = 0)

this will set the var to 0 ???

Yes.

== is the equality operator = is the assignment operator

this has been bugging me now for 3days, im still trying to get a stupid function to work,

so whats ment to happen is there is an input for a target score which is checked at the begining, if the score is zero it ask you to set a score, but for some reason its not updating ?

i know its going to be something stupid,

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int resetBtn = 22;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// TEAM 1 BUTTON + BUTTON STATE //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
const byte Team1 = A11;
//const byte Team1remove = A12;
byte Team1ButtonState;
byte Team1lastState = LOW;
byte Team1count = 255;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// TEAM 2 BUTTON + BUTTON STATE //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
const byte Team2 = A13;
byte Team2ButtonState;
byte Team2lastState = LOW;
byte Team2count = 255;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//          Set Target          //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
const byte Target1 = A12;
byte TargetButtonState;
byte TargetlastState = LOW;
byte Targetcount = 0;

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  pinMode(Team1, INPUT_PULLUP);
  pinMode(Team2, INPUT_PULLUP);
  pinMode(Target1, INPUT_PULLUP);
  lcd.setCursor(0, 0); // set lcd to print to line 1
  lcd.println(" Skip Ball Goal ");
  lcd.setCursor(0, 1); // set lcd to print to line 1
  lcd.println("     Keeper     ");
  delay(5000);
  lcd.clear();
}

void loop() {
     if (Targetcount == 0) // This will check to see if the count has been set or not
     SetTarget();

  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  //                TEAM 1 COUNTER                        //
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  Team1ButtonState = digitalRead(Team1);

  if (Team1ButtonState && Team1ButtonState != Team1lastState) // button latch, no debounce needed.
  {
    if (Team1count < 255) 
      Team1count += 1; // same as count = count + 1;
    else
      Team1count = 0;

    lcd.setCursor(0, 0); // set lcd to print to line 1

    lcd.print("Team 1        ");
    lcd.print(Team1count);
    Serial.println(Team1count);
  }
  Team1lastState = Team1ButtonState;
  if (Team1count > Targetcount)
    winner1();

  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  //                TEAM 2 COUNTER                        //
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  Team2ButtonState = digitalRead(Team2);

  if (Team2ButtonState && Team2ButtonState != Team2lastState) // button latch, no debounce needed.
  {
    if (Team2count < 255) 
      Team2count += 1; // same as count = count + 1;
    else
      Team2count = 0;
    lcd.setCursor(0, 1); // set lcd to print to line 1
    lcd.print("Team 2        ");
    lcd.print(Team2count);
    Serial.println(Team2count);
  }
  Team2lastState = Team2ButtonState;
  if (Team2count > Targetcount)
    winner2();

  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  //                     SET TARGET                       //
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  TargetButtonState = digitalRead(Target1);

  if (TargetButtonState && TargetButtonState != TargetlastState) // button latch, no debounce needed.
  {
    if (Targetcount < 255) 
      Targetcount += 1; // same as count = count + 1;
    else
      Targetcount = 0;

    lcd.setCursor(0, 0); // set lcd to print to line 1

    //    lcd.print("SET TARGET    ");
    //    lcd.print(Targetcount);
    Serial.println(Targetcount);
  }
  TargetlastState = TargetButtonState;
  delay(100);

}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//                      WINNER 1                        //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
void winner1() {

  lcd.setCursor(0, 0);
  lcd.print("   TEAM 1 ARE   ");
  lcd.setCursor(0, 1);
  lcd.print("  THE  WINNERS  ");
  delay(10000);
  Team1count = 0;
  Team2count = 0;
  Targetcount = 0;
  lcd.clear();
  lcd.setCursor(0, 0); // set lcd to print to line 1
  lcd.print("Team 1        ");
  lcd.print(Team1count);
  Serial.println(Team1count);
  lcd.setCursor(0, 1); // set lcd to print to line 1
  lcd.print("Team 2        ");
  lcd.print(Team2count);
  Serial.println(Team2count);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//                      WINNER 2                        //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
void winner2() {
  lcd.setCursor(0, 0);
  lcd.print("   TEAM 2 ARE   ");
  lcd.setCursor(0, 1);
  lcd.print("  THE  WINNERS  ");
  delay(10000);
  Team1count = 0;
  Team2count = 0;
  Targetcount = 0;
  lcd.clear();
  lcd.setCursor(0, 0); // set lcd to print to line 1
  lcd.print("Team 1        ");
  lcd.print(Team1count);
  Serial.println(Team1count);
  lcd.setCursor(0, 1); // set lcd to print to line 1
  lcd.print("Team 2        ");
  lcd.print(Team2count);
  Serial.println(Team2count);
}

void SetTarget() {
    if (Targetcount = 0) 
      Targetcount += 1; // same as count = count + 1;
    else
      Targetcount = 0;
  lcd.clear();
  lcd.setCursor(0,0);
 lcd.print("SET POINTS   ");
  lcd.print(Targetcount);
}

can someone see what im doing wrong i know its going to be something stupid

void SetTarget() {
    if (Targetcount = 0) 
      Targetcount += 1; // same as count = count + 1;
    else
      Targetcount = 0;

Haven't we just been over the difference between equality ( == ) operator and assignment (=) operator ? ...

we have, i get that

this means set Targetcount to 0

Targetcount = 0;

and this means if Targetcount = 0

Targetcount == 0

because this bit here works,

     if (Targetcount == 0) // This will check to see if the count has been set or not
     SetTarget();

however for some reason the count dose not go up

Read the code I posted, it came straight from your program. Tell me, should you be using == or = in that situation?