expected unqualified-id before "if"

I get this error on my code please help.

const int potPin = A0;//connect pot., button & leds
const int potPin2 = A1;
const int buttonPin = 2;
const int lightPin = 13;
const int red = 11;
const int green = 10;
const int blue = 9;



int buttonState = 0; //make variables to stor data
int potValue = 0;
int potValue2 = 0;

 int redValue = 255;   //special RGB variables
 int blueValue = 100;
 int greenValue = 5;
 int normal = 6;


void setup(){
  Serial.begin(9600);
  pinMode(lightPin, OUTPUT);
  pinMode(red,OUTPUT);
  pinMode(green,OUTPUT);
  pinMode(blue, OUTPUT);
}

void loop(){
if(potValue >= 1018){ 
      Serial.println("MAIN MENU: light [LED] reset");
      delay(400);
      if(potValue2 > 0 || potValue2 < 868){
        Serial.println("Ready to change color press button, back");
        delay(500);
         if(buttonState == HIGH){
          analogWrite(red, redValue);
          analogWrite(blue, blueValue);
          analogWrite(green, greenValue);

          greenValue = greenValue + 1;
          if(greenValue > 255){
            greenValue = 5;
          }
            if(buttonState == LOW){
              analogWrite(red, redValue);
              analogWrite(blue, blueValue);
              analogWrite(green, normal);
            }
         }
      }
    }
        if(potValue2 >= 864 || potValue < 1018){
      Serial.println("Ready to change color, [back]");
      delay(300);
      if(buttonState == HIGH){
        Serial.println("MAIN MENU: light LED  reset");
        delay(500);
      }
    }
    else{
       Serial.println("MAIN MENU: light LED  reset");
        delay(500);
    }

sorry for the super long code. It highlights "if(potValue2 >= 864 || potValue < 1018){" and says "expected unqualified-id before 'if' "

thank you in advance

Looks like you have code outside the loop() function.

.

it is really hard for me to find it can you tell me where

Match opening { to closing }

The last } in the sketch should match with the first { in loop()

Use CTRL T to format your sketches.
.

i fixed that but, I still get the same problem

i fixed that but, I still get the same problem

Then, you didn't fix it properly.

Put EVERY { on a line BY ITSELF.
Put EVERY } on a line BY ITSELF.
I'd say to use Tools + Auto Format to properly indent your code, but you won't be able to since you do not have the same number of { and }. So, manually indent your code until you find the { and doesn't have a } or the } that doesn't have a {.

thank you for your help

Seems you are missing the closing "}" for the loop. Try using auto format under the Tools header. Go to the bottom and click on the last }. Notice how the curly bracket right above it in the else statement is highlighted? That is it's mate. Loop also needs it's closing curly brace at the end.

Basically, put another } at the very end of the sketch

whats is the wrong with this code help please ?

// PCA Ship define
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

#define MIN_PULSE_WIDTH 650
#define MAX_PULSE_WIDTH 2350
#define DEFAULT_PULSE_WIDTH 1500
#define FREQUENCY 50

// define Dc motor driver with joystick

#define enA 10
#define in1 2
#define in2 3
#define enB 11
#define in3 4
#define in4 5

int motorSpeedA = 0;
int motorSpeedB = 0;

// define powerscrew

#define enC 12
#define in5 6
#define in6 7
#define enD 13
#define in7 8
#define in8 9
#define switchA 53

int motorSpeedC = 0;

// define servos
uint8_t s1 = 0;
uint8_t s2 = 2;
uint8_t s3 = 4;
uint8_t s4 = 6;
//uint8_t s5 = 8;
//uint8_t s6 = 10;

// define buttons
int button1 = 41;
int button2 = 43;
int button3 = 45;
int button4 = 47;
int button5 = 49;

void setup()
{
// Servos

Serial.begin(9600);
Serial.println("16 channel Servo test!");
pwm.begin();
pwm.setPWMFreq(FREQUENCY);
pwm.setPWM(0, 0, 0);
pwm.setPWM(2, 0, 0);

// Dc motors
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(enC, OUTPUT);
pinMode(in5, OUTPUT);
pinMode(in6, OUTPUT);
}
int pulseWidth(int angle)
{
int pulse_wide, analog_value;
pulse_wide = map(angle, 0, 180, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
analog_value = int(float(pulse_wide) / 1000000 * FREQUENCY * 4096);
Serial.println(analog_value);
return analog_value;
}

void loop() {
// Servos loop
if (digitalRead(button1) == HIGH) // position 1 for chair
{
pwm.setPWM(0, 0, pulseWidth(90));

pwm.setPWM(2, 0, pulseWidth(90));

pwm.setPWM(4, 0, pulseWidth(90));

pwm.setPWM(6, 0, pulseWidth(90));

//pwm.setPWM(8, 0, pulseWidth(90));

//pwm.setPWM(10, 0, pulseWidth(90));

digitalWrite(in5, LOW);
digitalWrite(in6, HIGH);
// Set Motor C forward
digitalWrite(in7, LOW);
digitalWrite(in8, HIGH);
motorSpeedC = 255;

delay(1000);

}
else if (digitalRead(button2) == HIGH) // position 2 for chair
{
pwm.setPWM(0, 0, pulseWidth(10));

pwm.setPWM(2, 0, pulseWidth(10));

pwm.setPWM(4, 0, pulseWidth(10));

pwm.setPWM(6, 0, pulseWidth(10));

//pwm.setPWM(8, 0, pulseWidth(0));

//pwm.setPWM(10, 0, pulseWidth(0));

digitalWrite(in5, HIGH);
digitalWrite(in6, LOW);
// Set Motor c backward
digitalWrite(in7, HIGH);
digitalWrite(in8, LOW);
motorSpeedC = 255;

delay(1000);
}

else if (digitalRead(button3) == HIGH) // position 3 for chair
{
pwm.setPWM(0, 0, pulseWidth(170));

pwm.setPWM(2, 0, pulseWidth(170));

pwm.setPWM(4, 0, pulseWidth(170));

pwm.setPWM(6, 0, pulseWidth(170));

//pwm.setPWM(8, 0, pulseWidth(180));

//pwm.setPWM(10, 0, pulseWidth(180));

digitalWrite(in5, HIGH);
digitalWrite(in6, LOW);
// Set Motor c backward
digitalWrite(in7, HIGH);
digitalWrite(in8, LOW);
motorSpeedC = 255;

delay(1000);
}
if (digitalRead(switchA) == LOW )
{
motorSpeedA = 0;
}
analogWrite(enC, motorSpeedC);
analogWrite(enD, motorSpeedC);
}

/*if (digitalRead(button2) == HIGH)

pwm.setPWM(0, 0, pulseWidth(180));
pwm.setPWM(0, 0, pulseWidth(180));
pwm.setPWM(0, 0, pulseWidth(180));
pwm.setPWM(0, 0, pulseWidth(180));
delay(1000);*/

// Dc motor loop

int xAxis = analogRead(A0); // Read Joysticks X-axis
int yAxis = analogRead(A1); // Read Joysticks Y-axis
// Y-axis used for forward and backward control
if (yAxis < 470) {
// Set Motor A backward
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Set Motor B backward
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
motorSpeedA = map(yAxis, 470, 0, 0, 255);
motorSpeedB = map(yAxis, 470, 0, 0, 255);
}
else if (yAxis > 550) {
// Set Motor A forward
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// Set Motor B forward
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
// Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
motorSpeedA = map(yAxis, 550, 1023, 0, 255);
motorSpeedB = map(yAxis, 550, 1023, 0, 255);
}
// If joystick stays in middle the motors are not moving
else {
motorSpeedA = 0;
motorSpeedB = 0;
}
// X-axis used for left and right control
if (xAxis < 470) {
// Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
int xMapped = map(xAxis, 470, 0, 0, 255);
// Move to left - decrease left motor speed, increase right motor speed
motorSpeedA = motorSpeedA - xMapped;
motorSpeedB = motorSpeedB + xMapped;
// Confine the range from 0 to 255
if (motorSpeedA < 0) {
motorSpeedA = 0;
}
if (motorSpeedB > 255) {
motorSpeedB = 255;
}
}
if (xAxis > 550) {
// Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
int xMapped = map(xAxis, 550, 1023, 0, 255);
// Move right - decrease right motor speed, increase left motor speed
motorSpeedA = motorSpeedA + xMapped;
motorSpeedB = motorSpeedB - xMapped;
// Confine the range from 0 to 255
if (motorSpeedA > 255) {
motorSpeedA = 255;
}
if (motorSpeedB < 0) {
motorSpeedB = 0;
}
}
// Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)
if (motorSpeedA < 70) {
motorSpeedA = 0;
}
if (motorSpeedB < 70) {
motorSpeedB = 0;
}

analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
analogWrite(enB, motorSpeedB); // Send PWM signal to motor B

}

The first thing that is wrong is that you did not use code tags to post your code. Read How to use this forum - please read, specifically point 7.

Second thing is that you don't describe what is wrong. Does it compile? If not, give the complete error message. If it does compile and load, describe what you expect and what it actually does.

You did not really read the replies above, did you. It was mentioned to use the auto-format functionality in the IDE. That will properly indent your code and makes it easier to spot errors.

After auto-format, lines 166 to 168

  analogWrite(enC, motorSpeedC);
  analogWrite(enD, motorSpeedC);
}

That } at the beginning of the line indicates that that is where a function (loop() in this case) ends. Anything after that is in the wrong place.

Oh, and the last point: cross posting is not appreciated. Let's continue in your own thread

i've got that error message ( expected unqualified-id before 'if' ) so what is the problem ?

this is the code :

// PCA Ship define
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

#define MIN_PULSE_WIDTH 650
#define MAX_PULSE_WIDTH 2350
#define DEFAULT_PULSE_WIDTH 1500
#define FREQUENCY 50

// define Dc motor driver with joystick

#define enA 10
#define in1 2
#define in2 3
#define enB 11
#define in3 4
#define in4 5

int motorSpeedA = 0;
int motorSpeedB = 0;

// define powerscrew

#define enC 12
#define in5 6
#define in6 7
#define enD 13
#define in7 8
#define in8 9
#define switchA 53

int motorSpeedC = 0;

// define servos
uint8_t s1 = 0;
uint8_t s2 = 2;
uint8_t s3 = 4;
uint8_t s4 = 6;
//uint8_t s5 = 8;
//uint8_t s6 = 10;

// define buttons
int button1 = 41;
int button2 = 43;
int button3 = 45;
int button4 = 47;
int button5 = 49;

void setup()
{
// Servos

Serial.begin(9600);
Serial.println("16 channel Servo test!");
pwm.begin();
pwm.setPWMFreq(FREQUENCY);
pwm.setPWM(0, 0, 0);
pwm.setPWM(2, 0, 0);

// Dc motors
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(enC, OUTPUT);
pinMode(in5, OUTPUT);
pinMode(in6, OUTPUT);
}
int pulseWidth(int angle)
{
int pulse_wide, analog_value;
pulse_wide = map(angle, 0, 180, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
analog_value = int(float(pulse_wide) / 1000000 * FREQUENCY * 4096);
Serial.println(analog_value);
return analog_value;
}

void loop() {
// Servos loop
if (digitalRead(button1) == HIGH) // position 1 for chair
{
pwm.setPWM(0, 0, pulseWidth(90));

pwm.setPWM(2, 0, pulseWidth(90));

pwm.setPWM(4, 0, pulseWidth(90));

pwm.setPWM(6, 0, pulseWidth(90));

//pwm.setPWM(8, 0, pulseWidth(90));

//pwm.setPWM(10, 0, pulseWidth(90));

digitalWrite(in5, LOW);
digitalWrite(in6, HIGH);
// Set Motor C forward
digitalWrite(in7, LOW);
digitalWrite(in8, HIGH);
motorSpeedC = 255;

delay(1000);

}
else if (digitalRead(button2) == HIGH) // position 2 for chair
{
pwm.setPWM(0, 0, pulseWidth(10));

pwm.setPWM(2, 0, pulseWidth(10));

pwm.setPWM(4, 0, pulseWidth(10));

pwm.setPWM(6, 0, pulseWidth(10));

//pwm.setPWM(8, 0, pulseWidth(0));

//pwm.setPWM(10, 0, pulseWidth(0));

digitalWrite(in5, HIGH);
digitalWrite(in6, LOW);
// Set Motor c backward
digitalWrite(in7, HIGH);
digitalWrite(in8, LOW);
motorSpeedC = 255;

delay(1000);
}

else if (digitalRead(button3) == HIGH) // position 3 for chair
{
pwm.setPWM(0, 0, pulseWidth(170));

pwm.setPWM(2, 0, pulseWidth(170));

pwm.setPWM(4, 0, pulseWidth(170));

pwm.setPWM(6, 0, pulseWidth(170));

//pwm.setPWM(8, 0, pulseWidth(180));

//pwm.setPWM(10, 0, pulseWidth(180));

digitalWrite(in5, HIGH);
digitalWrite(in6, LOW);
// Set Motor c backward
digitalWrite(in7, HIGH);
digitalWrite(in8, LOW);
motorSpeedC = 255;

delay(1000);
}
if (digitalRead(switchA) == LOW )
{
motorSpeedA = 0;
}
analogWrite(enC, motorSpeedC);
analogWrite(enD, motorSpeedC);
}

/*if (digitalRead(button2) == HIGH)

pwm.setPWM(0, 0, pulseWidth(180));
pwm.setPWM(0, 0, pulseWidth(180));
pwm.setPWM(0, 0, pulseWidth(180));
pwm.setPWM(0, 0, pulseWidth(180));
delay(1000);*/

// Dc motor loop

int xAxis = analogRead(A0); // Read Joysticks X-axis
int yAxis = analogRead(A1); // Read Joysticks Y-axis
// Y-axis used for forward and backward control
if (yAxis < 470) {
// Set Motor A backward
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Set Motor B backward
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
motorSpeedA = map(yAxis, 470, 0, 0, 255);
motorSpeedB = map(yAxis, 470, 0, 0, 255);
}
else if (yAxis > 550) {
// Set Motor A forward
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// Set Motor B forward
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
// Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
motorSpeedA = map(yAxis, 550, 1023, 0, 255);
motorSpeedB = map(yAxis, 550, 1023, 0, 255);
}
// If joystick stays in middle the motors are not moving
else {
motorSpeedA = 0;
motorSpeedB = 0;
}
// X-axis used for left and right control
if (xAxis < 470) {
// Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
int xMapped = map(xAxis, 470, 0, 0, 255);
// Move to left - decrease left motor speed, increase right motor speed
motorSpeedA = motorSpeedA - xMapped;
motorSpeedB = motorSpeedB + xMapped;
// Confine the range from 0 to 255
if (motorSpeedA < 0) {
motorSpeedA = 0;
}
if (motorSpeedB > 255) {
motorSpeedB = 255;
}
}
if (xAxis > 550) {
// Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
int xMapped = map(xAxis, 550, 1023, 0, 255);
// Move right - decrease right motor speed, increase left motor speed
motorSpeedA = motorSpeedA + xMapped;
motorSpeedB = motorSpeedB - xMapped;
// Confine the range from 0 to 255
if (motorSpeedA > 255) {
motorSpeedA = 255;
}
if (motorSpeedB < 0) {
motorSpeedB = 0;
}
}
// Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)
if (motorSpeedA < 70) {
motorSpeedA = 0;
}
if (motorSpeedB < 70) {
motorSpeedB = 0;
}

analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
analogWrite(enB, motorSpeedB); // Send PWM signal to motor B

}

Did you follow the first link in reply #9 ?

Is that still the same code; or did you fix what I indicated?

so what is the problem ?

The problem is that you don't have an "expected unqualified-id before 'if'".

Or something. I'm 100% certain that that is not the correct paraphrasing of the error message, and I am 100% certain that the compiler to you which line the error occurred on.

Your indenting is horrid. If you are too lazy to indent properly as you type code, at least use Tools + Auto Format before you foist that mess off on us.

And, where the hell are the code tags you are supposed to have used?

i fix what i indicated :slight_smile:

the problem is about the line

// X-axis used for left and right control

if (xAxis < 470)

Hala_hamdy:
the problem is about the line

// X-axis used for left and right control

if (xAxis < 470)

The compiler told you the line number that the error is on.

Now, compare that line number to the line number where loop() ends.

If loop() ends first (it does), you have code that is not in a function, and that is NOT allowed.

If you properly indented your code, or used Tools + Auto Format, that would be perfectly obvious.