BCD score keeper help

Hi all,

I can't find my first post about this topic, but I'm still stuck. I'm programming a shooting gallery and can't seem to figure out how to keep score. I have 2 BCD chips connected to 2 7-segment displays. The displays are reading 00 fine. I can't get the score to increment after each hit of a target. Can anyone please shed some light on this for me? Relevant code only included. Thanks! Not sure if I entered code correctly.

[/ /*

  
 
 */


const int A =  34;     // BCD 1A (to BCD - 7 segment IC)
const int B =  35;     // BCD 1B (to BCD - 7 segment IC)
const int C =  36;     // BCD 1C (to BCD - 7 segment IC)
const int D =  37;     // BCD 1D (to BCD - 7 segment IC)
const int AA =  38;    // BCD 2A (to BCD - 7 segment IC)
const int BB =  39;    // BCD 2B (to BCD - 7 segment IC)
const int CC =  40;    // BCD 2C (to BCD - 7 segment IC)
const int DD =  41;    // BCD 2D (to BCD - 7 segment IC)


int target2State = 0;        // variable for reading the target1 status
int target3State = 0;        // variable for reading the target2 status
int target4State = 0;        // variable for reading the target3 status
int target5State = 0;        // variable for reading the target4 status
int target6State = 0;        // variable for reading the target5 status
int target7State = 0;        // variable for reading the target6 status
int target8State = 0;        // variable for reading the target7 status
int target9State = 0;        // variable for reading the target8 status
int target10State = 0;        // variable for reading the target9 status
int target11State = 0;       // variable for reading the target10 status
int pos = 0;                 // variable to store the servo position 
int brightness = 0;          // how bright the LED is
int fadeAmount = 5;          // how many points to fade the LED by
int counter = 0;
int num_array[10][9] = {  { 0,0,0,0,0,0,0,0 },    // 0
                          { 0,0,0,0,0,0,0,1 },    // 1
                          { 0,0,0,0,0,0,1,0 },    // 2
                          { 0,0,0,0,0,0,1,1 },    // 3
                          { 0,0,0,0,0,1,0,0 },    // 4
                          { 0,0,0,0,0,1,0,1 },    // 5
                          { 0,0,0,0,0,1,1,0 },    // 6
                          { 0,0,0,0,0,1,1,1 },    // 7
                          { 0,0,0,0,1,0,0,0 },    // 8
                          { 0,0,0,0,1,0,0,1 }};   // 9



void setup() {
  
  Serial.begin(4800);        //Set to 4800 bps for sound card
  //Serial.print(0xEF); //reset mp3 player
  delay(2000);
  giraffeStepper.setSpeed(80);
  cobraservo.attach(12);     // attaches the servo on pin 12 to the servo object 
  mickeyservo.attach(13);     // attaches the servo on pin 13 to the servo object 
  for(int i = 2; i <=11; i++){
  pinMode(i, INPUT);         // initialize the target pins 2-11 as inputs:
  }
  for(int i = 22; i <=53; i++){
  pinMode(i, OUTPUT);        // initialize the attraction pins 22-53 as outputs:
  cobraservo.write(90);
  mickeyservo.write(179);   
  }
  pinMode(wheel3Pin, OUTPUT);
  pinMode(eyePin, OUTPUT);
  pinMode(fantPin, OUTPUT);
  
  digitalWrite(A, LOW);
  digitalWrite(B, LOW);
  digitalWrite(C, LOW);
  digitalWrite(D, LOW);
  digitalWrite(AA, LOW);
  digitalWrite(BB, LOW);
  digitalWrite(CC, LOW);
  digitalWrite(DD, LOW);
  delay(10);
}

void loop(){ 
  
  // read the state of the targets:
  target11State = digitalRead(target11Pin);  // START
  if (target11State == HIGH) {       //READ START BUTTON  
    Serial.print(001);
    digitalWrite(fantPin, HIGH);
    
  
  target3State = digitalRead(target3Pin);  // 
  target4State = digitalRead(target4Pin);  // Twinks
  target5State = digitalRead(target5Pin);  // Cobra
  target6State = digitalRead(target6Pin);  // Animals
  target7State = digitalRead(target7Pin);  // Cannon
  target8State = digitalRead(target8Pin);  // SR Cannoe
  target9State = digitalRead(target9Pin);  // SL Cannoe
  target10State = digitalRead(target10Pin);  // Mickey Mountain

  
  
    if (target4State == HIGH) {     //READ TWINKS
    // turn LED on:    
    for (int counter = 0; counter < 20; ++counter) 
  {
   Num_Write(counter+1); 
  }

    digitalWrite(game24Pin, LOW);
      } 
  else {

    digitalWrite(game24Pin, LOW); 
  }

}

void Num_Write(int);
  counter == 1{
  digitalWrite(A,HIGH);
  digitalWrite(B,LOW);
  digitalWrite(C,LOW);
  digitalWrite(D,LOW);
  digitalWrite(AA,LOW);
  digitalWrite(BB,LOW);
  digitalWrite(CC,LOW);
  digitalWrite(DD,LOW);  
  }
  counter == 2{
  digitalWrite(A,LOW);
  digitalWrite(B,HIGH);
  digitalWrite(C,LOW);
  digitalWrite(D,LOW);
  digitalWrite(AA,LOW);
  digitalWrite(BB,LOW);
  digitalWrite(CC,LOW);
  digitalWrite(DD,LOW);  
  }  
  counter == 3{
  digitalWrite(A,HIGH);
  digitalWrite(B,HIGH);
  digitalWrite(C,LOW);
  digitalWrite(D,LOW);
  digitalWrite(AA,LOW);
  digitalWrite(BB,LOW);
  digitalWrite(CC,LOW);
  digitalWrite(DD,LOW);  
  }  
  counter == 4{
  digitalWrite(A,LOW);
  digitalWrite(B,LOW);
  digitalWrite(C,HIGH);
  digitalWrite(D,LOW);
  digitalWrite(AA,LOW);
  digitalWrite(BB,LOW);
  digitalWrite(CC,LOW);
  digitalWrite(DD,LOW);  
  }  
  counter == 5{
  digitalWrite(A,HIGH);
  digitalWrite(B,LOW);
  digitalWrite(C,HIGH);
  digitalWrite(D,LOW);
  digitalWrite(AA,LOW);
  digitalWrite(BB,LOW);
  digitalWrite(CC,LOW);
  digitalWrite(DD,LOW);  
  }  
  counter == 6{
  digitalWrite(A,LOW);
  digitalWrite(B,HIGH);
  digitalWrite(C,HIGH);
  digitalWrite(D,LOW);
  digitalWrite(AA,LOW);
  digitalWrite(BB,LOW);
  digitalWrite(CC,LOW);
  digitalWrite(DD,LOW);  
  }  
  counter == 7{
  digitalWrite(A,HIGH);
  digitalWrite(B,HIGH);
  digitalWrite(C,HIGH);
  digitalWrite(D,LOW);
  digitalWrite(AA,LOW);
  digitalWrite(BB,LOW);
  digitalWrite(CC,LOW);
  digitalWrite(DD,LOW);  
  }  
  counter == 8{
  digitalWrite(A,LOW);
  digitalWrite(B,LOW);
  digitalWrite(C,LOW);
  digitalWrite(D,HIGH);
  digitalWrite(AA,LOW);
  digitalWrite(BB,LOW);
  digitalWrite(CC,LOW);
  digitalWrite(DD,LOW);  
  }  
  counter == 9{
  digitalWrite(A,HIGH);
  digitalWrite(B,LOW);
  digitalWrite(C,LOW);
  digitalWrite(D,HIGH);
  digitalWrite(AA,LOW);
  digitalWrite(BB,LOW);
  digitalWrite(CC,LOW);
  digitalWrite(DD,LOW);  
  }  
  counter == 10{
  digitalWrite(A,LOW);
  digitalWrite(B,LOW);
  digitalWrite(C,LOW);
  digitalWrite(D,LOW);
  digitalWrite(AA,HIGH);
  digitalWrite(BB,LOW);
  digitalWrite(CC,LOW);
  digitalWrite(DD,LOW);  
  }  
  counter == 11{
  digitalWrite(A,HIGH);
  digitalWrite(B,LOW);
  digitalWrite(C,LOW);
  digitalWrite(D,LOW);
  digitalWrite(AA,HIGH);
  digitalWrite(BB,LOW);
  digitalWrite(CC,LOW);
  digitalWrite(DD,LOW);  
  }  
  }
}
code]

