Expected unqualified-id before 'else' help

hello can you help me someone who is getting this error?

int buttonState = 0;

void setup()
{
pinMode(2, OUTPUT);

pinMode(3, OUTPUT);

pinMode(4, OUTPUT);

pinMode(5, OUTPUT);

pinMode(6, OUTPUT);

pinMode(7, OUTPUT);

pinMode(8, OUTPUT);

pinMode(9, OUTPUT);

pinMode(0, INPUT);
}

void loop()
{

buttonState = digitalRead(0);

if (buttonState == HIGH) {
{
for(int i=2;i<10;i++)

digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);

}

}

}
else
{

  for(int k=2;k<10;k++)

digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);

}

}

Did you forget one of these { ?
(Though, given the contents of the loop, it looks like the for is redundant)

Please remember to use code tags when posting code.

(Hint: the error message contains useful hints. Always post it/them)

Hi @jazzmagic
Please redo your initial post and enclose your code in </> tags.
If you don't know how to do this then why not read the initial thread when registering in the forum.
Then read: How to get the best out of this forum.

ArduinoForum

RV mineirin

@jazzmagic, Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

Please edit your post, select all code and click the </> button to apply so-called code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code.

After using tools -> autoformat in the IDE, your sketch looks like this

int buttonState = 0;

void setup()
{
  pinMode(2, OUTPUT);

  pinMode(3, OUTPUT);

  pinMode(4, OUTPUT);

  pinMode(5, OUTPUT);

  pinMode(6, OUTPUT);

  pinMode(7, OUTPUT);

  pinMode(8, OUTPUT);

  pinMode(9, OUTPUT);

  pinMode(0, INPUT);
}

void loop()
{

  buttonState = digitalRead(0);

  if (buttonState == HIGH) {
    {
      for (int i = 2; i < 10; i++)

        digitalWrite(2, HIGH);
      digitalWrite(3, HIGH);
      digitalWrite(3, HIGH);
      digitalWrite(4, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, HIGH);
      digitalWrite(7, HIGH);
      digitalWrite(8, HIGH);
      digitalWrite(9, HIGH);

    }

  }

}
else
{

  for (int k = 2; k < 10; k++)

    digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);

}

}

Two } at the beginning of a line at the end of the sketch after an autoformat indicates a mismatch of { and }.

An else at the beginning of a line indicates a problem. This is caused by the } just before that which indicates the end of the loop() function; so the rest of the code is outside a function.

The below after an autoformat might indicate a problem. It means that only digitalWrite(2, HIGH); is part of the for-loop; possibly not what you want.

      for (int i = 2; i < 10; i++)

        digitalWrite(2, HIGH);
      digitalWrite(3, HIGH);

It looks like you meant to write:

int buttonState = 0;

void setup()
{
  for (int i = 2; i < 10; i++)
    pinMode(i, OUTPUT);
}

void loop()
{
  buttonState = digitalRead(0);
  if (buttonState == HIGH)
  {
    for (int i = 2; i < 10; i++)
      digitalWrite(i, HIGH);
  }
  else
  {
    for (int k = 2; k < 10; k++)
      digitalWrite(k, LOW);
  }

}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.