0
votes

I am trying to use my arduino to make a reaction game where it chooses a random length of time before turning the built in LED on where it waits for you to press the button then gives you a score of fast or slow, the problem is that when I try to upload it to the arduino (all settings are correct) it comes up with an error message on certain lines that are the exact same as another programme that is different bust uses the same things and works.

const int buttonPin = 4;
const int ledPin1 = 3;
const int ledPin2 = 2;
//~~~~~~~~~~##~~~~~~~~~~\\
int buttonState = 0;
int oldButtonState = 0;
int wait = 0;
bool isLoop = true;
//~~~~~~~~~~##~~~~~~~~~~\\
void setup() {
  Serial.begin(9600);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(buttonPin, INPUT);
}
digitalWrite(LED_BUILTIN, LOW);
//~~~~~~~~~~##~~~~~~~~~~\\
void loop() {
  // put your main code here, to run repeatedly:
  randInt = random(500, 5001);
  delay(randInt);
  digitalWrite(LED_BUILTIN, HIGH);
  while (isLoop == true){
    buttonState = digitalRead(buttonPin);
    if (buttonState == LOW){
      isLoop = false;
    }
  }
  wait = millis() - randInt
  Serial.println(String(wait));
  if (wait <= 1000){
    digitalWrite(ledPin1, HIGH);
  }else{
    digitalWrite(ledPin2, HIGH);
  }
  delay(2000);
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
}

the above is my code, and when I try to upload it I get the following error message:

Arduino: 1.8.5 (Windows Store 1.8.10.0) (Windows 10), Board: "Arduino/Genuino Uno"

Reaction_Game:11: error: 'Serial' does not name a type

Serial.begin(9600);

^

Reaction_Game:12: error: expected constructor, destructor, or type conversion before '(' token

pinMode(ledPin1, OUTPUT);

      ^

Reaction_Game:13: error: expected constructor, destructor, or type conversion before '(' token

pinMode(ledPin2, OUTPUT);

      ^

Reaction_Game:14: error: expected constructor, destructor, or type conversion before '(' token

pinMode(LED_BUILTIN, OUTPUT);

      ^

Reaction_Game:15: error: expected constructor, destructor, or type conversion before '(' token

pinMode(buttonPin, INPUT);

      ^

Reaction_Game:16: error: expected declaration before '}' token

}

^

exit status 1 'Serial' does not name a type

This report would have more information with "Show verbose output during compilation" option enabled in File -> Preferences.

this is despite the fact that this exact line:

Serial.begin(96000);

is in another programme that works perfectly (same place and everything)

could someone please help me? I have almost given up on this after finding nothing.

2

2 Answers

0
votes

1 Get rid of these weird useless comments

//~~~~~~~~~~##~~~~~~~~~~\\

2 This is outside of void setup

digitalWrite(LED_BUILTIN, LOW);

3 Missing semicolon here

wait = millis() - randInt

4 Declarate randInt, put int before this line

randInt = random(500, 5001);

5 Tip 1

while (isLoop == true){

can be written as

while (isLoop) {

6 Tip 2, You dont have to stringify long

Serial.println(String(wait));
0
votes

The following line is outside the void setup function:

digitalWrite(LED_BUILTIN, LOW);

You also need a semicolon in

wait = millis() - randInt

Declare int as the datatype in

randInt = random(500, 5001);