I have a working code for my pinball machine to show the score when a button is hit but after I added my left side buttons, I can't seem to get the score to show up on my display anymore. I have gone through this code multiple times and I can't get it to work.
#define MAX7219_DIN 6 // set up for the display
#define MAX7219_CS 7
#define MAX7219_CLK 8
#define targetCount 8 // set up the number of targets in the target set
#define targetLed1 9
#define targetLed2 10
#define targetLed3 11
#define targetLed4 12
#define targetPin1 22
#define targetPin2 24
#define targetPin3 26
#define targetPin4 28
#define targetPin5 2
#define targetPin6 3
#define targetPin7 4
#define targetPin8 5
typedef struct target
{
byte detectPin;
byte ledPin;
byte hit;
};
uint16_t score; //this is the variable to store the score
target targets[targetCount] = {{targetPin1, 0}
,{targetPin2, 0}
,{targetPin3, 0}
,{targetPin4, 0}
,{targetPin5, 0}
,{targetPin6, 0}
,{targetPin7, 0}
,{targetPin8, 0}
};
//right side of the board scoring info
long last_rightBlue = 0;
long last_rightGreen = 0;
long last_rightRed = 0;
long last_rightWhite = 0;
//left side of the board scoring info
long last_leftBlue = 0;
long last_leftGreen = 0;
long last_leftRed = 0;
long last_leftWhite = 0;
//top four rollover pins for the start of the game for a posible big score
/*
int last_topLeft = 0;
int last_topMiddleLeft = 0;
int last_topMiddleRight = 0;
int last_topRight = 0;
*/
// the setup function runs once when you press reset or power the board
void setup() {
pinMode(targetPin1, INPUT);
pinMode(targetPin2, INPUT);
pinMode(targetPin3, INPUT);
pinMode(targetPin4, INPUT);
pinMode(targetPin5, INPUT);
pinMode(targetPin6, INPUT);
pinMode(targetPin7, INPUT);
pinMode(targetPin8, INPUT);
Serial.begin(9600);
Serial.println("Ready to begin!");
initialiseMAX7219();
output(0x0f, 0x00); //display test register - test mode off
output(0x0c, 0x01); //shutdown register - normal operation
output(0x0b, 0x07); //scan limit register - display digits 0 thru 7
output(0x0a, 0x0f); //intensity register - max brightness
output(0x09, 0xff); //decode mode register - CodeB decode all digits
output(0x08, 0x0c); //digit 7 (leftmost digit) data
output(0x07, 0x0b);
output(0x06, 0x0d);
output(0x05, 0x0e);
output(0x04, 0x08);
output(0x03, 0x07);
output(0x02, 0x06);
output(0x01, 0x05); //digit 0 (rightmost digit) data
}
// the loop function runs over and over again forever
void loop()
{
detectCollision();
redrawTargets();
displayScore(score);
}
//This shows the score variable on the 8-digit display
void displayScore(int score)
{
//calculate number of digits
String myString = String(score);
long numberOfDigits = myString.length();
//clear unused digits
for (long i = 8; i > numberOfDigits; i--)
{
output(i, 0x0F);
}
//write digits to the display
for (long i = 1; i <= numberOfDigits; i++)
{
output(i, myString[numberOfDigits-i]);
}
} //end of score display
//This checks if targets are hit
void detectCollision()
{
long rightBlue = digitalRead(targets[0].detectPin);
long rightGreen = digitalRead(targets[1].detectPin);
long rightRed = digitalRead(targets[2].detectPin);
long rightWhite = digitalRead(targets[3].detectPin);
long leftBlue = digitalRead(targets[4].detectPin);
long leftGreen = digitalRead(targets[5].detectPin);
long leftRed = digitalRead(targets[6].detectPin);
long leftWhite = digitalRead(targets[7].detectPin);
//Check if target1 is hit
if(rightBlue != last_rightBlue){
if (rightBlue == HIGH) { // Switch is pressed
}
else { // Switch is released and score added
targets[0].hit = 1;
Serial.print("Traff A!"); Serial.print("New score: ");
Serial.print(score); Serial.print("!");Serial.println();
score += 50;
delay(50);
}
delay(100);
last_rightBlue = rightBlue;
}
//Check if target2 is hit
if(rightGreen != last_rightGreen){
if (rightGreen == HIGH) { // Switch is pressed
}
else { // Switch is released and score added
targets[1].hit = 1;
Serial.print("Traff B!"); Serial.print("New score: ");
Serial.print(score); Serial.print("!");Serial.println();
score += 25;
delay(50);
}
delay(100);
last_rightGreen = rightGreen;
}
//Check if target3 is hit
if(rightRed != last_rightRed){
if (rightRed == HIGH) { // Switch is pressed
}
else { // Switch is released and score added
targets[2].hit = 1;
Serial.print("Traff C!"); Serial.print("New score: ");
Serial.print(score); Serial.print("!");Serial.println();
score += 100;
delay(50);
}
delay(100);
last_rightRed = rightRed;
}
//Check if target4 is hit
if(rightWhite != last_rightWhite){
if (rightWhite == HIGH) { // Switch is pressed
}
else { // Switch is released and score added
targets[3].hit = 1;
Serial.print("Traff D!"); Serial.print("New score: ");
Serial.print(score); Serial.print("!");Serial.println();
score += 5;
delay(50);
}
delay(100);
last_rightWhite = rightWhite;
}
if(leftBlue != last_leftBlue){
if (leftBlue == HIGH) { // Switch is pressed
}
else { // Switch is released and score added
targets[4].hit = 1;
Serial.print("Traff D!"); Serial.print("New score: ");
Serial.print(score); Serial.print("!");Serial.println();
score += 30;
delay(50);
}
delay(100);
last_leftBlue = leftBlue;
}
if(leftGreen != leftGreen){
if (leftGreen == HIGH) { // Switch is pressed
}
else { // Switch is released and score added
targets[5].hit = 1;
Serial.print("Traff D!"); Serial.print("New score: ");
Serial.print(score); Serial.print("!");Serial.println();
score += 45;
delay(50);
}
delay(100);
last_leftGreen = leftGreen;
}
if(leftRed != last_leftRed){
if (leftRed == HIGH) { // Switch is pressed
}
else { // Switch is released and score added
targets[6].hit = 1;
Serial.print("Traff D!"); Serial.print("New score: ");
Serial.print(score); Serial.print("!");Serial.println();
score += 8;
delay(50);
}
delay(100);
last_leftRed = leftRed;
}
if(leftWhite != last_leftWhite){
if (leftWhite == HIGH) { // Switch is pressed
}
else { // Switch is released and score added
targets[7].hit = 1;
Serial.print("Traff D!"); Serial.print("New score: ");
Serial.print(score); Serial.print("!");Serial.println();
score += 11;
delay(50);
}
delay(100);
last_leftWhite = leftWhite;
}
}
void redrawTargets()
{
for(long i = 0; i < targetCount; i++)
digitalWrite(targets[i].ledPin, 1-targets[i].hit);
}
void initialiseMAX7219() // MAX7219 7-Segment LED Display
{
digitalWrite(MAX7219_CS, HIGH);
pinMode(MAX7219_DIN, OUTPUT);
pinMode(MAX7219_CS, OUTPUT);
pinMode(MAX7219_CLK, OUTPUT);
}
//common routine to talkt o MAX7219
void output(byte address, byte data)
{
digitalWrite(MAX7219_CS, LOW);
shiftOut(MAX7219_DIN, MAX7219_CLK, MSBFIRST, address);
shiftOut(MAX7219_DIN, MAX7219_CLK, MSBFIRST, data);
digitalWrite(MAX7219_CS, HIGH);
}
