Pages: [1]   Go Down
Author Topic: [Solved] Simple Button Program Problem  (Read 423 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Brain Hacked
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everyone,
I've just got the Arduino UNO yesterday, and I had a problem with a simple program.
the program is when the push button is pressed LED1 is ON and LED2 is OFF and when released LED1 is OFF and LED2 is ON.
what happens is LED2 is always OFF  smiley-neutral, and LED1 is switching normally as it should.
if anyone could help, I'll appreciated.

here's the code:
Code:
int PushButton = 2;
int LED1 = 3;
int LED2 = 4;

void setup() {
 pinMode(PushButton,INPUT);
 pinMode(LED1,OUTPUT);
 pinMode(LED2,OUTPUT);
}
 
void loop() {
  if (digitalRead(PushButton)==HIGH) {
    digitalWrite(LED1,HIGH);
    digitalWrite(LED2,LOW);
  }
  if (digitalRead(PushButton)==LOW) {
   digitalWrite(LED1,LOW);
   digitalWrite(LED2,HIGH);
  }

}

And about my connections, I've connected the switch with a pull down resistor 220ohm, and the two LEDs are connected to the digital pins 3 and 4 through 220ohm resistors.
I've checked for the LEDs and the wires and everything is just fine.
« Last Edit: April 05, 2012, 08:29:41 pm by Mashadawy » Logged

alabama
Offline Offline
Full Member
***
Karma: 1
Posts: 183
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First thing i would check is the polarity of the second LED, then whether it works at all by switching with the other led.
TomJ
Logged

Einstein once said you don't really understand anything until you can explain it to your Grandmother

Offline Offline
Edison Member
*
Karma: 20
Posts: 2113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

swap the LED's around.

2. add a debounce (or a capacitor to keep the voltage raised or in that brief moment you've pressed the button it can measure the metal contacts touching each other hundreds of times, the cap stops that) routine eg... AnalougeRead.... {Delay(250); (enough to get your finger off in time) your code}
Logged

Central MN, USA
Online Online
Tesla Member
***
Karma: 65
Posts: 6904
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

And about my connections, I've connected the switch with a pull down resistor 220ohm

Show how you connected this. The resistor is too small as pull down, try 10K or 20K not 220. Why not trying to use the internal pull_up resistor. Just add digitalWrite(button,HIGH); after pinMode() to enable the internal pull_up resistor. Then connect the pin to button, the button other side to gnd.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 11
Brain Hacked
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

it worked, I made a software debouncing for the switch, and enabled the internal pull-up resistors, and it worked just perfect.
thx everybody for the help, really appreciated.  smiley
Logged

Pages: [1]   Go Up
Jump to: