Go Down

Topic: Else error: Previous If (Read 425 times) previous topic - next topic

Programming for the Arduino Uno.
I'm new to programming, and am essentially amalgamating circuits 3 5 and 6.
Full program:
Quote

//Define RGB pin #s

const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;
int colorIt = 0; //var for which color is being recorded


//Define ID of sensor pin.

const int sensorPin = 0;

// Set up global vars for light level.

int lightLevel, high = 0, low = 1023;

//set consts for button pins

const int buttonPin = 2; 

void setup()
{
  //set pushpin to input
  pinMode(buttonPin, INPUT);

  //set RGB pins to output
  //set LED pins to output

  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
}


void loop()
{


  int buttonState; //var to hold button state


    buttonState = digitalRead(buttonPin); //Read when button is pushed.

  if (buttonState == LOW);//When the button is pushed
  {
    lightLevel = analogRead(sensorPin); //Read from the light sensor

    if (colorIt = 0); //First iteration
    {      
      analogWrite(RED_PIN, lightLevel); //Light up the red
      colorIt = colorIt + 1; //tell it to ready the next iteration
    }
    else
    {
      if (colorIt = 1); //Second iteration
      {
        analogWrite (BLUE_PIN, lightLevel); //Light up blue

        colorIt = colorIt + 1;
      }
    }
    else
    { 
      if (colorIt = 2) //Third (final) iteration
      }
    {
      analogWrite (GREEN_PIN, lightLevel); 
    }



  }

}




The part with the error is:
Quote

    if (colorIt = 0); //First iteration
    {      
      analogWrite(RED_PIN, lightLevel); //Light up the red
      colorIt = colorIt + 1; //tell it to ready the next iteration
    }
    else
    {
      if (colorIt = 1); //Second iteration
      {
        analogWrite (BLUE_PIN, lightLevel); //Light up blue

        colorIt = colorIt + 1;
      }



It's telling me that that else doesn't have a preceding if, when it clearly (to my eyes) does.

Full error log:
sketch_feb02a.ino: In function 'void loop()':
sketch_feb02a:53: error: 'else' without a previous 'if'
sketch_feb02a:62: error: 'else' without a previous 'if'
sketch_feb02a:65: error: expected primary-expression before '}' token
sketch_feb02a:65: error: expected `;' before '}' token

A couple of those are housekeeping errors that I haven't taken the time to fix yet.
You can probably ignore those, unless you have an urge to tell me why they're there.

I'm mystified about this if..else thing. I've looked at the reference and the bit there
is identical to my code. Hopefully you see something I don't.
Thanks!

Nick Gammon

Read this before posting a programming question

Code tags please, not quotes. I know the IDE has a nice "copy to forum" feature. Please don't use it.


Code: [Select]

    if (colorIt = 0); //First iteration


Two problems here.

Comparison is "==".

And the semicolon terminates the "if" so any subsequent "else" will be mismatched.

Should be:

Code: [Select]

    if (colorIt == 0) //First iteration

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Quote
Read this before posting a programming question


I did, though it seems not thoroughly enough.

Quote
Code tags please, not quotes. I know the IDE has a nice "copy to forum" feature. Please don't use it.


My apologies, I'll try to remember in future. Are there any plans to fix/change this "feature"?

Fixes: Thanks. Compiles just fine now.
Sorry I'm such a poor programmer; I'm literally learning as I go.

Thanks again!


Nick Gammon


Are there any plans to fix/change this "feature"?


We've tried. But ... we moderators ... are just a pawn ... in game of Life.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Quote
We've tried. But ... we moderators ... are just a pawn ... in game of Life.


Which I have also tried to write. Sadly, I don't have nearly enough of the right components to play Conway's Game of Life on my Arduino. :(

Thank you for your fast and polite responses. You're an excellent moderator which, knowing half the stuff mods have to put up with, is saying a lot.
I'll not take any more of your time.
Keep up the good work.

Go Up