expected unqualified-id before 'if'

the code doesn't work!

 //Arduino PLL Speed Control?
int E1 = 4;   
int M1 = 5;  
int E2 = 7;                         
int M2 = 6; 
int tilt_s1 = 2;
int tilt_s2 = 3;

void setup() 
{ 
 pinMode(M1, OUTPUT);
 pinMode(M2, OUTPUT); 
 pinMode(tilt_s1, INPUT);
 pinMode(tilt_s2, INPUT);
 Serial.begin(9600);   
} 
 
void loop(){
  int position = getTiltPosition();
  Serial.println(position);
  delay(200);
}  

int getTiltPosition(){
  int s1 = digitalRead(tilt_s1);
  int s2 = digitalRead(tilt_s2);
  return (s1 << 1) | s2;
} 

if (position = 3) 
  {
  int value
  for(value = 0 ; value <= 1; value+=5) 
  {
    digitalWrite(M1, HIGH);  
    digitalWrite(M2, HIGH);
    analogWrite(E2, 1);    
    analogWrite(E1, 1);   //PLL Speed Control
    delay(30); 
  }
  
  }   
{
else
  int value
  for(value = 0 ; value <= 1; value+=5) 
  {
    digitalWrite(M1, LOW);  
    digitalWrite(M2, LOW);    
    analogWrite(E1, 1);
    analogWrite(E2, 1);   //PLL Speed Control
    delay(30); 
  }  

}

All code to be executed needs to be in a function.

your if statement is not in a function.

Also, you should be using == rather than = in the if.

it still doesn't work

error: expected unqualified-id before 'if'

how do you make the if statement in a function?

WizenedEE:
All code to be executed needs to be in a function.

your if statement is not in a function.

Also, you should be using == rather than = in the if.

cekstuffertz:
it still doesn't work

error: expected unqualified-id before 'if'

Post the updated code, so we know you did it right.

cekstuffertz:
how do you make the if statement in a function?

You don't, you put it into a function.

