I need somebody help

Hi everyone, I have a big prob dat my brain does not want to understand :slight_smile:
Here I have 2 buttons, b1 and b2, with b2 I want to switch my other led, lr, on. With b2 I want to turn lr off. I made the electronics, everything is fine, I tested it with only 5v and GND and everything was mervellous.
Here is my code :

const int b1 = 2;
const int b2 = 3;
const int lr = 10;

void setup() {

pinMode(b1, INPUT);
pinMode(b2, INPUT);
pinMode(lv, OUTPUT);
pinMode(lj, OUTPUT);
pinMode(lr, OUTPUT);
digitalWrite(lr, HIGH);
digitalWrite(lj, HIGH);
digitalWrite(lv, HIGH);

}

void loop() {

int stateb1 = digitalRead(b1);
int stateb2 = digitalRead(b2);
int i;

if(stateb2 == LOW){
i = 1;

}
if(statetb1 == LOW){
i = 2;
}
if(i == 1){
digitalWrite(lr, LOW);
}
if(i == 2){
digitalWrite(lr, HIGH);
}
}

Thx :wink:

everything is fine

Clearly, that is NOT true.

Post a schematic, showing the switches and external resistors.

Explain what the code ACTUALLY does, and how that differs from what you expect.

And please indent your code properly so its readable and use code tags. This is all explained in the
sticky threads about how to post.

PaulS:
Explain what the code ACTUALLY does

It does not compile

PaulS:
and how that differs from what you expect.

Expect it to compile, I suppose.

:smiley:

Expect it to compile, I suppose.

Well, yeah, that’s a good first step.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks… Tom… :slight_smile:

OK thank you for your asnwers,
Here is my code, I just want to be able to stop and light a led with two buttons,
Actually, when I launch the led is lightning and only b2 react by turning off the led when i press it

const int b1 = 2;
const int b2 = 3;
const int lv = 8;

void setup() {
  // put your setup code here, to run once:
pinMode(b1, INPUT);
pinMode(b2, INPUT);                                                                                                                                    
pinMode(lv, OUTPUT);
digitalWrite(lv, HIGH);

}

void loop() {

int etatb1 = digitalRead(b1);
int etatb2 = digitalRead(b2);
int i;

if(etatb2 == LOW){
i = 1;

}
if(etatb1 == LOW){
i = 2;
}
if(i == 1){
digitalWrite(lv, LOW);
}
if(i == 2){
digitalWrite(lv, HIGH);
}
}

https://mail.google.com/mail/u/0/?ui=2&ik=0b646b9cd3&view=fimg&th=15c8228b06eeade5&attid=0.1&disp=emb&realattid=ii_15c8226ac7b1ef3d&attbid=ANGjdJ-s7cucKcXi9k27HNs5nJpAdjZ8nxrpoi-vZoKVvJf0Q8ZOX2_6JQkEDbTuMlAVJ5bNZTu9STV4fGxWAWGvikek9doUwfqAiclrHWuJeRSthL18l1bipY6fIa4&sz=w272-h362&ats=1496832775472&rm=15c8228b06eeade5&zw&atsh=1

https://mail.google.com/mail/u/0/?ui=2&ik=0b646b9cd3&view=fimg&th=15c822f63dd06921&attid=0.1&disp=emb&realattid=ii_15c822e223414ff7&attbid=ANGjdJ9oGqkbguMLPHwx77A5Kt2SmdT5_pn5v8X_vsddSSdH6G3KdsUr7rFB5y4yLm6nlKz_pBgDGqDCmeZH0pmCnqB73X6UOzgAMgdM5f0wIm7z8owZksNVAja2tu4&sz=w272-h362&ats=1496833933349&rm=15c822f63dd06921&zw&atsh=1

https://mail.google.com/mail/u/0/?ui=2&ik=0b646b9cd3&view=fimg&th=15c8228b06eeade5&attid=0.2&disp=emb&realattid=ii_15c82271b46fcfc2&attbid=ANGjdJ_mBaKZFRXTUfn7sNDJbquhm_7Hoc8LmjOD2LscvzoQ7kh9TcgeNoyiwIvC107Y7PyULAAr1YS_l6jul1xzijGdrAik-t73cpw07vQCTSgBCeE7GuzYPgiaS0I&sz=w362-h272&ats=1496832775472&rm=15c8228b06eeade5&zw&atsh=1

Get rid of i. Set the state of the LED based on the state of the switches.

You failed to post a schematic. I have no clue what those links are, so I'm not about to click one. I'm real leary of anything with mail and google in the name.

I'm going to assume that your switches are not wired correctly until you prove that they are.

The simplest way to wire a switch is to connect one leg to ground and connect the other leg to a digital pin, and set the pinMode() for that pin to INPUT_PULLUP. Why didn't you wire your switches that way?

I did it and this is the reason why I said that electronic was fine. My only problem is with the code I want to know is why my code isn't right.
Just what I want to do with my code:
When I push the second button (b2) I want my led to be turned on. And when I push the other button(b1) I want my led to be turned off. That's all.
But thx for taking the time to respond to me.

I did it

Did what? Does you Mom know you did it?

My only problem is with the code I want to know is why my code isn't right.

That is NOT true. The code works with the hardware. We STILL can't see a schematic.

So, all we can say is WIRE YOUR SWITCHES PROPERLY!

Ok, I'm a bit tired today, if it is for talking to me that way i prefer trying to solve my problem alone,
Thank you for your time.

const int b = 2;
const int b2 = 3;
const int led = 8;
int etatb;
int etatb2;

void setup() {
  // put your setup code here, to run once:
pinMode(led, OUTPUT);
pinMode(b, INPUT);
digitalWrite(led, HIGH);
etatb = HIGH;
etatb2 = HIGH;

}

void loop() {
  // put your main code here, to run repeatedly:
etatb = digitalRead(b);
etatb2 = digitalRead(b2);


if(etatb2 == LOW){
digitalWrite(led, LOW);}
if(etatb == LOW){
  digitalWrite(led, HIGH);}
}

I found,
my switches were perfectly wired, thank you PaulS!

Hi,
You are declaring etatb and atatb2 as integers, thats range from -32,768 to 32,767.

You need to declare them

bool atatb
bool atatb2

if(etatb2 == false){

Then they will be true or false.

Tip:
when you assign pin numbers try

const int b1Pin=2;
const int b2Pin=3;

Variables associated with those pins.

bool b1State;
bool b2State;

so

b1State = digitalRead(b1Pin);

if(b1State == false){

This way the above statement becomes self explanatory.
Just to make your code easy to read.
The same with ledPin.
Tom... :slight_smile:

Also, before you post your code, with the cursor in the code window of the IDE, press Ctrl-T to reformat your code. It will make it easier to read.

Tip:
when you assign pin numbers try
Code: [Select]
const int b1Pin=2;
const int b2Pin=3;

Better still, at least until Arduinos get more than 256 pins, try

const byte b1Pin=2;
const byte b2Pin=3;

Great, thank all of you, I will use these!