Day 1 c++ idiot here. I've tried reading up on this and in my mind everything is correct, although I do realise this will be a stupid question. Please bear with me
The short version is, in this bit of code, why are all of my if statements running, and not just number 2 ?
//www.elegoo.com
//2016.12.8
// define pins
#define BLUE 3
#define GREEN 5
#define RED 6
#define Button 12
#define DELAYTIME 2000
void setup()
{
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(Button, INPUT_PULLUP);
digitalWrite(RED, LOW);
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, LOW);
Serial.begin (115200); // initialize serial comms at 115200 baud
}
// define variables
int redValue =0;
int greenValue=0;
int blueValue=0;
int effect =2;
// define functions ???? IS this the right term
void UpdateLeds()
{
analogWrite(RED, redValue);
analogWrite(GREEN, greenValue);
analogWrite(BLUE, blueValue);
}
// main loop
void loop()
{
if (digitalRead(Button) == LOW) //button pressed - Main chunk of code
{
if (effect == 1); {//effect will eventually be controlled by a keypad or BT
Serial.println ("Effect 1 start");
Serial.println (effect);
redValue =255;
greenValue=0;
blueValue=0;
UpdateLeds();
Serial.println ("Effect 1 end");
}
if (effect == 2);{ //effect will eventually be controlled by a keypad or BT
Serial.println ("Effect 2 start");
redValue =0;
greenValue=255;
blueValue=0;
UpdateLeds();
Serial.println ("Effect 2 end");
}
if (effect == 3); {//effect will eventually be controlled by a keypad or BT
Serial.println ("Effect 3 start");
redValue =0;
greenValue=0;
blueValue=255;
UpdateLeds();
Serial.println ("Effect 3 end");
}
}
if (digitalRead(Button) == HIGH) //button up
{
analogWrite(RED, 0);
analogWrite(GREEN, 0);
analogWrite(BLUE, 0);
}
}
Serial output is
Effect 1 start
2
Effect 1 end
Effect 2 start
Effect 2 end
Effect 3 start
Effect 3 end
repeating
I'm sure you experts will have guessed I've got an RGB LED hooked up to this, but I was head scratching that its always white. I've just learned about serial.writeln which has allowed me to troubleshoot to the point that its a problem with my if statement, but the if statement looks correct to me, so I've had to resort to a forum post, please help.
I'm sure I'm doing something stupid here