I am trying to increment a seven segment display by 20,19,18,17,16,15, or 25 depending one which pushbutton is pressed.
I have a variable (one_score) that holds the value depending on which buttons are pressed.
Now I'd like to print the value continuously to the seven segment display as buttons are pressed changing the value of one_score.
It is a scoreboard for playing darts.
My trouble is coming with:
sevseg.setNumber(one_score,0);
sevseg.refreshDisplay(); // Must run repeatedly
Serial.print(one_score);
The display will change on the button press but it'll just be random segments. I've tested the display connection on another program so I believe it to be connected properly.
#include <SevSeg.h>
SevSeg sevseg; //Initiate a seven segment controller object
// this constant won't change:
const int twenty_buttonPin_one = 22; // the pin that the pushbutton is attached to
const int nineteen_buttonPin_one = 23; // the pin that the pushbutton is attached to
const int eighteen_buttonPin_one = 24; // the pin that the pushbutton is attached to
const int seventeen_buttonPin_one = 25; // the pin that the pushbutton is attached to
const int sixteen_buttonPin_one = 26; // the pin that the pushbutton is attached to
const int fifteen_buttonPin_one = 27; // the pin that the pushbutton is attached to
const int bullseye_buttonPin_one = 28;
const int twenty_buttonPin_two = 29; // the pin that the pushbutton is attached to
const int nineteen_buttonPin_two = 30; // the pin that the pushbutton is attached to
const int eighteen_buttonPin_two = 31; // the pin that the pushbutton is attached to
//const int seventeen_buttonPin_two = ##; // the pin that the pushbutton is attached to
//const int sixteen_buttonPin_two = ##; // the pin that the pushbutton is attached to
//const int fifteen_buttonPin_two = ##; //
//const int bullseye_buttonPin_two = ##;
const int reset_buttonPin = 50; // the pin that the pushbutton is attached to
// Variables will change:
int one_score = 0;
int two_score = 0;
// Player 1 - 20
int twenty_buttonPushCounter_one = 0; // counter for the number of button presses
int twenty_buttonState_one = 0; // current state of the button
int twenty_lastButtonState_one = 0; // previous state of the button
//Player 1 - 19
int nineteen_buttonPushCounter_one = 0; // counter for the number of button presses
int nineteen_buttonState_one = 0; // current state of the button
int nineteen_lastButtonState_one = 0; // previous state of the button
//Player 1 - 18
int eighteen_buttonPushCounter_one = 0; // counter for the number of button presses
int eighteen_buttonState_one = 0; // current state of the button
int eighteen_lastButtonState_one = 0; // previous state of the button
//Player 1 - 17
int seventeen_buttonPushCounter_one = 0; // counter for the number of button presses
int seventeen_buttonState_one = 0; // current state of the button
int seventeen_lastButtonState_one = 0; // previous state of the button
//Player 1 - 16
int sixteen_buttonPushCounter_one = 0; // counter for the number of button presses
int sixteen_buttonState_one = 0; // current state of the button
int sixteen_lastButtonState_one = 0; // previous state of the button
//Player 1 - 15
int fifteen_buttonPushCounter_one = 0; // counter for the number of button presses
int fifteen_buttonState_one = 0; // current state of the button
int fifteen_lastButtonState_one = 0; // previous state of the button
//Player 1 - Bullseye
int bullseye_buttonPushCounter_one = 0; // counter for the number of button presses
int bullseye_buttonState_one = 0; // current state of the button
int bullseye_lastButtonState_one = 0; // previous state of the button
// Player 2 - 20
int twenty_buttonPushCounter_two = 0; // counter for the number of button presses
int twenty_buttonState_two = 0; // current state of the button
int twenty_lastButtonState_two = 0; // previous state of the button
//Player 2 - 19
int nineteen_buttonPushCounter_two = 0; // counter for the number of button presses
int nineteen_buttonState_two = 0; // current state of the button
int nineteen_lastButtonState_two = 0; // previous state of the button
//Player 2 - 18
int eighteen_buttonPushCounter_two = 0; // counter for the number of button presses
int eighteen_buttonState_two = 0; // current state of the button
int eighteen_lastButtonState_two = 0; // previous state of the button
//Player 2 - 17
int seventeen_buttonPushCounter_two = 0; // counter for the number of button presses
int seventeen_buttonState_two = 0; // current state of the button
int seventeen_lastButtonState_two = 0; // previous state of the button
//Player 2 - 16
int sixteen_buttonPushCounter_two = 0; // counter for the number of button presses
int sixteen_buttonState_two = 0; // current state of the button
int sixteen_lastButtonState_two = 0; // previous state of the button
//Player 2 - 15
int fifteen_buttonPushCounter_two = 0; // counter for the number of button presses
int fifteen_buttonState_two = 0; // current state of the button
int fifteen_lastButtonState_two = 0; // previous state of the button
//Player 2 - Bullseye
int bullseye_buttonPushCounter_two = 0; // counter for the number of button presses
int bullseye_buttonState_two = 0; // current state of the button
int bullseye_lastButtonState_two = 0; // previous state of the button
//reset
int reset_buttonPushCounter = 0; // counter for the number of button presses
int reset_buttonState = 0; // current state of the button
int reset_lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
//Player 1
pinMode(twenty_buttonPin_one, INPUT);
pinMode(nineteen_buttonPin_one, INPUT);
pinMode(eighteen_buttonPin_one, INPUT);
pinMode(seventeen_buttonPin_one, INPUT);
pinMode(sixteen_buttonPin_one, INPUT);
pinMode(fifteen_buttonPin_one, INPUT);
pinMode(bullseye_buttonPin_one, INPUT);
//Player 2
pinMode(twenty_buttonPin_two, INPUT);
pinMode(nineteen_buttonPin_two, INPUT);
pinMode(eighteen_buttonPin_two, INPUT);
// pinMode(seventeen_buttonPin_two, INPUT);
// pinMode(sixteen_buttonPin_two, INPUT);
//pinMode(fifteen_buttonPin_two, INPUT);
//pinMode(bullseye_buttonPin_two, INPUT);
pinMode(reset_buttonPin, INPUT);
// initialize the LED as an output:
//pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
//Display
byte numDigits = 4;
byte digitPins[] = {2, 3, 4, 5};
byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13};
sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins);
sevseg.setBrightness(90);
}
void loop() {
// read the pushbutton input pin:
// Player 1
twenty_buttonState_one = digitalRead(twenty_buttonPin_one);
nineteen_buttonState_one = digitalRead(nineteen_buttonPin_one);
eighteen_buttonState_one = digitalRead(eighteen_buttonPin_one);
seventeen_buttonState_one = digitalRead(seventeen_buttonPin_one);
sixteen_buttonState_one = digitalRead(sixteen_buttonPin_one);
fifteen_buttonState_one = digitalRead(fifteen_buttonPin_one);
bullseye_buttonState_one = digitalRead(bullseye_buttonPin_one);
//Player 2
twenty_buttonState_two = digitalRead(twenty_buttonPin_two);
nineteen_buttonState_two = digitalRead(nineteen_buttonPin_two);
eighteen_buttonState_two = digitalRead(eighteen_buttonPin_two);
//seventeen_buttonState_two = digitalRead(seventeen_buttonPin_two);
//sixteen_buttonState_two = digitalRead(sixteen_buttonPin_two);
//fifteen_buttonState_two = digitalRead(fifteen_buttonPin_two);
//bullseye_buttonState_two = digitalRead(bullseye_buttonPin_two);
//Player 1 Scoring--------------------------------------------------------
// compare the buttonState to its previous state
//Player 1 - 20
if (twenty_buttonState_one != twenty_lastButtonState_one)
{
// if the state has changed, increment the counter
if (twenty_buttonState_one == HIGH) {
// if the current state is HIGH then the button went from off to on:
one_score+=20;
sevseg.setNumber(one_score,0);
sevseg.refreshDisplay(); // Must run repeatedly
Serial.print(one_score);
} else {
// if the current state is LOW then the button went 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
twenty_lastButtonState_one = twenty_buttonState_one;
//Player 1 - 19
}