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.
*/
// 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)