I have written a program but only one switch works.
the program i have written is
const int buttonPin = 0;//pushbutton pin
const int buttonPin1 = 1;// pushbutton pin
const int ledPin = 2; // LED pin
const int ledPin1 = 3;
int ledPinState = HIGH; // the current state of the output pin
int ledPin1State = HIGH;
int buttonPinState; // the current reading from the input pin
int buttonPin1State;
int lastButtonPinState = LOW; // the previous reading from the input pin
int lastButtonPin1State = LOW;
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
const int buttonPin = 0;//pushbutton pin
const int buttonPin1 = 1;// pushbutton pin
const int ledPin = 2; // LED pin
const int ledPin1 = 3;
int ledPinState = HIGH; // the current state of the output pin
int ledPin1State = HIGH;
int buttonPinState; // the current reading from the input pin
int buttonPin1State;
int lastButtonPinState = LOW; // the previous reading from the input pin
int lastButtonPin1State = LOW;
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(buttonPin1, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
// set initial LED state
digitalWrite(ledPin, ledPinState);
digitalWrite(ledPin1, ledPin1State);
}
void loop() {
{
int reading = digitalRead(buttonPin);
if (reading != lastButtonPinState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay && reading != buttonPinState ) {
{
buttonPinState = reading;
if (buttonPinState == HIGH) {
ledPinState = !ledPinState;
}
}
}
lastButtonPinState = reading;
int reading =digitalRead(buttonPin1);
if(reading!=lastButtonPin1State){
lastDebounceTime=millis();
}
if ((millis() - lastDebounceTime) > debounceDelay && reading != buttonPin1State ) {
{
buttonPin1State=reading;
if(buttonPin1State==HIGH){
ledPinState = !ledPin1State;
}
}
}
lastButtonPin1State=reading;
}
// set the LED:
digitalWrite(ledPin, ledPinState);
digitalWrite(ledPin1, ledPin1State);
}
use brackets properly . try above code, Let me know result.
There code tag option avilable here to post your code, Use autoformat from IDe for proper arrangement
Sry AMPS-N the code which you suggested me in #2 is not working. Both the leds are glowing at the initial stage but afer pressing the pushbutton the leds are not going off.
Hi, AMPS when i start the arduino the LEDS are switched on. I want led1 to go off when i press the pushbutton1 and if i press it(pushbutton1) again it(led1) must get switched off. And same is applied with pushbutton2 and led2 they must operate irrespective of each other.
I want led1 to go off when i press the pushbutton1 and if i press it(pushbutton1) again it(led1) must get switched off. And same is applied with pushbutton2 and led2 they must operate irrespective of each other.
what does it mean
when you press first time led already goes off, so why you want to switch off again??
You mean to say if button pressed once led goes off , next time when pressed led goes ON.similarly alternating cycle.
I have written a program but only one switch works.
the program i have written is
const int buttonPin = 0;//pushbutton pin
const int buttonPin1 = 1;// pushbutton pin
These maybe the serial tx/rx pins, so you may need to use other pins for the button pins.
Hi,
As Zoomcat says, not a good idea to use the 0(RX) and 1(TX) pins, as they are used for all serial comms, you must have pleanty of others you could use.