error: expected primary-expression before 'if'

I keep getting these errors and i follow through and i gives me this and the edits it tells me to make aren't supposed to be in my code could some one3 tell me what i could have did wrong? Thanks.

NIGHTLIGHT:84:2: error: expected primary-expression before 'if'
(if else
^~
NIGHTLIGHT:84:2: error: expected ')' before 'if'
exit status 1

---------------------------------ERROR MESSAGES----------------------------------------------------------

Arduino: 1.8.12 (Mac OS X), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"

NIGHTLIGHT:41:2: error: expected primary-expression before 'if'
(if else (ledMode == 2);
^~
NIGHTLIGHT:41:2: error: expected ')' before 'if'
NIGHTLIGHT:48:2: error: expected primary-expression before 'if'
(if else (ledMode == 3);
^~
NIGHTLIGHT:48:2: error: expected ')' before 'if'
NIGHTLIGHT:55:4: error: expected '(' before 'else'
if else (ledMode == 4);
^~~~
NIGHTLIGHT:62:2: error: expected primary-expression before 'if'
(if else (ledMode == 5);
^~
NIGHTLIGHT:62:2: error: expected ')' before 'if'
NIGHTLIGHT:69:2: error: expected primary-expression before 'if'
(if else (ledMode == 6);
^~
NIGHTLIGHT:69:2: error: expected ')' before 'if'
NIGHTLIGHT:77:2: error: expected primary-expression before 'if'
(if else (ledMode == 7);
^~
NIGHTLIGHT:77:2: error: expected ')' before 'if'
NIGHTLIGHT:84:2: error: expected primary-expression before 'if'
(if else
^~
NIGHTLIGHT:84:2: error: expected ')' before 'if'
exit status 1
expected primary-expression before 'if'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

CODE:

const int BLED = 9;
const int GLED = 10;
const int RLED = 11;
const int BUTTON = 2;

boolean currentButton = LOW;
int ledMode = 0;

void setup()
{
pinMode (BLED, OUTPUT);
pinMode (GLED, OUTPUT);
pinMode (RLED, OUTPUT);
pinMode (BUTTON, INPUT);

}

void loop()
{
currentButton = digitalRead (BUTTON);
if (currentButton == HIGH)
{
ledMode++;
}
if (ledMode >= 8);

{
ledMode = 0;
}
setMode();
}
void setMode()
{
if (ledMode == 1);
{
analogWrite (BLED, 255);
analogWrite (GLED, 0);
analogWrite (RLED, 0);
}
// Blue Light
(if else (ledMode == 2);
{
analogWrite (BLED, 0);
analogWrite (GLED, 255);
analogWrite (RLED, 0);
}
// Green Light
(if else (ledMode == 3);
{
analogWrite (BLED, 0);
analogWrite (GLED, 0);
analogWrite (RLED, 255);
}
// Purple Light
if else (ledMode == 4);
{
analogWrite (BLED, 127);
analogWrite (GLED, 0);
analogWrite (RLED, 127);
}
//Teal Light
(if else (ledMode == 5);
{
analogWrite (BLED, 0);
analogWrite (GLED, 127);
analogWrite (RLED, 127);
}
// Orange Light
(if else (ledMode == 6);
{
analogWrite (BLED, 127);
analogWrite (GLED, 127);
analogWrite (RLED, 0);
}
// white Light

(if else (ledMode == 7);
{
analogWrite (BLED, 85);
analogWrite (GLED, 85);
analogWrite (RLED, 85);
}
// Off (ledMode == 7);
(if else
{
digitalWrite (BLED, LOW);
digitalWrite (GLED, LOW);
digitalWrite (RLED, LOW);
}
}

OK, first things first.

You need to go and read the forum instructions so that you can go back and modify your original post here (not re-post it) - using the "More -> Modify" option below the right hand corner of your post - to mark up your code as such using the "</>" icon in the posting window. Just highlight each section of code (or output if you need to post that) from the IDE and click the icon.

In fact, the IDE itself has a "copy for forum" link to put these markings on a highlighted block for you so you then just paste it here in a posting window. But even before doing that, don't forget to use the "Auto-Format" (Ctrl-T) option first to make it easy to read. If you do not post it as "code" it can as you now see, be garbled to varying degree and is always more difficult to read due to the font.

It is inappropriate to attach it as a ".ino" file unless it is clearly too long to include in the post proper. People can usually see the mistakes directly and do not want to have to actually load it in their own IDE. And even that would also assume they are using a PC and have the IDE running on that PC.

Also tidy up your blank space. Do use blank lines, but only single blanks between complete functional blocks.


Now, "if else" makes no sense. Presumably you meant "else if".

if (someCondition);

Never put a semicolon after an "if" like you have done multiple times.

OK, there is that as well!

Post has been reported to be split.

Split from an old topic

Do NOT hijack old topics

Hi,
With all those if..else.. it would be easier for you to use switch.. case..

Tom... :slight_smile: