Hi.
I was trying to write some short code in my free time , but I faced a weird problem .
/* Made by Arman on 23 / APRIL / 2015
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // LCD is declared
void setup()
{
lcd.init(); // LCD's initialization
lcd.backlight(); // backlight
lcd.clear(); // clear shit
if (random(2,5) == 3); {
lcd.print("It will ");
}
else {
lcd.print("It won't ");
}
delay(1000); //makes things more stressful
setCursor(0,1);
// writes the first line
if (random(10,27) == 13); {
lcd.print("stop happening.");
}
else {
lcd.print("happen.");
}
// writes the second line
}
void loop () {
}
Then a weird error appears (a bunch of them) :
sketch_apr23a.ino: In function 'void setup()':
sketch_apr23a:18: error: 'else' without a previous 'if'
sketch_apr23a:22: error: 'setCursor' was not declared in this scope
sketch_apr23a:24: error: expected `;' before 'if'
sketch_apr23a:27: error: 'else' without a previous 'if'
Else without previous if ?
I know I should put
If - else If - else
but I don't need else If .
setCursor wasn't declared ?
I tried setCursor(0,1);
and setCursor(0,1)
(without semicolon) but no difference .
Your if's have an empty statement as the then-part. The empty statement is ";".
If takes either a statement or a block, you gave an empty statement so the brace
starts a new block unrelated to the if.
Here is your code with slightly different formatting and comments added:
if (random(2,5) == 3); // if (random(2,5) == 3) ==> do nothing, if-statement finished
{ // begin a code block
lcd.print("It will ");
} // end a code block
else // else without prevous if!
{
lcd.print("It won't ");
}