I've searched both the forum and google, and I can't seem to find out why my code is returning the "Else" without a previous "if" error. Every example I've found is someone terminating the condition with a semicolon. I'm definitely not doing that. I'm sure it's a newby error of some sort, since I am quite new at this, but I can't for the life of me find it. Can anyone help me here, please? Here's the section of code:
if (ch7 < 1500) // Only allows Actuators to move if ch7 is switched on
{
digitalWrite(R01, HIGH);
digitalWrite(R02, HIGH);
}
else
{
if (ch5 < 1200 && HallSwitch01 == LOW)
{
digitalWrite(R01, LOW); // Actuator is extending
digitalWrite(R02, HIGH);
}
}
else if (ch5 < 1200 && HallSwitch01 == HIGH) // Indicates actuator has reached the end of its extending travel
{
digitalWrite(R01, HIGH); // Actuator is turned off and braking
digitalWrite(R02, HIGH);
}
else
{
digitalWrite(R01, HIGH); // Actuator is retracting
digitalWrite(R02, LOW);
// if(ch5 < 1200 && HallSwitch01 == LOW)
// digitalWrite(R02, HIGH);
// else
// digitalWrite(R02, LOW);
}
Thanks for taking the time to respond. Can I not put an "If > Else If > Else" set as the "Else" condition within a higher level "If > Else" statement? That's what I'm trying to do here. Again, I'm new. You'll have to forgive me if I'm not indenting properly. I've never had a programming class, I'm stumbling my way through tutorials, and I'm asking questions on forums.
"else if" is valid in most variants of Basic. You would think that it would work in C because you can put a single statement after an "else" without putting braces {} around it, and an "if" is a single statement.
But it doesn't work. You have to use braces, which means you have to indent it "properly".
If you want a long chain of else-if's without getting indented off the right edge of the screen, consider using a switch-case.
Thanks MorganS! So, If I'm reading correctly, it is possible to do what I'm attempting here (put an "If > Else If > Else" set as the "Else" condition within a higher level "If > Else" statement), but I'm indenting incorrectly. I have braces around what I think are the correct spots. I understand that I need to place them around the commands I want to make conditional, and I've looked through them about a bajillion times. Could you point me in the right direction as far as indenting correctly goes here? Do I need to put the open brace "{" on the same line as the If, Else If, and Else? Thanks again. I'm probably learning this out of order, but there it is.
I prefer the first one because it saves space but the second one makes it a lot more obvious which braces match up.
The neat thing about the Arduino IDE is you can write code so that it complies without error, then hit the Tools->Auto Format (alt-T) command.
The first few times you do this it will appear to scramble your code in an unfamiliar way. Once you get used to following that style, then you can work without using that tool at all.
I think a few people are missing what I was trying to do. Here's a non-code, approximation of what I was trying to logically accomplish:
If (condition)
Execute this code
Else
Execute this next conditional:
If (condition)
Execute this code
Else If (condition)
Execute this code
Else
Execute this code
I am not experienced enough with this stuff to say whether or not this is legal, but it certainly works. I have done it before, and now it is working in my code above. I understand the braces are necessary to have multiple executions happen under a conditional. The problem that I was running into, is that I had an extra brace where I didn't need one. A typo. it did not seem to have anything to do with indenting, in the end. Thanks again, for everyone's help and time.
iskanderdumacedon:
I think a few people are missing what I was trying to do. Here's a non-code, approximation of what I was trying to logically accomplish:
If (condition)
Execute this code
Else
Execute this next conditional:
If (condition)
Execute this code
Else If (condition)
Execute this code
Else
Execute this code
I am not experienced enough with this stuff to say whether or not this is legal, but it certainly works. I have done it before, and now it is working in my code above. I understand the braces are necessary to have multiple executions happen under a conditional. The problem that I was running into, is that I had an extra brace where I didn't need one. A typo. it did not seem to have anything to do with indenting, in the end. Thanks again, for everyone's help and time.
Then is must be parenthesized like this, which is NOT what you had in your original post:
If (condition)
{
// Execute this code
} Else
{
//Execute this next conditional:
If (condition)
{
//Execute this code
} Else If (condition)
{
//Execute this code
} Else
{
//Execute this code
}
}
ALWAYS use curly braces when doing if/else, even though they are not always required. Use Ctrl_t to reformat the code correctly. ALWAYS indent properly, and mistakes will be obvious. When you DO get an error, LOOK at the error message - it gives you the exact line number, and the exact character position where the error is, and the error message is NEVER wrong. IF you get an error, there IS a mistake in your code, and it IS exactly what and where the error message says it is.