I took the baseline you posted and added the tone() function to drive your piezo speaker, now to be on pin 9 vs pin 5. I’m not entirely sure that the library tone() won’t screw up the millis() function. So if the timing doesn’t work or the times seem short by 0.5 secs (from the start), then the rev1 is what’s causing it. I believe there are ways to make the PWM run off a different timer (from the 16 bit one used by milis()) but I don’t know how to do that. I’ll leave that up to you to fix … if desired.
What should happen is a 0.5 sec buzz of 2kHz at the start and a 1.0 sec long buzz of 1kHz on the stop. You can see the new constants if you want to change these values.
I don’t think you need an #include XYZ but if the compiler barfs, that’s one thing to look for.
// set pin numbers
const int StartPin = 4; // the number of the Start pushbutton pin
const int UpPin = 6; // the number of the ScrollUp pushbutton pin
const int DownPin = 7; // the number of the ScrollDown pushbutton pin
const int TargetAPin = 2; // the number of the A targets input pin
const int TargetBPin = 3; // the number of the B targets input pin
const int BuzzerPin = 9; // the number of the buzzer output pin
const int LEDPin = 13; // the number of the LED output pin
const unsigned long MaxTime = 30000; // the max time the timer can run for = 30 secs
const unsigned long WaitTime = 3000; // the wait time btw start button and timer running = 3 secs
const unsigned long DB_delay = 200; // set a debounce wait time of 200 msecs
const unsigned long BuzzTime5 = 500; // set the on time for the buzzer, 500 msecs
const unsigned long BuzzTime2 = 200; // set the on time for the buzzer, 200 msecs
const unsigned long BuzzTime10 = 1000; // set the on time for the buzzer, 1000 msecs
const unsigned int FreqHi = 2000; // High frequency of buzzer tone
const unsigned int FreqLo = 1000; // Low frequency of buzzer tone
const int RUN = 1; // make code easy to understand
// initialize global variables
volatile int TimerState = 0; // variable for state of timer, running or not running
int DisplayState = 0; // variable for controlling what"s displayed
unsigned long StartTime = 0; // variable to hold the start time
unsigned long BuzzTime = 500;// variable to hold the buzzer on time
unsigned int Freq = 2000; // variable for high or low buzzer tone
volatile int A_count = 0; // variable to hold the number of A hits
volatile int B_count = 0; // variable to hold the number of B hits
volatile int A_Times[10]; // array to hold up to 10 hit times for target A
volatile int B_Times[10]; // array to hold up to 10 hit times for target B
void setup()
{
// initialize output pins
pinMode(BuzzerPin, OUTPUT);
pinMode(LEDPin, OUTPUT);
digitalWrite(BuzzerPin, LOW);
digitalWrite(LEDPin, LOW);
// initialize the input pins with internal pullups
pinMode(StartPin, INPUT);
pinMode(UpPin, INPUT);
pinMode(DownPin, INPUT);
pinMode(TargetAPin, INPUT);
pinMode(TargetBPin, INPUT);
digitalWrite(StartPin, HIGH);
digitalWrite(UpPin, HIGH);
digitalWrite(DownPin, HIGH);
digitalWrite(TargetAPin, HIGH);
digitalWrite(TargetBPin, HIGH);
// opens serial port, sets data rate to 9600 bps
Serial.begin(9600);
// setup ext pins as interrupts
attachInterrupt(0, ISR_A, FALLING);
attachInterrupt(1, ISR_B, FALLING);
}
void loop()
{
if (digitalRead(StartPin) == LOW)
{
// start stop button has been pushed, is timer running
if (TimerState == !RUN)
{
// timer is not running so start it
// Do the following debug code only to get system running
// This will send message to PC to show button was pushed
Serial.println("Timer is running");
// clear all the prior runs data
ClearData();
// delay the Wait Time from start button push
// this delay will change to random later on
delay(WaitTime);
// turn on the LED to show timer is running
digitalWrite(LEDPin, HIGH);
// enable the interrupts just in case
interrupts();
// save the starting time of this run
StartTime = millis();
// set state of timer to running
TimerState = RUN;
// now buzz the speaker for 0.5 secs
Buzz(FreqHi, BuzzTime5)
// no need to debounce as mucho time has elapsed
}
else
{
// start stop button has been pushed again to stop timer
StopTimer();
Buzz(FreqLo, BuzzTime10);
// no need to debounce switch, a delay is built into Buzz function
// just for the moment send times to PC only not to display
SendTimes();
}
}
//Check for max time out if timer is running
if (TimerState == RUN)
{
if ((millis() - StartTime) > MaxTime)
{
// call the function that does all the things needed to stop
StopTimer();
Buzz(FreqLo, BuzzTime10);
// just for the moment send times to PC only not to display
SendTimes() ;
}
}
}
void StopTimer()
{
//noInterrupts();
TimerState = !RUN;
// turn the LED off to show timer is stopped
digitalWrite(LEDPin, LOW);
}
void Buzz(Freq,BuzzTime)
{
// turn buzzer on to alert user
tone(BuzzerPin, Freq, BuzzTime);
}
void ClearData()
{
A_count = 0;
B_count = 0;
A_Times [0,0,0,0,0,0,0,0,0,0];
B_Times [0,0,0,0,0,0,0,0,0,0];
}
void SendTimes()
{
Serial.println("Timer is stopped");
Serial.println("Here are the times for Shooter A");
Serial.print("Number of hits : ");
Serial.print("\t");
Serial.println(A_count);
Serial.println("A Hit times are : ");
for (int i=0; i < A_count ; i++)
{
Serial.println(A_Times[i]);
}
Serial.println("Here are the times for Shooter B");
Serial.print("Number of hits : ");
Serial.print("\t");
Serial.println(B_count);
Serial.println("B Hit times are : ");
for (int i=0; i < B_count ; i++)
{
Serial.println(B_Times[i]);
}
}
void ISR_A()
{
if(TimerState == RUN)
{
// store the hit time
A_Times[A_count] = millis() - StartTime;
// increment the hit count and array index
++A_count;
}
}
void ISR_B()
{
if(TimerState == RUN)
{
// store the hit time
B_Times[B_count] = millis() - StartTime;
// increment the hit count and array index
++B_count;
}
}