expected primary-expression before 'else'

I am attempting to move a servo with an IR remote but keep getting this message:
expected primary-expression before ‘else’

Here is my code:

#include <IRremote.h>
#include <Servo.h>

int RECV_PIN = 11; // the pin where you connect the output pin of TSOP4838
#define code1  0xFFA25D // code received from button A

Servo myservo;

int pos = 0;


IRrecv irrecv(RECV_PIN);

decode_results results;

void setup() {
  Serial.begin(9600);   // you can comment this line
  irrecv.enableIRIn();  // Start the receiver
  myservo.attach(1);
}

void loop() {
  if (irrecv.decode(&results)) {
    unsigned int value = results.value;
    switch (value) {
      case code1:
   
         if (else
            for  (pos = 0; pos <= 180; pos += 1) {
            myservo.write(pos);
            delay(15);
            )
          }
          for (pos = 180; pos >= 0; pos -= 1) {
            myservo.write(pos);
            delay(15);
          }
        }
    }
    Serial.println(value);
    irrecv.resume();
  }
}

Use the IDE autoformat tool (ctrl-t or Tools, Auto Format) on the code and you may see where it is going wrong.

if (else
            for  (pos = 0; pos <= 180; pos += 1) {
            myservo.write(pos);
            delay(15);
            )
          }

Extra parenthesis and syntax mistakes right there. I have no idea how to fix that cause I do not know what you are trying to do.

You have a lot more errors that that and they tell you a lot more, like what line the errors are on and what commands exactly it's complaining about. Please post the complete error listing. The "Copy error messages" button can help there.

You first error is writing 'if(else for (pos=....' which makes no sense. I can't suggest how to change it because I have no idea what you were hoping it would do.

Steve

What version of IRRemote library are you using? The newest version is not compatible with older version code. You will have to use an older version of the library or fix the code to be compatible with the new version.

Pin 1 is the hardware serial TX pin. Choose a different pin for the servo.

After autoformat:

   Serial.println(value);  // value is out of scope here.  Make it a global variable so it is visible here.
   irrecv.resume();
}
}  // an extra }

I found a version of IRRemote that works! Thanks for all of the help :slight_smile:

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