Problem with if/else

I want to use 2x an IR sensor to switch 2 relais (as a start of a project), I'm using an Mega 2560.
In the sketch I have replaced the relais for LED's just to do some testing.
This is my code:

void setup()
{
  pinMode(10, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(53, INPUT);
  pinMode(49, INPUT);
//  Serial.begin(9600);
}
void loop()
{
  if (digitalRead(53) == LOW)
  {
    digitalWrite(12, HIGH);

    delay(12);
  }
  else
  {

    digitalWrite(12, LOW);
    delay(12);

  }

}
{
  if (digitalRead(49) == LOW)
  {
    digitalWrite(10, HIGH);

    delay(12);
  }
  else
  {

    digitalWrite(10, LOW);
    delay(12);

  }

}

If I compile the sketch I get an error:

IR_sensor:26:1: error: expected unqualified-id before '{' token
{
^
exit status 1
expected unqualified-id before '{' token

What am I doing wrong?

Take a look at where your loop function actually ends.

Look at your braces { }

Hello
Let run a "{" vs "}" counter simply.

That's your void loop() function, above.

This code is in a place where it shouldn't be.

Any time you do the same thing in an 'if' code block, as in the 'else' code block connected with it, you should move it outside the statement, as it doesn't depend on the condition at all.

You have an additional set of {} between your first if/else statement and the second one.
This should work:

void loop()
{
  if (digitalRead(53) == LOW) {
    digitalWrite(12, HIGH);
    delay(12);
  }
  else {
    digitalWrite(12, LOW);
    delay(12);
  }

  if (digitalRead(49) == LOW) {
    digitalWrite(10, HIGH);
    delay(12);
  }
  else {
    digitalWrite(10, LOW);
    delay(12);
  }
}

For what it's worth, Visual Studio has a handy dandy colour coded braces plugin. I've not actually used it but it sounds like something that might be handy in Arduino IDE too.

EDIT: Ah nice... Turns out the plugin in VSCode is now baked in. You just add "editor.bracketPairColorization.enabled": true to the settings.json file

It makes a difference too (don't shoot me in the leg for this code):

pgmPrintLCD((const char *)pgm_read_word(&ampTypeNames[allIndexes[11]]));
vs

You could just write

void loop() {
  digitalWrite(12, digitalRead(53) == LOW ? HIGH : LOW);
  delay(12);
  digitalWrite(10, digitalRead(49) == LOW ? HIGH : LOW);
  delay(12);
}

BTW. what are the delays for?

Thanks to all of you.
I will try your suggestíons tomorrow.

To give you some insights in the problems of using delay,
have a look at the following sketch.

It is basically your sketch, enhanced with some test code,
to count loop iterations per second and change the used delay value.
Of course some printing is also needed.
I changed the IO pins to something usable on my test Nano.

const uint8_t sensor1Pin = 2; // 53;
const uint8_t sensor2Pin = 3; // 49;
const uint8_t led1Pin = 12;
const uint8_t led2Pin = 10;

int8_t inconspicuousDelayValue = 12;

bool lowerDelay = true;
uint32_t lastResults;
uint32_t loops;

void setup() {
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  Serial.begin(115200);
}
void loop() {
  digitalWrite(led1Pin, digitalRead(sensor1Pin) == LOW ? HIGH : LOW);
  delay(inconspicuousDelayValue);
  digitalWrite(led2Pin, digitalRead(sensor2Pin) == LOW ? HIGH : LOW);
  delay(inconspicuousDelayValue);

  loops++;
  if (millis() - lastResults >= 1000) {
    Serial.print(F("two delays of "));
    Serial.print(inconspicuousDelayValue);
    Serial.print(F(" millis each result in "));
    Serial.print(loops);
    Serial.println(F(" loops per second"));
    if (lowerDelay) {
      if (--inconspicuousDelayValue < 0) {
        inconspicuousDelayValue = 1;
        lowerDelay = false;
      }
    } else {
      if (++inconspicuousDelayValue > 25) {
        inconspicuousDelayValue = 24;
        lowerDelay = true;
      }
    }
    loops = 0;
    lastResults = millis();
  }
}
two delays of 12 millis each result in 42 loops per second
two delays of 11 millis each result in 46 loops per second
two delays of 10 millis each result in 50 loops per second
two delays of 9 millis each result in 56 loops per second
two delays of 8 millis each result in 63 loops per second
two delays of 7 millis each result in 72 loops per second
two delays of 6 millis each result in 84 loops per second
two delays of 5 millis each result in 100 loops per second
two delays of 4 millis each result in 125 loops per second
two delays of 3 millis each result in 166 loops per second
two delays of 2 millis each result in 248 loops per second
two delays of 1 millis each result in 488 loops per second
two delays of 0 millis each result in 27549 loops per second
two delays of 1 millis each result in 488 loops per second
two delays of 2 millis each result in 248 loops per second
two delays of 3 millis each result in 166 loops per second
two delays of 4 millis each result in 125 loops per second
two delays of 5 millis each result in 100 loops per second
two delays of 6 millis each result in 84 loops per second
two delays of 7 millis each result in 72 loops per second
two delays of 8 millis each result in 63 loops per second
two delays of 9 millis each result in 56 loops per second
two delays of 10 millis each result in 50 loops per second
two delays of 11 millis each result in 46 loops per second
two delays of 12 millis each result in 42 loops per second
two delays of 13 millis each result in 39 loops per second
two delays of 14 millis each result in 36 loops per second
two delays of 15 millis each result in 34 loops per second
two delays of 16 millis each result in 32 loops per second
two delays of 17 millis each result in 30 loops per second
two delays of 18 millis each result in 28 loops per second
two delays of 19 millis each result in 27 loops per second
two delays of 20 millis each result in 25 loops per second
two delays of 21 millis each result in 24 loops per second
two delays of 22 millis each result in 23 loops per second
two delays of 23 millis each result in 22 loops per second
two delays of 24 millis each result in 21 loops per second
two delays of 25 millis each result in 20 loops per second

See how the count without a delay sticks out.

With a baud rate of 115200, roughly 11500 characters arrive through Serial,
with a buffer depth of 64 you need to empty that at least 180 times a second.

I prefer to grab each byte as it comes in, so I don't want any delays in the execution flow.

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