with a lot of reading i have gotten to the point of where i can keep track of scores for two players and start and stop the play clock. however i cannot figure out where to put the command to stop the clock and sound a buzzer once the clock hits zero. i have placed the if statement at the end of the loop and many other different places within the code. however everytime the clock hits zero it just keeps counting down and show -1,-2,-3 etc etc. Anyone have any hints as to where im going wrong. thanks
oh and i know i can increase a counter by one with a button press. example buttoncounter++. but is there a way to increase it by 10 or even 100. i have tried. buttoncounter + 10 and it does not work.
Thanks again
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <gfxfont.h>
#include <Adafruit_LEDBackpack.h>
#include "TimerOne.h"
Adafruit_7segment matrix = Adafruit_7segment();
#define DATA1 8 // -
#define LATCH1 9 // ---handles the shift registers for player one score
#define CLOCK1 10 // -
#define DATA2 11 //
#define LATCH2 12 //---- handles the shift registers for player two score
#define CLOCK2 13 //
int timeRemaining = 0;
int minutesToGo = 0;
int secondsToGo = 0;
int timeLine = 300; //start with 5minutes
int homeScore=0;
int awayScore=0;
int buttonState = 0;
int buttonState2 = 0;
int buttonStateDec = 0;
int buttonStateDec2 = 0;
int lastButtonState = 0;
int lastButtonState2 = 0;
int lastButtonStateDec = 0;
int lastButtonStateDec2 = 0;
int digits[] = {2, 158, 36, 12, 152, 72, 64, 30, 0, 24, 16, 0, 98, 2, 96, 112,}; /// used for displaying player one score
int digits2[] = {2, 158, 36, 12, 152, 72, 64, 30, 0, 24, 16, 0, 98, 2, 96, 112,}; /// used for displaying player two score
bool paused=1; // start in a paused state
//button functions
// 2 Reset score and time (and waits for buttone 3 to start)
// 3 Pause/Resume (in paused state timer stops going down)
// 4 Increment Home Score
// 5 Reduce Home Score
// 6 Increase Away Score
// 7 Reduce Away Score
void setup()
{
pinMode(LATCH1, OUTPUT);
pinMode(DATA1, OUTPUT);
pinMode(CLOCK1, OUTPUT);
player1displayNumber(homeScore);
pinMode(LATCH2, OUTPUT);
pinMode(DATA2, OUTPUT);
pinMode(CLOCK2, OUTPUT);
player2displayNumber(awayScore);
matrix.begin(0x70);
int n;
for (n=2;n<8;n++){
pinMode(n,INPUT_PULLUP);
Serial.begin(9600);
Timer1.initialize();
Timer1.attachInterrupt(updateTimeline);
}
}
//set by interrupt routine to signal display need updating
bool timeUpdated=true;
void loop()
{
int n;
bool debounce=false;
//first look for any input
for (n=2;n<8;n++){
if(digitalRead(n)==LOW)
{
debounce=true;
switch(n)
{
case 2://reset
timeLine=300;//start with 3minutes
homeScore=0;
awayScore=0;
paused=true;
break;
case 3://pause-restart
paused=!paused;
break;
case 4://increment home
player1();
break;
case 5://decrement home score
player1Sub();
break;
case 6://increment away score
player2();
break;
case 7://decrement home score
player2Sub();
break;
}
}
}
//Now update display (but only if updateTime has executed since last time)
if(timeUpdated){
updateDisplay();
}
if(debounce){
delay(400);
}
}
void updateDisplay()
{
boolean drawDots = true;
minutesToGo = timeLine / 60;
secondsToGo = timeLine % 60;
timeRemaining = minutesToGo * 100 + secondsToGo;
matrix.print(timeRemaining);
matrix.drawColon(drawDots);
matrix.writeDisplay();
}
void updateTimeline()
{
if(!paused){
timeLine--;
}
timeUpdated=true;
}
void player1() ////// increases player one score
{
int n;
buttonState = digitalRead(n);
if(buttonState != lastButtonState)
{
if (buttonState = HIGH){
homeScore++;
player1displayNumber(homeScore);
delay(25);
}
}
if (homeScore == 100){
homeScore = 0;
}
}
void player1Sub() ///// decreases player one score
{
int n;
buttonStateDec = digitalRead(n);
if(buttonStateDec != lastButtonStateDec)
{
if (buttonStateDec = HIGH){
homeScore--;
player1displayNumber(homeScore);
delay(25);
}
}
if (homeScore == 100){
homeScore = 0;
}
}
void player1displayNumber(int n)
{
int left, right=0;
if (n < 10)
{
digitalWrite(LATCH1, LOW);
shiftOut(DATA1, CLOCK1, LSBFIRST, digits[n]);
shiftOut(DATA1, CLOCK1, LSBFIRST, 2);
digitalWrite(LATCH1, HIGH);
}
else
{
right = n % 10;
left = n / 10;
digitalWrite(LATCH1, LOW);
shiftOut(DATA1, CLOCK1, LSBFIRST, digits);
shiftOut(DATA1, CLOCK1, LSBFIRST, digits);
digitalWrite(LATCH1, HIGH);
}
}
void player2() ///// increases player two score
{
int n;
buttonState2 = digitalRead(n);
if(buttonState2 != lastButtonState2)
{
if (buttonState2 = HIGH){
awayScore++;
player2displayNumber(awayScore);
delay(25);
}
}
if (homeScore == 100){
homeScore = 0;
}
}
void player2Sub() ///// decreases player two score
{
int n;
buttonStateDec2 = digitalRead(n);
if(buttonStateDec2 != lastButtonStateDec2)
{
if (buttonStateDec2 = HIGH){
awayScore--;
player2displayNumber(awayScore);
delay(25);
}
}
if (homeScore == 100){
homeScore = 0;
}
}
void player2displayNumber(int n)
{
int left, right=0;
if (n < 10)
{
digitalWrite(LATCH2, LOW);
shiftOut(DATA2, CLOCK2, LSBFIRST, digits[n]);
shiftOut(DATA2, CLOCK2, LSBFIRST, 2);
digitalWrite(LATCH2, HIGH);
}
else
{
right = n % 10;
left = n / 10;
digitalWrite(LATCH2, LOW);
shiftOut(DATA2, CLOCK2, LSBFIRST, digits2);
shiftOut(DATA2, CLOCK2, LSBFIRST, digits2);
digitalWrite(LATCH2, HIGH);
}
}