const int xpin = A0;
const int ypin = A1;
const int zpin = A2;
const int led1 = 5;
const int led2 = 6;
const int led3 = 7;
void setup()
{
Serial.begin(9600);
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
}
unsigned int zaxis,yaxis,xaxis;
void loop()
{
xaxis = analogRead(xpin);
yaxis = analogRead(ypin);
zaxis = analogRead(zpin);
if (xaxis > 390);
{
digitalWrite (led1,HIGH);
}
else
{
digitalWrite (led1,LOW);
}
if (yaxis > 390);
{
digitalWrite (led2,HIGH);
}
else
{
digitalWrite (led2,LOW);
}
if (zaxis > 390);
{
digitalWrite (led3,HIGH);
}
else
{
digitalWrite (led3,LOW);
}
}
Oops.
Every single "if"
Remove the semicolons from your if statements.
C
Welcome to the forum
Delete the semicolons on the end of the if statements. They should not be there
This worked
Do you understand why ?
Many Thanks
unsigned int zaxis,yaxis,xaxis;
With the code as it looks, there is no reason for these variables to have global scope.
Yea, I removed semicolons from all "if" statements
Ah, okay
But do you understand what the problem was and why removing the semicolons fixed it ?
Can u pls explain ?
The structure of an if/else is
if (this is true)
{
//execute this code
}
else
{
//execute this code
}
When you put a semicolon on the end of an if test it is the code that is executed when the test returns true so the code effectively becomes
if (this is true)
{
//execute the semicolon
}
{
//execute this code
}
else
{
//execute this code
}
Note how there are now 2 code blocks after the if before the else hence the "else without previous if error
Ahh.. Got it Now!!
Many thanks for this explanation
It is quite a common error, hence the quick responses here
At least when you do it with if/else you get an explicit error when compiling. When you do it with a simple if (or while) the problem can be more difficult to track down because instead of an error the code does not do what you want or expect
Yea, Alright
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.