sspbass:
me_n_mac… The Arduino isn’t liking your code for using tone() to adjust the buzzer.
it says variable or field “Buzz” declared void.
Hmmm try this, I got rid of “Buzz”. It’s simpler actually.
// 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
tone(BuzzerPin, FreqHi, BuzzTime5)
// no need to debounce as mucho time has elapsed
}
else
{
// start stop button has been pushed again to stop timer
StopTimer();
tone(BuzzerPin, FreqLo, BuzzTime10);
// no need to debounce switch, delay inherit with tone
// 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();
tone(BuzzerPin, 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 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;
}
}
BTW you should probably AC couple your Arduino output pin to the piezo speaker. Put something like a 1 uF capacitor between the pin and the speaker. It may work connected directly but the speaker will be happier when AC coupled.