86Views4Replies

Author Options:

Arduino score board code question Answered

hi all.
Ive been trying to work this out for a week and I'm guessing its a simple answer.
Im building a score board, just LED modules (lights) instead of a 7 segment display
There are two buttons, one for when each player scores a point. The led will work similar to a chaser but manually, each time the button is pressed the light advances one space and when the opposite players button is pushed the light goes towards the other player. 

I tried using a button counter code, but because its counting the tines it touched it works in multiples. and just messes things up. can anyone help?
thanks


The schematic doesn't show the full thing, nor does the code. Im only working with one player at the moment to simply things a bit.


// this constant won't change:
const int  buttonPin1 = 6;    // the pin that pushbutton 1 is attached to
const int  buttonPin2 = 7;    // the pin that pushbutton 2 is attached to
const int ledPin1 = 2;       // the pin that the LED 1 is attached to
const int ledPin2 = 3;       // the pin that the LED 2 is attached to
const int ledPin3 = 4;       // the pin that the LED 3 is attached to
const int ledPin4 = 5;       // the pin that the LED 4 is attached to
const int ledPin4 = 8;       // the pin that the LED 5 is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin1);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;


  // turns on the LED every four button pushes by
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of
  // the division of two numbers:
  if (buttonPushCounter % 1 == 0) {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);

  } else {
    digitalWrite(ledPin1, LOW);
  }

  if (buttonPushCounter % 2 == 0) {
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin4, LOW);
    digitalWrite(ledPin3, LOW);}
   
      {if (buttonPushCounter % 3 == 0) {
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin1, LOW);}
   
      if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin4, HIGH);
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);

}
      }
}

Discussions

Simple digital counter in the BIT form, plenty of examples in the Arduino playground.
May I suggest to look there first?

Cheers
i have the score going up with each button press, but i cant seem to get it using the other button. Any sort of code i put in utilizing the down button (buttonPin2) the whole thing is bricked until i change the code.

Is there something im missing to get the counter to go down with each push of button2?

// this constant won't change:

const int buttonPin1 = 2; // the pin that the pushbutton is attached to

const int buttonPin2 = 3; // the pin that the pushbutton is attached to

const int ledPin1 = 4; // the pin that the LED is attached to

const int ledPin2 = 5; // the pin that the LED is attached to

const int ledPin3 = 6; // the pin that the LED is attached to

const int ledPin4 = 7; // the pin that the LED is attached to

// Variables will change:

int buttonPushCounter1 = 0; // counter for the number of button1 presses

int buttonState1 = 0; // current state of the button

int lastButtonState1 = 0; // previous state of the button

int buttonPushCounter2 = 0; // counter for the number of button1 presses

int buttonState2 = 0; // current state of the button

int lastButtonState2 = 0; // previous state of the button

void setup() {

// initialize the button pin as a input:

pinMode(buttonPin1, INPUT);

pinMode(buttonPin2, INPUT);

// initialize the LED as an output:

pinMode(ledPin1, OUTPUT);

pinMode(ledPin2, OUTPUT);

pinMode(ledPin3, OUTPUT);

pinMode(ledPin4, OUTPUT);

// initialize serial communication:

Serial.begin(9600);

}

void loop() {

// read the pushbutton input pin:

buttonState1 = digitalRead(buttonPin1);

// compare the buttonState to its previous state

if (buttonState1 != lastButtonState1) {

// if the state has changed, increment the counter

if (buttonState1 == HIGH) {

// if the current state is HIGH then the button

// wend from off to on:

buttonPushCounter1++;

Serial.println("on");

Serial.print("number of button pushes: ");

Serial.println(buttonPushCounter1);

} else {

// if the current state is LOW then the button

// wend from on to off:

Serial.println("off");

}

// Delay a little bit to avoid bouncing

delay(50);

}

// save the current state as the last state,

//for next time through the loop

lastButtonState1 = buttonState1;

// turns on the LED every four button pushes by

// checking the modulo of the button push counter.

// the modulo function gives you the remainder of

// the division of two numbers:

if (buttonPushCounter1 == 1 ) {

digitalWrite(ledPin1, HIGH);

} else {

digitalWrite(ledPin1, LOW);

}

if((buttonPushCounter1 == 2) ) {

digitalWrite(ledPin2, HIGH);

} else {

digitalWrite(ledPin2, LOW);

}

if((buttonPushCounter1 ==3) ) {

digitalWrite(ledPin3, HIGH);

} else {

digitalWrite(ledPin3, LOW);

}

if((buttonPushCounter1 ==4) ) {

digitalWrite(ledPin4, HIGH);

} else {

digitalWrite(ledPin4, LOW);}

}

