I am working through the first couple examples in the Arduino cookbook. They are 2.3 and 2.4. In each example there is a line of code that is just the term “else”. When I try to compile, the sketch pad gives me this error -‘else’ without previous ‘if’-. I found an error list for the book, but it says nothing about either of these errors. I am not particularly computer savvy, so please explain without to many programing terms if possible.
Each else in a program needs an if. Not all IFs need an else or an else if.
As an example:
If( I am hungry) {
eat;
} else {
do not eat;
}
Now this example has two instruction blocks that could be executed. “eat” if you are hungry or “do not eat” if you are not. The else indicates to the compiler what instruction or instructions will be executed for each clause. In the above I used curly braces to indicate the blocks of code to be executed in each case. If the curly braces are absent, only the next single instruction will be executed.
It is generally considered good form to use curly braces even if you are only executing one line of code. Although this is compilable:
If( I am hungry)
eat;
else
do not eat;
Someone needs to give you a book on C/C++ for your birthday!
I tried putting ‘if’ before and after ‘else’ and it still wouldn’t compile.
Then post the function that has the problem and we can offer a suggestion.
Usually the error is quite descriptive of whats wrong.
The program was copied straight from the book “Arduino Cookbook” by Michael Margolis, so there shouldn’t be a problem. I may have a copying error, but I double and triple checked it. I also tried the program in the previous chapter, it also had a line with just ‘else’ that the sketch pad did not like. I have looked at a couple books on C programing online however I am not sure which one would be appropriate for me. I am not vary computer inclined. I like electronics and have some formal training, but I have always struggled with computers. Here is a copy of the second program I tried to run.
/*
array sketch
an array of switches controls an array of LEDs
*/
int inputPins = {2,3,4}; // create an array of pins for switch inputs
int ledPins = {11,12,13}; // create array of output pins for LEDs
void setup()
{
for(int index = 0; index < 3; index++)
{
pinMode(ledPins[index], OUTPUT); //declares LED as output
pinMode(inputPins[index], INPUT); //declares push botton as input
digitalWrite(inputPins[index], HIGH); //enable pull-up resistor
}
}
void loop()
{
for(int index = 0; index < 3; index++)
{
int val = digitalRead(inputPins[index]); // read input value
if (val == LOW); // check if the switch is pressed
{
digitalWrite(ledPins[index], HIGH); // turn LED on if switch is pressed
}
else if
{
digitalWrite(ledPins[index], LOW); //turn LED off
}
}
}
The else if (6 from bottom) should have a condition, or lose the if. The compiler is expecting an else if(condition){ or a else{. Just simply a else if{ is not valid.
Just deleting the letters if so that that 6th line reads else should fix the problem.
did you really expect that the code would work straight from the book? Silly person…
I added the if as one of my attempts to fix. I tried else alone, exactly as in book and it did not like it. My LEDs stay on and will not turn off with switch.
But is you remove the if, the compiler errors go away but you are left with a logic error (thing does not do what you want it to do)?
If that is the case, triple check that your wiring uses the same pins as are referenced in the code and that the leds and switches are connected properly.
If you still have a compiler error, repost the clean code and the error message from the compiler.
Well Kernighan and Plauger (and I think K&R) would run their machine readable text through the compiler to make sure that the programs worked.fll-freak:
Just deleting the letters if so that that 6th line reads else should fix the problem.did you really expect that the code would work straight from the book? Silly person…
If only more authors would be so considerate. 8^)
Just a note, if you use the “code” button you’ll get a nice, easy-to-read post like this …
/*
array sketch
an array of switches controls an array of LEDs
*/
int inputPins[] = {2,3,4}; // create an array of pins for switch inputs
int ledPins[] = {11,12,13}; // create array of output pins for LEDs
void setup()
{
Serial.begin(9600);
for(int index = 0; index < 3; index++)
{
pinMode(ledPins[index], OUTPUT); //declares LED as output
pinMode(inputPins[index], INPUT); //declares push botton as input
digitalWrite(inputPins[index], HIGH); //enable pull-up resistor
}
}
void loop()
{
for(int index = 0; index < 3; index++)
{
int val = digitalRead(inputPins[index]); // read input value
if (val == LOW) // check if the switch is pressed, = LOW
{
digitalWrite(ledPins[index], HIGH); // turn LED on if switch is pressed
Serial.print("Switch is pressed");
}
else
{
digitalWrite(ledPins[index], LOW); //turn LED off
}
}
}
Note that I’ve added a “debug” print statement that will tell you if the switch was pressed. That way you can tell if you’re having a software or hardware problem. From the following …
My LEDs stay on and will not turn off with switch.
The code comments would indicate to me that the LEDs are on until/unless a button is pressed. Generally the button provides a contact to ground when pressed and an open when not. A pullup resistor provides a logic HIGH when the button is an open (and you have pullups enabled).
You had this above …
if (val == LOW);
```but it should be this
if (val == LOW)
Thanks Mee_n_Mac, that was the problem. The book also had it correct. Could you also check this program for me, and let me know how to run it. I think it is just an exercise in programing, not necessarily a program for my Arduino.
/*Floating-point example
*This sketch initialized a float value to 1.1
*It repeatedly reduces the value by 0.1 until the value by 0.1
*It repeatedly reduces the value by 0.1 untilthe value is 0*/
float value = 1.1;
void setup()
{
Serial.begin(9600);
}
void loop()
{
value = value - 0.1; //reduce value by 0.1 each time through loop
if( value == 0)
Serial.println("The value is exactly zero");
else if(almostEqual(value, 0));
{
Serial.print("The value");
Serial.print(value,7); //print to 7 decimal places
Serial.println(" is almost equal to zero");
}
Serial.println(value);
delay(100);
}
//returns true if difference between a and b is small
//set value of DELTA to maximum differnce considered to be equal
boolean almostEqual(float a, float b)
{
const float DELTA = .00001; // max difference to be almost equal
if (a == 0) return fabs(b) <= DELTA;
if (a == 0) return fabs(a) <= DELTA;
return fabs((a-b) / max(fabs(a), fabs(b))) <= DELTA;
}
Well the first part is intended to print a value, over and over over the serial line at 9600 baud and at 8,0,1. The second part, I am drawing a blank. Someone please step in.
The second part there is checking to see if the value is within 1 hundred thousandth of each other.
In the function, almostEqual, the second if statement is unreachable. I think that should be
if(a == 0) return fabs(b) <= DELTA;
if(b == 0) return fabs(a) <= DELTA;
This part of the code is shortcutting doing many math operations if one of the operators is 0. Otherwise it will give you the actual delta between the numbers.
Dan
Oh its a function… Now I feel stupid. Haha. Didn’t see that.
dkulinski:
Otherwise it will give you the actual delta between the numbers.Dan
I think that’s the intent but because the function “almostEqual” is declared to return a boolean (0 or 1), I don’t know what’ll happen. I’d hope the compiler would puke on it and cry error.
Also aren’t there some missing braces in the main loop ?
And a missing else ?
void loop()
{
value = value - 0.1; //reduce value by 0.1 each time through loop
if( value == 0)
{
Serial.println("The value is exactly zero");
}
else if(almostEqual(value, 0));
{
Serial.print("The value");
Serial.print(value,7); //print to 7 decimal places
Serial.println(" is almost equal to zero");
}
else
{
Serial.println(value);
}
delay(100);
}
Lastly I don’t know what this does ?
fabs(a) <= DELTA;
You are right, it returns a boolean. I didn’t check the braces just read over the code real quickly.
As for the snippet you ask about, it takes the absolute value of a, and returns true if it is smaller than DELTA or false otherwise. fabs is a float absolute value function.
Dan
Thanks for the help guys. I fixed the 'else and added the second bracket, but I am still getting an error. "else without previous if. Also, how do I run the program. Will it just run on the sketch pad when it is correct. This book is kind of hard to follow. I am trying to go through it step by step, but I keep hitting walls. It shows what the serial monitor output is supposed to be, but WTF is the serial monitor. I am sorry if these are stupid questions. I got this book because it was supposed to start at the beginning, apparently I needed to start well before the beginning. Maybe you guys can suggest a another book with a little more background.
Check the line # that’s in the error code. Make sure there’s not a typo or such on that line.
I think you should have a good look at the Arduino home site. There’s good references and examples there. Read the “Getting started” and “Environment” pages.
The serial monitor is a terminal emulator program that opens up a window on your desktop. IIRC you can get it to run from the tools menu in the Arduino window. This allows you to see what’s “printed” in your code and for you to input text to the Arduino when it’s running your code. A useful debugging tool you should learn to use. And when not to use as print statements can take up a lot of processing time.