Go Down

Topic: ^ exit status 1 expected primary-expression before '||' token (Read 677 times) previous topic - next topic

PaulSn

I am getting this error  while compiling and I'm not sure why. I'm new to arduino, so go easy on me. Please advise, thanks!!



error:

uav_payload_receiver:33: error: expected primary-expression before '||' token

     if(input == 0000) || (digitalRead(10) == LOW) {//if off code is received and no test button, all off and servo to 90

                       ^

uav_payload_receiver:43: error: 'input' was not declared in this scope

     if(input == 1111){//if on code is received

        ^

Using library SoftwareSerial at version 1.0 in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SoftwareSerial
Using library Servo at version 1.1.2 in folder: C:\Program Files (x86)\Arduino\libraries\Servo
exit status 1
expected primary-expression before '||' token


Code: [Select]

/*
  5 channel remote receiver for UAV tooling. 1
  servo rotator and 4 power FETs
  Author Paul Sneed
  based on:
  HC-12 Momentary Button Receive
  Author Tom Heylen tomtomheylen.com
*/

#include <SoftwareSerial.h>
#include <Servo.h>

SoftwareSerial mySerial(0, 1); // RX, TX
Servo servo_9;

void setup()
{
  mySerial.begin(9600);
  pinMode(10, INPUT);
  pinMode(13, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  servo_9.attach(9);

}

void loop() {

   if(mySerial.available() > 1){
    int input = mySerial.parseInt();//read serial input and convert to integer (-32,768 to 32,767)
    if(input == 0000) || (digitalRead(10) == LOW) {//if off code is received and no test button, all off and servo to 90
      digitalWrite(13, LOW);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, LOW);
      servo_9.write(90);
      delay(20); // Wait for 20 millisecond(s)
      }
  }
    if(input == 1111){//if on code is received
    // take code 1111 to rotate servo45
    digitalWrite(13, HIGH);
    servo_9.write(45);
    delay(20); // Wait for 20 millisecond(s)
  } else {
    if(input == 2222) {
      // take code 2222 to turn on FET2
      digitalWrite(13, HIGH);
      digitalWrite(4, HIGH);
      delay(20); // Wait for 20 millisecond(s)
    } else {
      if(input == 3333) {
       // take code 3333 to turn on FET3
        digitalWrite(13, HIGH);
        digitalWrite(5, HIGH);
        delay(20); // Wait for 20 millisecond(s)
      } else {
        if(input == 4444) {
          digitalWrite(13, HIGH);
          digitalWrite(6, HIGH);
          delay(20); // Wait for 20 millisecond(s)
        } else {
          if(input == 5555) {
            digitalWrite(13, HIGH);
            digitalWrite(7, HIGH);
            delay(20); // Wait for 20 millisecond(s)
          } else {
            if (digitalRead(10) == HIGH) {
              // test button, all fets on and servo45
              servo_9.write(45);
              digitalWrite(4, HIGH);
              digitalWrite(5, HIGH);
              digitalWrite(6, HIGH);
              digitalWrite(7, HIGH);
              digitalWrite(13, HIGH);
              delay(20); // Wait for 20 millisecond(s)
            }
          }
        }
      }
    }
  }
  mySerial.flush();//clear the serial buffer for unwanted inputs     
 
  delay(20);//delay little for better serial communication
 
}





DKWatson

Yes, your first nested if statement needs some more parentheses.

Second issue is that input is being declared inside the first if test and as such has no scope in the next. You need to move int input; out of the if, just inside loop(). Replace statement in if with input = mySerial.parseInt();
Live as if you were to die tomorrow. Learn as if you were to live forever. - Mahatma Gandhi

PaulSn

great, thanks!.. I'm not sure I completely understand..

does int input = mySerial.parseInt();//read my.Serial input and convert to integer (-32,768 to 32,767) need to be out of the loop?, up near int pos = 170;?. or are you saying to make it input = mySerial.parseInt(); where it is, and lose the int?.

I got this one to verify in arduinoIDE, I have not uploaded it yet.

You all are awesome!..

Code: [Select]
/*
  5 channel remote receiver for UAV tooling. 1
  servo rotator and 4 power FETs
  Author Paul Sneed
  based on:
  HC-12 Momentary Button Receive
  Author Tom Heylen tomtomheylen.com
*/

#include <Servo.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(0, 1); //RX, TX

Servo servo_9;

int pos = 170;


void setup()
{
  mySerial.begin(9600);
  pinMode(10, INPUT);
  pinMode(13, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  servo_9.attach(9);

}

void loop()
{
  if(mySerial.available() > 1){   
    int input = mySerial.parseInt();//read my.Serial input and convert to integer (-32,768 to 32,767)
    if (mySerial.read() == 0 || digitalRead(10) == LOW) {
    // no inputs, all low and servo 90
    digitalWrite(13, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    servo_9.write(90);
    delay(20); // Wait for 20 millisecond(s)
  }
  if (mySerial.read() == 1111) {
    // take code 1111 to rotate servo
    digitalWrite(13, HIGH);
    servo_9.write(45);
    delay(20); // Wait for 20 millisecond(s)
  } else {
    if (mySerial.read() == 2222) {
      // take code 2222 to turn on FET
      digitalWrite(13, HIGH);
      digitalWrite(4, HIGH);
      delay(20); // Wait for 20 millisecond(s)
    } else {
      if (mySerial.read() == 3333) {
        digitalWrite(13, HIGH);
        digitalWrite(5, HIGH);
        delay(20); // Wait for 20 millisecond(s)
      } else {
        if (mySerial.read() == 4444) {
          digitalWrite(13, HIGH);
          digitalWrite(6, HIGH);
          delay(20); // Wait for 20 millisecond(s)
        } else {
          if (mySerial.read() == 5555) {
            digitalWrite(13, HIGH);
            digitalWrite(7, HIGH);
            delay(20); // Wait for 20 millisecond(s)
          } else {
            if (digitalRead(10) == HIGH) {
              // test button, all on and servo45
              servo_9.write(45);
              digitalWrite(4, HIGH);
              digitalWrite(5, HIGH);
              digitalWrite(6, HIGH);
              digitalWrite(7, HIGH);
              digitalWrite(13, HIGH);
              delay(20); // Wait for 20 millisecond(s)
            }
          }
        }
      }
    }
  }
}
  mySerial.flush();//clear the serial buffer for unwanted inputs     
  delay(20);//delay little for better serial communication
}


MorganS

Your blocks aren't lined up properly. Use the auto-format tool in the IDE to fix it.

You seem to read an integer in the variable input and then ignore that value, trying to read from mySerial again and again. The mySerial buffer will be empty by that point.

Also, the single character read by read() could never contain the value 1111. It is simply too small for a number that big.
"The problem is in the code you didn't post."

PaulSn

ok.. should I use something like a if (mySerial.read() == 1) or something?. the information it reads is not critical, I can have the transmitter send whatever this receiver wants to read. I think the transmitter/remote is working, I tested it on the serial monitor and the button presses send the serial print as expected (I'm sure I'll be here to fix that one also, lol).

I ran the auto-format, not sure what changed. also deleted a bunch of the delay(20);s, I don't think they are necessary until the last one.

how do I fix the mySerial.read() parts?, and the parseInt parts?.

Thanks!

MorganS

You fix it by using the value you read instead of trying to read again.

From the Arduino's point of view, Serial is glacially slow. It is like a friend sending you an email with only one character and one whole week waiting to get the next character in your mail.

Yes, reading one-character commands is easiest, if the commands are all simple. The only problem with your suggestion is that you cant type 1 on your keyboard. Try this...

Code: [Select]
char inputChar;
if(mySerial.available()) {
  inputChar = mySerial.read();
  switch(inputChar) {
    case '1':
      //do command 1
      break;
    case 'g':
      //do command g
      break;
    case '\n':
    case '\r':
      //ignore newline and linefeed
      break;
    default:
      Serial.print("Unrecognised command: ");
      Serial.println(inputChar);
  }
}
"The problem is in the code you didn't post."

Go Up