I can't find my first post about this topic

It's here
If you click on your username, you'll see your profile details with a control labelled "Show posts" - that's how you can find previous posts

void Num_Write(int);
  counter == 1{

Didn't the compiler complain about that?

Hello,

I really think you should look into the AND Operator (&).
If you have a value in Counter there is no reason to write tons of code within a switch case construct (which is probably what you are trying to do with the counter == 1 stuff). You could write it once instead of x times.

digitalWrite(A, counter & B00000001);
digitalWrite(B, counter & B00000010); etc.

You may also want to examine the code with "int num_array[10][9] = { { 0,0,0,0,0,0,0,0 }, // 0".
An integer value takes multiple Bytes to store and your values are only 0 or 1. Byte instead of int would be more efficient. Using the AND Operator may make the array completely unnecessary.

Thanks for the help with the forum and the question. The previous code was a combination of several solutions presented on line. None of them worked. Here is a stripped down version of what I have going right now. The compiler doesn't like the "digitalWrite(Score) line. What am I doing wrong please?!

void loop(){ 
  
const int target11Pin = 11;   // START target pin

const int A =  34;     // BCD 1A (to BCD - 7 segment IC)
const int B =  35;     // BCD 1B (to BCD - 7 segment IC)
const int C =  36;     // BCD 1C (to BCD - 7 segment IC)
const int D =  37;     // BCD 1D (to BCD - 7 segment IC)
const int AA =  38;    // BCD 2A (to BCD - 7 segment IC)
const int BB =  39;    // BCD 2B (to BCD - 7 segment IC)
const int CC =  40;    // BCD 2C (to BCD - 7 segment IC)
const int DD =  41;    // BCD 2D (to BCD - 7 segment IC)

int target11State = 0;       // variable for reading the target10 status
int Score = 0;

void loop(){ 

  target11State = digitalRead(target11Pin);  // READ TARGET 
if (target11State == HIGH){                 //TARGET IS HIT
Score = Score++;                          //INCREMENT SCORE BY 1
digitalWrite(Score);
}
}
void Score()
  
if (Score == 1) {
  digitalWrite(A, HIGH);
  digitalWrite(A, LOW);
  digitalWrite(A, LOW);
  digitalWrite(A, LOW);
}
if (Score == 2) {
  digitalWrite(A, LOW);
  digitalWrite(A, HIGH);
  digitalWrite(A, LOW);
  digitalWrite(A, LOW);
}
if (Score ==3) {
  digitalWrite(A, HIGH);
  digitalWrite(A, HIGH);
  digitalWrite(A, LOW);
  digitalWrite(A, LOW);
}
//AND SO ON UNTILL 20

You have loop() and inside that you have defined a new loop() as well as a Score() function.

You can not define functions inside other functions and you can only have one loop() function.

Score = Score++;
Trap#8

.

Thanks for the Trap#8 tip. Apologies for the typo of the added loop. That's not actually in the code. Here's the code as it appears now. Compiler doesn't accept digitalWrite(Score). Any extra help would be appreciated.

const int target11Pin = 11;   // START target pin

const int A =  34;     // BCD 1A (to BCD - 7 segment IC)
const int B =  35;     // BCD 1B (to BCD - 7 segment IC)
const int C =  36;     // BCD 1C (to BCD - 7 segment IC)
const int D =  37;     // BCD 1D (to BCD - 7 segment IC)
const int AA =  38;    // BCD 2A (to BCD - 7 segment IC)
const int BB =  39;    // BCD 2B (to BCD - 7 segment IC)
const int CC =  40;    // BCD 2C (to BCD - 7 segment IC)
const int DD =  41;    // BCD 2D (to BCD - 7 segment IC)

int target11State = 0;       // variable for reading the target10 status
int Score = 0;

void loop(){ 

  target11State = digitalRead(target11Pin);  // READ TARGET 
if (target11State == HIGH){                 //TARGET IS HIT
Score++;                          //INCREMENT SCORE BY 1
digitalWrite(Score);
}
}
void Score()
  
if (Score == 1) {
  digitalWrite(A, HIGH);
  digitalWrite(A, LOW);
  digitalWrite(A, LOW);
  digitalWrite(A, LOW);
}
if (Score == 2) {
  digitalWrite(A, LOW);
  digitalWrite(A, HIGH);
  digitalWrite(A, LOW);
  digitalWrite(A, LOW);
}
if (Score ==3) {
  digitalWrite(A, HIGH);
  digitalWrite(A, HIGH);
  digitalWrite(A, LOW);
  digitalWrite(A, LOW);
}

You're missing the opening and the closing brace of the Score() function for starters. You also have an Score() function and a variable score; rename one of them.

Oh, and this will more than likely also not do what you think it does (whatever that is).

    digitalWrite(Score);

If your code doesn't compile, say so and post the complete set of error messages you get too.

Trying to increment Score and send to BCD ICs.

const int target11Pin = 11;   // START target pin

const int A =  34;     // BCD 1A (to BCD - 7 segment IC)
const int B =  35;     // BCD 1B (to BCD - 7 segment IC)
const int C =  36;     // BCD 1C (to BCD - 7 segment IC)
const int D =  37;     // BCD 1D (to BCD - 7 segment IC)
const int AA =  38;    // BCD 2A (to BCD - 7 segment IC)
const int BB =  39;    // BCD 2B (to BCD - 7 segment IC)
const int CC =  40;    // BCD 2C (to BCD - 7 segment IC)
const int DD =  41;    // BCD 2D (to BCD - 7 segment IC)


int target11State = 0;       // variable for reading the target10 status

void setup() {
  }

void loop(){ 
  
  target11State = digitalRead(target11Pin);  // READ TARGET 
if (target11State == HIGH){                      //TARGET IS HIT
Score+1;                                                  //INCREMENT SCORE BY 1
}
}

void Score(){
  
if (Score == 1) {
  digitalWrite(A, HIGH);
  digitalWrite(A, LOW);
  digitalWrite(A, LOW);
  digitalWrite(A, LOW);
}
if (Score == 2) {
  digitalWrite(A, LOW);
  digitalWrite(A, HIGH);
  digitalWrite(A, LOW);
  digitalWrite(A, LOW);
}
if (Score == 3) {
  digitalWrite(A, HIGH);
  digitalWrite(A, HIGH);
  digitalWrite(A, LOW);
  digitalWrite(A, LOW);
}
}
//AND SO ON UNTILL 20

Does not compile: ISO C++ forbids comparison between pointer and integer [-fpermissive]

Look at these:
Score+1

void Score()

if (Score == 1) {

Where/what is Score defined?

.

As I said in my previous post, you have a variable Score and a function Score; that does not work and you must rename one of them.

Change void Score() to void showScore()

void loop()
{

  target11State = digitalRead(target11Pin);  // READ TARGET
  if (target11State == HIGH)                       //TARGET IS HIT
  {
    Score + 1;                                                //INCREMENT SCORE BY 1
  }
}

void showScore()
{
  ...
  ...
}

Now there are two problems. Score + 1 does not do anything. If you want to increment the score you can use one of the three below

Score++;
Score += 1;
Score = Score + 1;

And you do not call the Score (or the renamed version) function.

So your loop() can now be something like

void loop()
{
  target11State = digitalRead(target11Pin);  // READ TARGET
  if (target11State == HIGH)                       //TARGET IS HIT
  {
    Score += 1;                                               //INCREMENT SCORE BY 1
    showScore();
  }
}

Note: I strongly suggest that you start read up on the C language; you don't seem to understand the ideas (no offense intended). E.g. start with the old bible: Kernighan and Ritchie's 'The C programming language (second edition)'. Note that this covers the basics of C (and not C++) but for now that does not really matter.

Thanks for the advice. Yes, I am an electronics engineer not a programmer. Trying though! My code (the relevant portion)now looks like this:

const int target11Pin = 11;   // START target pin

const int A =  34;     // BCD 1A (to BCD - 7 segment IC)
const int B =  35;     // BCD 1B (to BCD - 7 segment IC)
const int C =  36;     // BCD 1C (to BCD - 7 segment IC)
const int D =  37;     // BCD 1D (to BCD - 7 segment IC)
const int AA =  38;    // BCD 2A (to BCD - 7 segment IC)
const int BB =  39;    // BCD 2B (to BCD - 7 segment IC)
const int CC =  40;    // BCD 2C (to BCD - 7 segment IC)
const int DD =  41;    // BCD 2D (to BCD - 7 segment IC)

int Score = 0;
int target11State = 0;       // variable for reading the target10 status

void setup() {
  }

void loop(){ 
  
  target11State = digitalRead(target11Pin);  // READ TARGET 
if (target11State == HIGH){                 //TARGET IS HIT
Score++;                          //INCREMENT SCORE BY 1
showScore();
}

void showScore(){
  
if (Score == 1) {
  digitalWrite(A, HIGH);
  digitalWrite(A, LOW);
  digitalWrite(A, LOW);
  digitalWrite(A, LOW);
}
if (Score == 2) {
  digitalWrite(A, LOW);
  digitalWrite(A, HIGH);
  digitalWrite(A, LOW);
  digitalWrite(A, LOW);
}
if (Score == 3) {
  digitalWrite(A, HIGH);
  digitalWrite(A, HIGH);
  digitalWrite(A, LOW);
  digitalWrite(A, LOW);
}
}
//AND SO ON UNTILL 20

Compiler says "'showScore' was not declared in this scope". How can I make the link from Score to BCD output?

You're missing a closing curly somewhere by the looks of it; on a tablet so I can't really check but probably at the end of loop().

if (Score == 1) {

digitalWrite(A, HIGH);
  digitalWrite(A, LOW);
  digitalWrite(A, LOW);
  digitalWrite(A, LOW);
}

Not good. You are writing to the same pin too many times in a row. I don't think that's what you want.

My solution would be to use a system like this:

int leftover = Score;

if (leftover >= 80) {
  digitalWrite(DD, HIGH);
  leftover -= 80;
}
else {
  digitalWrite(DD, LOW);
}

if (leftover >= 40) {
  digitalWrite(CC, HIGH);
  leftover -= 40;
}
else {
  digitalWrite(CC, LOW);
}

if (leftover >= 20) {
  digitalWrite(BB, HIGH);
  leftover -= 20;
}
else {
  digitalWrite(BB, LOW);
}

// ... and so forth

The principle is that each pin is "worth" a certain number of points, so all you have to do is select pins that together are "worth" the desired number of points and set them HIGH. So, for 15 points, you would select pins AA (10 points), C (4 points), and A (1 point), and set them HIGH and all the other pins LOW. The algorithm goes like this:

Example: Show a score of 15.
15 points remaining. Pin DD is worth 80, which is too much, so set pin DD to LOW.
Still 15 points remaning. Pin CC is worth 40, which is too much, so set pin CC to LOW.
Still 15 points remaining. Pin BB is worth 20, which is too much, so set pin BB to LOW.
Still 15 points remaining. Pin AA is worth 10, so set pin AA to HIGH, and reduce points remaining by 10.
Now only 5 points remaining. Pin D is worth 8, which is too much, so set pin D to low.
Still 5 points remaining. Pin C is worth 4, so set pin C to HIGH, and reduce points remaining by 4.
Now only 1 point remaining. Pin B is worth 2, which is too much, so set pin B to low.
Still 1 point remaining. Pin A is worth 1, so set it to HIGH, and we are finished.

Almost working. However, when target goes high, the score goes from 0 right to 7. (or whatever number happens to be last in the showScore list. Anyone know how I can access the other scores and increment by only one after each hit? Thanks!

const int target4Pin = 4;   // START target pin

const int A =  34;     // BCD 1A (to BCD - 7 segment IC)
const int B =  35;     // BCD 1B (to BCD - 7 segment IC)
const int C =  36;     // BCD 1C (to BCD - 7 segment IC)
const int D =  37;     // BCD 1D (to BCD - 7 segment IC)
const int AA =  38;    // BCD 2A (to BCD - 7 segment IC)
const int BB =  39;    // BCD 2B (to BCD - 7 segment IC)
const int CC =  40;    // BCD 2C (to BCD - 7 segment IC)
const int DD =  41;    // BCD 2D (to BCD - 7 segment IC)

int Score =0;
int target4State = 0;       // variable for reading the target10 status

void setup() {
  pinMode(target4Pin, INPUT);
  for(int i = 34; i <=41; i++){
  pinMode(i, OUTPUT);    
  }
  }

void loop(){ 
  
  target4State = digitalRead(target4Pin);  // READ TARGET 
if (target4State == HIGH){                 //TARGET IS HIT
Score = Score+1;                          //INCREMENT SCORE BY 1
showScore();
}
}
void showScore(){
  
if (Score == 0) {
  digitalWrite(A, LOW);
  digitalWrite(B, LOW);
  digitalWrite(C, LOW);
  digitalWrite(D, LOW);
  digitalWrite(AA, LOW);
  digitalWrite(BB, LOW);
  digitalWrite(CC, LOW);
  digitalWrite(DD, LOW);
}
if (Score == 1) {
  digitalWrite(A, LOW);
  digitalWrite(B, LOW);
  digitalWrite(C, LOW);
  digitalWrite(D, HIGH);
  digitalWrite(AA, LOW);
  digitalWrite(BB, LOW);
  digitalWrite(CC, LOW);
  digitalWrite(DD, LOW);
}
if (Score == 2) {
  digitalWrite(A, LOW);
  digitalWrite(B, LOW);
  digitalWrite(C, HIGH);
  digitalWrite(D, LOW);
  digitalWrite(AA, LOW);
  digitalWrite(BB, LOW);
  digitalWrite(CC, LOW);
  digitalWrite(DD, LOW);
}
if (Score == 3) {
  digitalWrite(A, LOW);
  digitalWrite(B, LOW);
  digitalWrite(C, HIGH);
  digitalWrite(D, HIGH);
  digitalWrite(AA, LOW);
  digitalWrite(BB, LOW);
  digitalWrite(CC, LOW);
  digitalWrite(DD, LOW);
}
if (Score == 4) {
  digitalWrite(A, LOW);
  digitalWrite(B, HIGH);
  digitalWrite(C, LOW);
  digitalWrite(D, LOW);
  digitalWrite(AA, LOW);
  digitalWrite(BB, LOW);
  digitalWrite(CC, LOW);
  digitalWrite(DD, LOW);
}
if (Score == 5) {
  digitalWrite(A, LOW);
  digitalWrite(B, HIGH);
  digitalWrite(C, LOW);
  digitalWrite(D, HIGH);
  digitalWrite(AA, LOW);
  digitalWrite(BB, LOW);
  digitalWrite(CC, LOW);
  digitalWrite(DD, LOW);
}
if (Score == 6) {
  digitalWrite(A, LOW);
  digitalWrite(B, HIGH);
  digitalWrite(C, HIGH);
  digitalWrite(D, LOW);
  digitalWrite(AA, LOW);
  digitalWrite(BB, LOW);
  digitalWrite(CC, LOW);
  digitalWrite(DD, LOW);
}
if (Score == 7) {
  digitalWrite(A, LOW);
  digitalWrite(B, HIGH);
  digitalWrite(C, HIGH);
  digitalWrite(D, HIGH);
  digitalWrite(AA, LOW);
  digitalWrite(BB, LOW);
  digitalWrite(CC, LOW);
  digitalWrite(DD, LOW);
}
}
//AND SO ON UNTIL 20

I assume you know loop() runs over again after the last } millions of times a second.

You need to change the code looking for a change in state and then when target4State is HIGH.

There is an example in the IDE.

Also:

.

As long as your "target pin" is high, the score will keep counting up. So, the way you have it, it will count up very rapidly, from 0 to 1, 2, 3, 4, 5, 6, 7, and even higher, faster than the eye can see. (It does not display the numbers higher than 7 because you have not given it instructions on how to display higher numbers.) So what you see is the display jumping from 0 to 7.

Here are some hints for you.

// Instead of just this:
int target4State = 0;       // variable for reading the target status

// you need one more variable, like this:
int target4State = 0;       // variable for reading the target status
int target4StateLastTime = 0;  // for remembering the previous target status



// Instead of just this:
if (target4State == HIGH){                 //TARGET IS HIT

// you need to write something like this:
if ((target4State == HIGH) && (target4StateLastTime == LOW)){ //TARGET JUST GOT HIT



// This should be the very last thing in your loop() function:
target4StateLastTime = target4State;  // remember target state for next time

Thanks to all for the help. My code is working beautifully! Great forum.