I'm new to programming and I'm not sure what is causing the problem. This was meant to be a reaction time tester.
I keep getting the following errors when I compile:
ReactionTime3___Sound.ino: In function 'void loop()':
ReactionTime3___Sound:50: error: expected initializer before 'if'
ReactionTime3___Sound:132: error: expected `}' at end of input
The line highlighted when I compile is on the left parenthesis:
if(potPlayers == 1)
Here is my Auto Formatted Code:
long randNumber;
int inputPin = 3; // create a switch input
int inputPin2 = 5; //switch input 2
int ledPin = 2; // create output pin for LED
int ledPin2 = 4; //input pin for LED2
int potPin = A1; //create potentiometer input
const int buzzer = 9;
const int songLength = 18; //change later
char notes[] = "cdfda ag cdfdg gf "; //change later (space = rest)
int beats[] = {
1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2}; //change later
int tempo = 150;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(ledPin2, OUTPUT);
pinMode(inputPin, INPUT); // declare pushbutton as input
digitalWrite(inputPin,LOW); // enable pull-up resistors
pinMode(inputPin2, INPUT);
digitalWrite(inputPin2, LOW);
pinMode(buzzer, OUTPUT);
}
void loop()
{
int pot = analogRead(potPin);
int potPlayers = ((int)(pot / 1023)+1);
// Serial.print("Potent value: ");
//Serial.println(pot);
Serial.print("Players: ");
Serial.println(potPlayers);
long randDelay = random(1000);
int state = 1;
int state2 = 1;
long count = 0; //counter for player 1
long count2 = 0; //counter for player 2
delay(randDelay);
int frequency(char note)
//digitalWrite(ledPin, HIGH);
//digitalWrite(ledPin2, HIGH);
if(potPlayers == 1)
{
while(state == 1)
{
int val = digitalRead(inputPin); // read input value
if (val == HIGH) // check if the switch is pressed
{
digitalWrite(ledPin, LOW); // turn LED off if switch is pressed
state = 0;
}
else
{
count++;
delay(1);
}
}
Serial.println(count);
}
else
{
while(state == 1)
{
int val = digitalRead(inputPin); //read input value
int val2 = digitalRead(inputPin2); //read input value for pin2
if(val == HIGH || val2 == HIGH)
{
if(val == HIGH)
{
digitalWrite(ledPin, LOW);
Serial.println("Yellow wins!");
}
else if(val2 == HIGH)
{
digitalWrite(ledPin2, LOW);
Serial.println("Red wins!");
}
int i, duration;
for(i = 0; i < songLength; i++) //step through song
{
duration = beats[i] * tempo; //length of note/rest in ms
if(notes[i] == ' ')
{
delay(duration);
}
else
{
tone(buzzer, frequency(notes[i]), duration);
delay(duration); //wait for tone to finish
}
delay(tempo/10); //pause between notes
}
state = 0;
}
}
}
int frequency(char note)
//returns frequency that corresponds with the note
int i;
const int numNotes = 8; //number of notes stored
char names[] = {
'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int frequencies[] = {
262, 294, 330, 349, 392, 440, 494, 523 };
//Search through the letter, and if found, return the frequency
for(i = 0; i < numNotes; i++) //step through notes
{
if(names[i] == note) //check if it equals the note
{
return(frequencies[i]); //return the frequency
}
}
return(0); //if it was not found, zero is returned
delay(1000);
}
Thanks in advance for any replies I receive!