Pages: [1]   Go Down
Author Topic: How to leave an LED on  (Read 686 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello guys, I am probably gonna get laughed at here but I need help programming an LED. I got the SUPER basic program on turning an LED on when a pushbutton on my breadboard is pressed but the LED only turns on when I press it, and it goes off when I release it. I need a program where I press and release it once and the LED turns (and remains) on. And when I press and release it again the LED goes off. I am using an Arduino UNO.

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:   
    digitalWrite(ledPin, LOW); 
  }
  else {
    // turn LED off:
    digitalWrite(ledPin, HIGH);
  }
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26510
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The key phrase here is:
Quote
I need a program where I press and release

You need to detect when the button goes from "pressed" to "not pressed" and simply flip the state of the LED.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1219
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Normally the button would be between a pin ( set with a pullup in setup )  and ground,

so if you press the button and digitalRead the pin it will be low,
Quote
 if (buttonState == HIGH) {    
    // turn LED on:  
    digitalWrite(ledPin, LOW);
 

Normally the LED is wired from the pin through a resistor to ground ( as is the built in LED on pin 13 )  so you would  
digitalWrite(ledPin, HIGH)  to switch it on

Quote
// check if the pushbutton is pressed.

  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {    
    // turn LED on:  
    digitalWrite(ledPin, LOW);
  }
  else {
    // turn LED off:
    digitalWrite(ledPin, HIGH);
  }

even when you get the logic sorted out, what you are doing is saying " if the buttons is pressed, light the LED' else ( otherwise ) turn it off.

So you might as well wire the LED to the button :-)

 when you have the logic levels sorted search for toggle.
Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the help. Hopefully I can figure this out. Kinda confused cos I'm used to programming 8051s on Silicon Labs.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26510
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

8051, PIC, PowerPC or ARM - the principle is identical.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

New York
Offline Offline
Sr. Member
****
Karma: 4
Posts: 380
xronosclock.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's a great video tutorial that will show you just what you need smiley
https://www.youtube.com/watch?v=_LCCGFSMOr4
Towards middle of it it shows how to switch on/off button so it stays lit or unlit.
Logged

Xronos Clock - A talking arduino based alarm clock is now available. Check out xronosclock.com for pictures, source code, schematics, and purchasing info smiley

Pages: [1]   Go Up
Jump to: