//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
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?
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.