Pages: [1]   Go Down
Author Topic: Remember you're a button  (Read 473 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 85
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

Just having some small confusion, about how to turn an LED on with one button press and when you press the button again; turns the led off.

Instead of lighting up when held down. Surely it's something very simple!

Sorry for the extremely basic question!
Logged

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

Do a search for "debouncing button" to get on your way.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 85
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you very very much!! smiley
« Last Edit: August 17, 2011, 02:20:53 pm by mouldysoul » Logged

Denmark
Offline Offline
Edison Member
*
Karma: 37
Posts: 1096
Happy Hobbyist
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This sketch is maybe want you want, every time you push a button a LED wil turn on, and next time you push itthe LED will turn off.


There is no debouncing , and there should be

Quote
int vent=100;
const int buttonPin = 7;
int flag=0;
int buttonState = 0;   

void setup()

 pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
   pinMode(buttonPin, INPUT);
Serial.begin(9600);

 
 
void loop()

 //   digitalWrite(4, HIGH);

buttonState = digitalRead(buttonPin);
delay(vent);


  Serial.print(buttonState);
Serial.println();

   if (buttonState==HIGH){
   
     if (flag==1) {
     flag=0;
     }
     else
       {
        flag=1;
     }
  
   }

 if (flag==1){
  Serial.print("HIGH\n");
   digitalWrite(4, HIGH);

 }
 
 if (flag==0){

 digitalWrite(4, LOW);
 
  Serial.print("LOW\n");
 }




« Last Edit: August 17, 2011, 02:53:22 pm by Erni » Logged

INDIA
Offline Offline
Sr. Member
****
Karma: 0
Posts: 382
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I wrote it in an easy way..
Hope this helps you.. smiley

Code:
int button_pin = 11;
int led_pin = 13;
int button_state = 0;
int led_state = LOW;

void setup(){
  pinMode(button_pin, INPUT);
  pinMode(led_pin, OUTPUT);
}

void loop(){
  if(button_state == HIGH){
    led_state = !led_state;
    digitalWrite(13, led_state);
    delay(100);
  }
}
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 85
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Cheers for the response, really good looking code, but couldn't get the LED to come on!
Logged

Germany
Offline Offline
Newbie
*
Karma: 1
Posts: 34
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I wrote it in an easy way..
Hope this helps you.. smiley

Enjoy your rapidly blinking LED. That will, as long as the button is pressed, toggle the LED every 100ms.
Logged

Pages: [1]   Go Up
Jump to: