expected initializer before "buttonstate" token error message

Very first sketch! Having issues implementing variable.
Error code reads “expected initializer before “buttonstate” token”
This is my sketch;

Thank you!

#include <Servo.h>

long randomNumber;
Servo myservo;
int pos = 0;

int buttonPin = 7;
int ledPin = 8;
boolean on = false;
int buttonstate = 0;

void setup() {
myservo.attach(9);
Serial.begin(9600);
randomSeed(analogRead(0));

{
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop()

buttonstate = digitalRead(buttonPin);
if (buttonstate == HIGH) {
if (on == true) {
on = false;
} else {
on = true;
}
}

if (on == true) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}

delay(500);

{
randomNumber = random(10000);

Serial.print(“random:”);
Serial.println(randomNumber);

randomNumber = map(randomNumber, 0, 10000, 0, 180);

Serial.print(“servo:”);
Serial.println(randomNumber);
myservo.write(randomNumber);
/*
for (pos = 0; pos <= 180; pos += 1)
myservo.write(pos);
delay(100);

for (pos = 180; pos >= 0; pos -= 1)
myservo.write(pos);
delay(1000);
*/
delay(10000);
}
}

void loop()

buttonstate = digitalRead(buttonPin);

Missing { brace.

Missing code tags

TolpuddleSartre:

void loop()

buttonstate = digitalRead(buttonPin);



Missing { brace.

Missing code tags

I just placed a { brace before it and now it says; a function-definition is not allowed here before '{' token

Any idea how i can fix this?

I just placed a { brace before it

You placed before which "it"?

loop () is a function, just like setup().
Take a look and see if you can see how setup() begins.

I mean i put a {brace after void loop()

Like this:

void loop(){

buttonstate = digitalRead(buttonPin);
if (buttonstate == HIGH) {
if (on == true) {
on = false;
} else {
on = true;
}
}

I now gives me the message: a function-definition is not allowed here before '{' token
:frowning:

And it gives you a line where that happens.
If you change something, post the whole correction.
In code tags.

I think you need to put a space between void loop() and {

void loop() {

}

Also try this:

if (digitalRead(buttonPin) = HIGH) {

}

Usually that never works for me somehow.

#include <Servo.h>

long randomNumber;
Servo myservo;
int pos = 0;


int buttonPin = 7;
int ledPin = 8;
boolean on = false;
int buttonstate = 0;

void setup() {
  myservo.attach(9);
  Serial.begin(9600);
  randomSeed(analogRead(0));

  {
    pinMode(ledPin, OUTPUT);
    pinMode(buttonPin, INPUT);
  }

   void loop() {
buttonstate = digitalRead(buttonPin);
    if (buttonstate == HIGH) {
      if (on == true) {
        on = false;
      }  else {
        on = true;
      }
    }


  if (on == true) {
    digitalWrite(ledPin, HIGH);
  }
  else {
    digitalWrite(ledPin, LOW);
  }

  delay(500);
  
{
  randomNumber = random(10000);

  Serial.print("random:");
  Serial.println(randomNumber);

  randomNumber = map(randomNumber, 0, 10000, 0, 180);

  Serial.print("servo:");
  Serial.println(randomNumber);
  myservo.write(randomNumber);
  /*
    for (pos = 0; pos <= 180; pos += 1)
    myservo.write(pos);
    delay(100);

    for (pos = 180; pos >= 0; pos -= 1)
    myservo.write(pos);
    delay(1000);
  */
  delay(10000);
}
  }
 a function-definition is not allowed here before '{' token

I hope this is what you mean

Use this code:

#include <Servo.h>

long randomNumber;
Servo myservo;
int pos = 0;


int buttonPin = 7;
int ledPin = 8;
boolean on = false;
int buttonstate = 0;

void setup() {
  myservo.attach(9);
  Serial.begin(9600);
  randomSeed(analogRead(0));

    pinMode(ledPin, OUTPUT);
    pinMode(buttonPin, INPUT);
  }

   void loop() {
    buttonstate = digitalRead(buttonPin);
    if (buttonstate == HIGH) {
      if (on == true) {
        on = false;
      }  else {
        on = true;
      }
    }


  if (on == true) {
    digitalWrite(ledPin, HIGH);
  }
  else {
    digitalWrite(ledPin, LOW);
  }

  delay(500);
  
{
  randomNumber = random(10000);

  Serial.print("random:");
  Serial.println(randomNumber);

  randomNumber = map(randomNumber, 0, 10000, 0, 180);

  Serial.print("servo:");
  Serial.println(randomNumber);
  myservo.write(randomNumber);
  /*
    for (pos = 0; pos <= 180; pos += 1)
    myservo.write(pos);
    delay(100);

    for (pos = 180; pos >= 0; pos -= 1)
    myservo.write(pos);
    delay(1000);
  */
  delay(10000);
}
  }

You placed too many braces.

Hope this helps.

Hint: Use the IDE's auto format.
Your code looks like it has been typed by a barrelful of drunk monkeys.

Yes the code has no more error massages.

The onlly thing now is, that the cercuit i build (it haves a servo and a led + pushbutton, that i want to put on and off) does’t work for the led and button, onlly the servo does his work. any idea how to make it that i both works? or do i go to another forum to ask about this specific problem?

#include <Servo.h>

long randomNumber;
Servo myservo;
int pos = 0;


int buttonPin = 7;
int ledPin = 8;
boolean on = false;
int buttonstate = 0;

void setup() {
  myservo.attach(9);
  Serial.begin(9600);
  randomSeed(analogRead(0));

    pinMode(ledPin, OUTPUT);
    pinMode(buttonPin, INPUT);
  }

   void loop() {
    buttonstate = digitalRead(buttonPin);
    if (buttonstate == HIGH) {
      if (on == true) {
        on = false;
      }  else {
        on = true;
      }
    }


  if (on == true) {
    digitalWrite(ledPin, HIGH);
  }
  else {
    digitalWrite(ledPin, LOW);
  }

  delay(500);
  
{
  randomNumber = random(10000);

  Serial.print("random:");
  Serial.println(randomNumber);

  randomNumber = map(randomNumber, 0, 10000, 0, 180);

  Serial.print("servo:");
  Serial.println(randomNumber);
  myservo.write(randomNumber);
  /*
    for (pos = 0; pos <= 180; pos += 1)
    myservo.write(pos);
    delay(100);

    for (pos = 180; pos >= 0; pos -= 1)
    myservo.write(pos);
    delay(1000);
  */
  delay(10000);
}
  }

Unsigned_Arduino:
I think you need to put a space between void loop() and {

void loop() {

}

No, C++ doesn't care about a space there.

Also try this:

if (digitalRead(buttonPin) = HIGH) {

}




Usually that never works for me somehow.

That's because you can't assign HIGH to digitalRead(buttonPin). Try using the comparison operator == instead of the assignment operator =.

If tried it, but it does't seem to work. If i put onlly one = it turns on and off with the servo when it turns. I just want to make the survo move randomly and the LED put on and off when i push the button.

Whoops.

  if (digitalRead(buttonPin) == HIGH) {

}

:o