Troubleshooting simple led program.

Hi,

I’m Kinda new to this. I’ve set up a small led circuit involving 2 leds and s momentary switches. The circuit works and I’ve done some smaller projects using it so its not an electrical thing.

I’ve compiled some code which the IDE says is ok but it does nothing. The aim is, both leds are off until one switch is pressed where one led tuns on until the other switch is pressed where one goes out while the other comes on.

I’ve had a look about and found similar projects but couldn’t find and exact code to just straight copy paste yet.

Thanks for any help anyone can spare.

const int BUTTON1 = 6;
const int BUTTON2 = 7;
const int BLUE = 10;
const int ORANGE = 11;
int state;

void setup()
{
pinMode(BUTTON1, INPUT);
pinMode(BUTTON2, INPUT);
pinMode(BLUE, OUTPUT);
pinMode(ORANGE, OUTPUT);
Serial.begin(9600);
}

void loop()
{
state = 0;
if (BUTTON1 == HIGH);
{
state = 1;
}
if (BUTTON2 == HIGH);
{
state = 2;
}
switch (state)
{
case 0:
digitalWrite (BLUE, LOW);
digitalWrite (ORANGE, LOW);
break;

case 1:
digitalWrite (BLUE, HIGH);
digitalWrite (ORANGE, LOW);
break;

case 2:
digitalWrite (BLUE, LOW);
digitalWrite (ORANGE, HIGH);
}
}

  if (BUTTON1 == HIGH);

Oops2
You’re missing a digitalRead, and you’ve got too many semicolons.

Please remember to use code tags

This one: if (BUTTON1 == HIGH);

OR

This one: if (digitalRead(BUTTON1) == HIGH)

hhhmmm... some of the examples i have don't have digitalRead but that's not to say i don't believe you. i'm going to research it now.

also axed the semi colons thanks.

Gigatrix:
hhhmmm… some of the examples i have don’t have digitalRead but that’s not to say i don’t believe you.

We need to see your examples.

BUTTON1 has the value 6, and HIGH has the value 1.
I’d say it’s going to be while before they’re equal.

Ok cool I have It toggling the lights again.

The blue led on until either button pushed and if button 2 pushed orange is on for button push or button one off complexity for press duration. but im sure i can trouble shoot the next part.

I thought the 6 was the pin designation. i'm running out of pin 6.

also example coppied: hastebin

Gigatrix:
I thought the 6 was the pin designation.

It is.

Ok i think i see what your saying. idk how to word it but ive got rid of the ++ 6 and playing with the if statements and digital read

Gigatrix:
also axed the semi colons thanks.

Why have you axed the semicolon (;) when a C statement is always terminated by a semicolon?

GolamMostafa:
Why have you axed the semicolon (;) when a C statement is always terminated by a semicolon?

{facepalm}

TheMemberFormerlyKnownAsAWOL:
{facepalm}

The chance is that the OP might get +Karma.

Oops2
You're missing a digitalRead, and you've got too many semicolons.

is why. I'm sorry if i'm being stupid but sometimes you guys speak kinda cryptically (that's this community) but you do help. Maybe i just don't understand.

Gigatrix:
I'm sorry if i'm being stupid but sometimes you guys speak kinda cryptically (that's this community) but you do help. Maybe i just don't understand.

Give correct answer to the question of Post#9 and earn +Karma.

As a general point , for “trouble shooting” , you can add print statements to check your program is doing what you think it should be doing .Just printing “ here” or printing a variable value will help you a lot

Hi, i'm working on a project. New to C++. I want to have two leds where both off to start and one comes on on a button press and the other comes on and the other goes out when the second button is pressed vise versa.

I have one led switching on but that's it. the circuit works for other projects though.

const int BUTTON1 = 6; //button 1 pin 6
const int BUTTON2 = 7; //button 2 pin 7
const int BLUE = 10; // blue led pin 10
const int ORANGE = 11; // orange led pin 11
int state = 0; // a resetable value state to know what "mode" the device is in set between 0,1,2 for white, blue and orange

void setup()
{
pinMode(BUTTON1, INPUT_PULLUP); //defining button 1 as an input
pinMode(BUTTON2, INPUT_PULLUP); //defining button 1 as an input
pinMode(BLUE, OUTPUT); //defining blue led as an output
pinMode(ORANGE, OUTPUT); //defining orange led as an output
Serial.begin(9600); //serial value
}

void loop()
{
if (digitalRead(BUTTON1 = HIGH)){ //reading button 1
int state = 1; //setting mode to 1
}
else if (digitalRead(BUTTON2 = HIGH)){ //reading button 2
int state = 2; //setting mode to 2
}

if (state == 1) { //reading the mode of the device
digitalWrite(BLUE,HIGH); //turn blue led on
digitalWrite(ORANGE,LOW); //turn orange led off
delay(100); //delay to let code catch up
}
else if(state == 2){ //reading the mode of the device
digitalWrite(BLUE, LOW); //turn blue led on
digitalWrite(ORANGE, HIGH); //turn orange led off
delay(100); //delay to let code catch up
}
}

43 posts and you still don't post code properly

Read this before posting a programming question

if (digitalRead(BUTTON1 = HIGH))

???

if (digitalRead(BUTTON1) == HIGH)

Have you studied the ‘change in state’ example in the IDE ?

duplicate thread.