state detection with two push buttons

Hi every one

I want to light two leds with two push buttons,I mean when I press the first one the first led lights and when press that again the led get off.
and this story is for the second push button too…
this is my sketch in the attachment.
thank you for helping
/*
State change detection (edge detection)

Often, you don’t need to know the state of a digital input all the time,
but you just need to know when the input changes from one state to another.
For example, you want to know when a button goes from OFF to ON. This is called
state change detection, or edge detection.

This example shows how to detect when a button or button changes from off to on
and on to off.

The circuit:

  • pushbutton attached to pin 2 from +5V
  • 10K resistor attached to pin 2 from ground
  • LED attached from pin 13 to ground (or use the built-in LED on
    most Arduino boards)

created 27 Sep 2005
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/ButtonStateChange

*/

// this constant won’t change:
const int buttonPina = 40;// the pin that the pushbutton is attached to
const int buttonPinb =42;
const int ledPina = 50;// the pin that the LED is attached to
const int ledPinb = 52;

// Variables will change:
int buttonPushCountera = 0; // counter for the number of buttonpresses
int buttonPushCounterb = 0;
int buttonStatea = 0;// current state of the button
int buttonStateb = 0;
int lastButtonStatea = 0; // previous state of the button
int lastButtonStateb = 0;

void setup() {
// initialize the button pin as a input:
pinMode(buttonPina, INPUT);
pinMode(buttonPinb,INPUT);
// initialize the LED as an output:
pinMode(ledPina,OUTPUT);
pinMode(ledPinb,OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}

void loop() {
// read the pushbutton input pin:
buttonStatea = digitalRead(buttonPina);
buttonStateb = digitalRead(buttonPinb);

// compare the buttonState to its previous state
if (buttonStatea != lastButtonStatea) {
// if the state has changed, increment the counter
if (buttonStatea == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCountera++;
Serial.println(“on”);
Serial.print("number of button pushes: ");
Serial.println(buttonPushCountera);
} else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println(“off”);
}
// Delay a little bit to avoid bouncing
if (buttonStateb != lastButtonStateb) {
// if the state has changed, increment the counter
if (buttonStateb == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounterb++;
Serial.println(“on”);
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounterb);
} else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println(“off”);
}
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonStatea = buttonStatea;
lastButtonStateb = buttonStateb;

// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (buttonPushCountera % 2 == 0) {
digitalWrite(ledPina, HIGH);

} else {
digitalWrite(ledPina, LOW);

}
delay(50);
if (buttonPushCounterb % 2 == 0) {
digitalWrite(ledPinb, HIGH);

} else {
digitalWrite(ledPinb, LOW);

}
}
}

lll.ino (3.33 KB)

But what is the problem? That's pretty important when you want help...

And while you're at it, place the code in the post with code tags. For all that and more see How to use the forum please.

the first led works correct but the second one doesn't

 if (buttonStatea != lastButtonStatea)

Why does the majority of your code depend on this ?

I didn't made a part of my text bold by accident...

Why have you nested the code for B in the code for A?

They are separate.

I'd recommend putting the common code in a function and calling that function twice from loop with appropriate parameters in each case. More structured, easier to read and change.

mohsen55544:
the first led works correct but the second one doesn’t

Perhaps you have it wired wrong?

You do have serial prints in your code. Does it react to pushing your second button? If it does your led or wiring is broken. If nothing is shown in serial monitor your button might be broken or wired wrong.

This is information you should have given in first post, together with code in code tags. Code tag is the leftmost text formatting symbol </>

no the serial doesn't work...

also I don't need serial I dont have problem without serial

That's called debugging my friend ;)

You have got plenty of hints what is wrong with the code. Here is another. Three closing brackets at the end is too much. One for the else and one for the loop is what it should be. If you hit Ctrl-T in the Arduino IDE you will get a good indentation and it will make it easier to spot the mistake.

I don't know what to do

please edit my code and send it if it is possible.

mohsen55544:
I don’t know what to do

please edit my code and send it if it is possible.

As I previously pointed out, most of your code depends on buttonStatea changing and that includes checking whether buttonStateb has changed. You need to check them independently.

Her I have shrinked part of your code, and used Ctrl-T for indentation. No empty lines and plenty of comments removed for better visibility.

Also if you click a bracket in Arduino IDE the corresponding will be marked. I have highlighted them in yellow. You see, your first if statement doesn't end until the very last of your code. It should end with the red part.

Red and blue parts should be on same level, start in the same column. As it is now blue parts will only be executed if your first if statement is true, you need to press both buttons simultaneously.

Now you should be able to move one of the closing brackets from the end up in your code.