Hello!
I have been working on a scoreboard project for some time now, and I have just now got around to the coding. The idea is to score a point, you swipe the paddle under the table, which is detected by the photoresistor. I am still fairly new to the Arduino language, and I didn't know what to expect.
The issue is that when I power on the device, P1 shows 11 in the P1 11 loop, and shows a Game Score of 2-3. Then, the P2 display shows 11, and still reads a Game Score of 2-3. This goes on until I turn off the power. I'm not sure what to try!
Properly, it should run to 11, flash, and then show the game score for 10 seconds, as well as switch the Score LED off, and the Game LED on.Then, it should go back to 0-0, reversing the state of both of those LEDs. If both players swipe, the scoreboard should completely reset.
Here is the code, minus some of the void() 7-Segment statements, as this made it too lengthy to post.
/* Ping Pong Score Keeper
Uses photoresistors and LEDs to detect a swipe of a paddle under the table,
and then adds a point to the scoreboard, depending on which side was swiped.
Both players swipe at the same time to clear the scoreboard.
Schematic for 7 - Segments:
13/6
-------------
| |
8/1| | 12/5
| 7/0 |
|-----------|
| |
9/2 | | 11/4
| |
|-----------|
10/3
Use a 10KOhm Pull-Down resistor to ground with the photoresistor, as well as standard current-limiting resistors.
Enjoy!
*/
int P1Photo = A0; //Setup P1 and P2's photoresistor
int P2Photo = A1;
int P1PhotoVal = 0;
int P2PhotoVal = 0;
int P1Score = 0; //Initialize the score
int P2Score = 0;
int P1GameScore = 0;
int P2GameScore = 0;
int P1OldScore = 0; //Setup OldScore for use in the delay mechanism.
int P2OldScore = 0;
int scoreLED = A3;
int gameLED = A4;
void cleardisp1() //Setup all of the commands for printing digits. I'm lazy, so I didn't use a shift register.
{
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
}
void cleardisp2()
{
digitalWrite(6, LOW);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
digitalWrite(3, LOW);
digitalWrite(2, LOW);
digitalWrite(1, LOW);
digitalWrite(0, LOW);
}
void scorezero1()
{
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
digitalWrite(8, HIGH);
}
void scoreone1()
{
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
}
void scoretwo1()
{
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(7, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
}
void scorethree1()
{
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(7, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
//That goes on for a long time...
//Put a 2 at the end for P2.
void setup () {
//Serial.begin(9600); //Initialize serial for easy debugging
pinMode(P1Photo, INPUT); //Setup the photoresistors as inputs
pinMode(P2Photo, INPUT);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(3, OUTPUT);
pinMode(2, OUTPUT);
pinMode(1, OUTPUT);
pinMode(0, OUTPUT);
pinMode(scoreLED, OUTPUT);
pinMode(gameLED, OUTPUT);
digitalWrite(scoreLED, HIGH);
}
void loop() {
int P1PhotoVal = analogRead(P1Photo); //Take a reading for P1's PR
int P2PhotoVal = analogRead(P2Photo); //Take a reading...
if (P1PhotoVal<50 && P2PhotoVal<50){
P1Score == 0;
P2Score == 0;
P1GameScore == 0;
P2GameScore == 0;
delay(5000);
}
if (P1PhotoVal<350){ //NOTE: Change 000 to the value of a swipe
P1Score ++; //Add 1 to P1's count
}
if(P2PhotoVal<350) { //Add points...
P2Score++;
}
if(P1Score = 0) {
cleardisp1();
scorezero1();
}
if(P1Score = 1) {
cleardisp1();
scoreone1();
}
if(P1Score = 2) {
cleardisp1();
scoretwo1();
}
if(P1Score = 3) {
cleardisp1();
scorethree1();
}
if(P1Score = 4) {
cleardisp1();
scorefour1();
}
if(P1Score = 5) {
cleardisp1();
scorefive1();
}
if(P1Score = 6) {
cleardisp1();
scoresix1();
}
if(P1Score = 7) {
cleardisp1();
scoreseven1();
}
if(P1Score = 8) {
cleardisp1();
scoreeight1();
}
if(P1Score = 9) {
cleardisp1();
scorenine1();
}
if(P1Score = 10) {
cleardisp1();
scoreten1();
}
if(P1Score = 11) {
cleardisp1();
scoreeleven1();
delay(1500);
cleardisp1();
delay(1000);
scoreeleven1();
delay(1000);
cleardisp1();
cleardisp2();
delay(1000);
digitalWrite(scoreLED, LOW);
digitalWrite(gameLED, HIGH);
P1GameScore++;
if (P1GameScore=0) {
cleardisp1();
scorezero1();
}
if (P2GameScore=0) {
cleardisp2();
scorezero2();
}
if (P1GameScore=1) {
cleardisp1();
scoreone1();
}
if (P2GameScore=1) {
cleardisp2();
scoreone1();
}
if (P1GameScore=2) {
cleardisp1();
scoretwo1();
}
if (P2GameScore=2) {
cleardisp2();
scoretwo2();
}
if (P1GameScore=3) {
cleardisp1();
scorethree1();
}
if (P2GameScore=3) {
cleardisp2();
scorethree2();
}
delay(10000);
digitalWrite(gameLED, LOW);
digitalWrite(scoreLED, HIGH);
P1Score = 0;
P2Score = 0;
}
if(P2Score = 0) {
cleardisp2();
scorezero2();
}
if(P2Score = 1) {
cleardisp2();
scoreone2();
}
if(P2Score = 2) {
cleardisp2();
scoretwo2();
}
if(P2Score = 3) {
cleardisp2();
scorethree2();
}
if(P2Score = 4) {
cleardisp2();
scorefour2();
}
if(P2Score = 5) {
cleardisp2();
scorefive2();
}
if(P2Score = 6) {
cleardisp2();
scoresix2();
}
if(P2Score = 7) {
cleardisp2();
scoreseven2();
}
if(P2Score = 8) {
cleardisp2();
scoreeight2();
}
if(P2Score = 9) {
cleardisp2();
scorenine2();
}
if(P2Score = 10) {
cleardisp2();
scoreten2();
}
if(P2Score = 11) {
cleardisp2();
scoreeleven2();
delay(1500);
cleardisp2();
delay(1000);
scoreeleven2();
delay(1000);
cleardisp1();
cleardisp2();
delay(1000);
digitalWrite(scoreLED, LOW);
digitalWrite(gameLED, HIGH);
P2GameScore++;
if (P1GameScore=0) {
cleardisp1();
scorezero1();
}
if (P2GameScore=0) {
cleardisp2();
scorezero2();
}
if (P1GameScore=1) {
cleardisp1();
scoreone1();
}
if (P2GameScore=1) {
cleardisp2();
scoreone1();
}
if (P1GameScore=2) {
cleardisp1();
scoretwo1();
}
if (P2GameScore=2) {
cleardisp2();
scoretwo2();
}
if (P1GameScore=3) {
cleardisp1();
scorethree1();
}
if (P2GameScore=3) {
cleardisp2();
scorethree2();
}
delay(10000);
digitalWrite(gameLED, LOW);
digitalWrite(scoreLED, HIGH);
P1Score = 0;
P2Score = 0;
}
if(P1Score>P1OldScore) { //Add a delay after a swipe until a new one can be registered.
delay(3500);
P1OldScore = P1Score;
}
if(P2Score>P2OldScore) { //Cool the jets, hold the horses, land the drones...
delay(3500);
P2OldScore = P2Score;
}
}
Any help is greatly appreciated in advance!
-Wizzrobes ![]()