I only had a few hours of sleep but I still think I am missing something...
Have you tried to define your counter variables in the setup and setting a default start value?
After all the thing can only count if it knows where to start and end ;)

this is going to sound stupid but my best answer to that is, i think so........
As you can probably guess this sort of thing is not my forte.

Reading my own code (and i was running on very little sleep trying to get it done) i didnt even have the part of it that i thought to make the counter go up and down. Ill add the revised code down below.

Button1 should make the counter go up b one, and button2 should make it come down by1.

Using this code i have the counter and lights going up in sequence but it doesn't come down and i cant figure out why.

// this constant won't change:

const int buttonPin1 = 2; // the pin that the pushbutton is attached to

const int buttonPin2 = 3; // the pin that the pushbutton is attached to

const int ledPin1 = 4; // the pin that the LED is attached to

const int ledPin2 = 5; // the pin that the LED is attached to

const int ledPin3 = 6; // the pin that the LED is attached to

const int ledPin4 = 7; // the pin that the LED is attached to

// Variables will change:

int buttonPushCounter1 = 0; // counter for the number of button1 presses

int buttonState1 = 0; // current state of the button1

int lastButtonState1 = 0; // previous state of the button1

int buttonPushCounter2 = 0; // counter for the number of button2 presses

int buttonState2 = 0; // current state of the button2

int lastButtonState2 = 0; // previous state of the button2

void setup() {

// initialize the button pin as a input:

pinMode(buttonPin1, INPUT);

pinMode(buttonPin2, INPUT);

// initialize the LED as an output:

pinMode(ledPin1, OUTPUT);

pinMode(ledPin2, OUTPUT);

pinMode(ledPin3, OUTPUT);

pinMode(ledPin4, OUTPUT);

// initialize serial communication:

Serial.begin(9600);

}

void loop() {

// read the pushbutton1 input pin:

if (buttonState1 = digitalRead(buttonPin1));

// compare the buttonState to its previous state

if (buttonState1 != lastButtonState1) {

// if the state has changed, increment the counter

if (buttonState1 == HIGH) {

// if the current state is HIGH then the button

// wend from off to on:

buttonPushCounter1++;

buttonPushCounter1 = constrain (buttonPushCounter1, 0, 4);

Serial.println("on");

Serial.print("number of button pushes: ");

Serial.println(buttonPushCounter1);

} {

// if the current state is LOW then the button

// wend from on to off:

Serial.println("off");

}

// Delay a little bit to avoid bouncing

delay(50);

}

// save the current state as the last state,

//for next time through the loop

lastButtonState1 = buttonState1;

if (buttonState1 = digitalRead(buttonPin1));

// compare the buttonState to its previous state

if (buttonState2 != lastButtonState2) {

// if the state has changed, increment the counter

if (buttonState2 == HIGH) {

// if the current state is HIGH then the button

// wend from off to on:

buttonPushCounter1--;

buttonPushCounter1 = constrain (buttonPushCounter1, 0, 4);

Serial.println("on");

Serial.print("number of button pushes: ");

Serial.println(buttonPushCounter1);

} {

// if the current state is LOW then the button

// wend from on to off:

Serial.println("off");

}

// Delay a little bit to avoid bouncing

delay(50);

}

// save the current state as the last state,

//for next time through the loop

lastButtonState2 = buttonState2;

if (buttonPushCounter1 == 1 ) {

digitalWrite(ledPin1, HIGH);

} else {

digitalWrite(ledPin1, LOW);

}

if((buttonPushCounter1 == 2) ) {

digitalWrite(ledPin2, HIGH);

} else {

digitalWrite(ledPin2, LOW);

}

if((buttonPushCounter1 ==3) ) {

digitalWrite(ledPin3, HIGH);

} else {

digitalWrite(ledPin3, LOW);

}

if((buttonPushCounter1 ==4) ) {

digitalWrite(ledPin4, HIGH);

} else {

digitalWrite(ledPin4, LOW);}}