sorry I am a newbie. So how do you put it in a function (i'm dutch and 12 years so sorry for my english)

cekstuffertz:
sorry I am a newbie. So how do you put it in a function (i'm dutch and 12 years so sorry for my english)

Assuming you don't have to return a value:

void someFunctionName()
{
  // code goes here
}

Assuming you do have to return a value (int in this example:

int someFunctionName()
{
  // code goes here
  return someInt;
}

Thank You!

But now its shows another error!

//Arduino PLL Speed Control?
int E1 = 4;   
int M1 = 5;  
int E2 = 7;                         
int M2 = 6; 
int tilt_s1 = 2;
int tilt_s2 = 3;

void setup() 
{ 
 pinMode(M1, OUTPUT);
 pinMode(M2, OUTPUT); 
 pinMode(tilt_s1, INPUT);
 pinMode(tilt_s2, INPUT);
 Serial.begin(9600);   
} 
 
void loop(){
  int position = getTiltPosition();
  Serial.println(position);
  delay(200);
  

int getTiltPosition(){
  int s1 = digitalRead(tilt_s1);
  int s2 = digitalRead(tilt_s2);
  return (s1 << 1) | s2;
 
}
if (position == 3) 
  {
  int value
  for(value = 0 ; value <= 1; value+=5) 
  {
    digitalWrite(M1, HIGH);  
    digitalWrite(M2, HIGH);
    analogWrite(E2, 1);    
    analogWrite(E1, 1);   //PLL Speed Control
    delay(30); 
  }
  
 }  
  
     
{
else
  int value
  for(value = 0 ; value <= 1; value+=5) 
  {
    digitalWrite(M1, LOW);  
    digitalWrite(M2, LOW);    
    analogWrite(E1, 1);
    analogWrite(E2, 1);   //PLL Speed Control
    delay(30); 
  }  

 }

ino: In function 'void loop()':
error: 'getTiltPosition' was not declared in this scope
error: a function-definition is not allowed here before '{' token
error: expected initializer before 'for'
error: 'value' was not declared in this scope
error: expected ;' before ')' token error: 'else' without a previous 'if' error: expected initializer before 'for' error: 'value' was not declared in this scope error: expected ;' before ')' token

Can you give me an example of how i am supposed to do it because i don't have much time left!

You can't have a function nested within another function:

void someFunction()
{
  // some code here
  int someNestedFunction()
  {
    // code
    return someInt;
  }
}

Functions need to be separate:

void someFunction()
{
  // some code here
}

int someNonNestedFunction()
{
  // code
  return someInt;
}

Can you give me an example of how i am supposed to do it because i don't have much time left!

That sounds suspiciously like "please do the rest of my assignment for me"

but if i turn

if (position == 3)

in to

int if (position == 3)

it stills shows the error of expected unqualified-id before if

cekstuffertz:
but if i turn

if (position == 3)

in to

int if (position == 3)

it stills shows the error of expected unqualified-id before if

That's because you are trying to turn the code into a function. You can't turn an if statement into a function, you can only nest it within the function. Why are you trying to turn that code into a function?

Because i don't understand how you nest a statement into a code!

cekstuffertz:
Because i don't understand how you nest a statement into a code!

What do you mean by "nest a statement into a code"? What do you consider "a statement"? What do you consider "a code"?

You can't turn an if statement into a function, you can only nest it within the function.

the statement is = if (position == 3)

cekstuffertz:

You can't turn an if statement into a function, you can only nest it within the function.

the statement is = if (position == 3)

So that needs to be within a function:

void someFunction()
{
   if (position == 3)
   {
     // code
   }
}

So

//Arduino PLL Speed Control?
int E1 = 4;
int M1 = 5;
int E2 = 7;
int M2 = 6;
int tilt_s1 = 2;
int tilt_s2 = 3;

void setup()
{
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
pinMode(tilt_s1, INPUT);
pinMode(tilt_s2, INPUT);
Serial.begin(9600);
}

void loop(){
int position = getTiltPosition();
Serial.println(position);
delay(200);
}

int getTiltPosition(){
int s1 = digitalRead(tilt_s1);
int s2 = digitalRead(tilt_s2);
return (s1 << 1) | s2;

}
void engine()
{
if (position == 3)
{
int value
for(value = 0 ; value <= 1; value+=5)
{
digitalWrite(M1, HIGH);
digitalWrite(M2, HIGH);
analogWrite(E2, 1);
analogWrite(E1, 1); //PLL Speed Control
delay(30);
}

}

{
else
int value
for(value = 0 ; value <= 1; value+=5)
{
digitalWrite(M1, LOW);
digitalWrite(M2, LOW);
analogWrite(E1, 1);
analogWrite(E2, 1); //PLL Speed Control
delay(30);
}

else
  int value
  for(value = 0 ; value <= 1; value+=5) 
  {
    digitalWrite(M1, LOW);  
    digitalWrite(M2, LOW);    
    analogWrite(E1, 1);
    analogWrite(E2, 1);   //PLL Speed Control
    delay(30); 
  }

Watch your curly braces. The indentation here suggests you want all of this code to be inside the else clause, but no curly braces means the only the first statement is:

if (someCondition)
{
  // code runs when someCondition is true
}
else
// first line of code runs when someCondition is false
// anything after that runs no matter the result of someCondition

Even if you only want a single statement to run, its good practice to ALWAYS surround code with curly braces for if/elseif /else so that you don't run into issues like you are having:

if (someCondition)
{
  // code runs when someCondition is true
}
else
{
  // code runs when someCondition is false
}
// code runs no matter the result of someCondition

You should also use the Auto-format tool (Ctl+T or Tools > Auto Format) to make sure you have proper indentation that won't cause confusion down the road. There is also the added benefit of telling your when you have an uneven number of right and left curly braces. Every opening left curly brace should have a closing right curly brace. Every closing right curly brace should have an opening left curly brace.

Lastly, don't just post "So" and a bunch of code, post something of value like "I tried this: and now I'm getting this error message ". Code goes in CODE tags, not QUOTE tags.

cekstuffertz:
Can you give me an example of how i am supposed to do it because i don't have much time left!

An if-then-else construct cannot appear at global scope. I expect that you really want the if-then-else to appear within your loop() function

void loop(){
  int pos = getTiltPosition();
  Serial.println(pos);
  delay(200);
  if (pos == 3) 
  {
  for(int value = 0 ; value <= 1; value+=5) 
  {
    digitalWrite(M1, HIGH);  
    digitalWrite(M2, HIGH);
    analogWrite(E2, 1);    
    analogWrite(E1, 1);   //PLL Speed Control
    delay(30); 
  }
  
 }else{
  for(int value = 0 ; value <= 1; value+=5) 
  {
    digitalWrite(M1, LOW);  
    digitalWrite(M2, LOW);    
    analogWrite(E1, 1);
    analogWrite(E2, 1);   //PLL Speed Control
    delay(30); 
  }  

 }
 
}

The for loops look a bit suspicious though. The variable called value will start at 0. The loop will run once. Next, the variable value will be 5. But since 5 is not less than or equal to 1, the loop will not run again. I'm not certain if that's what you really intended.

However, if you make the above change, moving your if statement inside the loop() function, your code should compile.

Thank you for the code, but it still doesn't work! It says:
In function 'void loop()':
error: 'getTiltPosition' was not declared in this scope

this is the code:

int tilt_s1 = 2;
int tilt_s2 = 3;
int E1 = 4;   
int M1 = 5;
int E2 = 7;                         
int M2 = 6;  

void setup()
{
   pinMode(M1, OUTPUT);   
   pinMode(M2, OUTPUT); 
   pinMode(tilt_s1, INPUT);
   pinMode(tilt_s2, INPUT);
   Serial.begin(9600);
}
void loop(){
  int pos = getTiltPos();
  Serial.println(pos);
  delay(200);
  if (pos == 3) 
  {
  for(int value = 0 ; value <= 200; value+=5) 
  {
    digitalWrite(M1, HIGH);  
    digitalWrite(M2, HIGH);
    analogWrite(E2, 200);    
    analogWrite(E1, 200);   //PLL Speed Control
    delay(30); 
  }
  
 }
 else{
  for(int value = 0 ; value <= 100; value+=5) 
  {
    digitalWrite(M1, LOW);  
    digitalWrite(M2, LOW);    
    analogWrite(E1, 100);
    analogWrite(E2, 100);   //PLL Speed Control
    delay(30); 
  }  

 }
 
}