help with button

Hi!

I'm working on a project, and I really need the function mentioned below:
When i push a button that I've connected to my arduino, I want an LED to light up (set output HIGH), and when i hit the same button again, I want the LED to turn off, just like a flashlight.
Does anyone have this sort of code?

Thanks!

You already have the code in the examples in the IDE.

Does anyone have this sort of code?

Yes you do. Look in the arduino software on your PC under examples -> 02 Digital ->StateChangeDetect

try this:

LEDpin = 13; //on-board LED
ButtonPin = 2; //digital pin 2

boolean Button;
boolean latch;

void setup() {
   pinMode(LEDpin, OUTPUT);
   Serial.begin(9600);
}

void loop() {
Button = digitalRead(ButtonPin);
If (Button == high && latch == false) {
   digitalWrite(LEDpin, HIGH);
   latch ==true;
}

If (Button == high && latch == true{
   digitalWrite(LEDpin, LOW);
   Latch == false;
}

else { } //no other options

}

Thanks!
The only problem is that the example sketch in the IDE is kinda "buggy". I have to press the button 3-4 times before the LED flash up.
Ive also tried to remove the Serial monitor part, but it haven't helped.

EDIT:
Thanks, I'll test it out, and reply soon :slight_smile:

I didn't include the debounce part. That you need to look up for yourself, in the digital examples, then incorporate it into this code.

OR simply use the debounce code itself.

The only problem is that the example sketch in the IDE is kinda "buggy". I have to press the button 3-4 times before the LED flash up.

From what I remember of the code, that's what it is supposed to do - four times is the actual number, IIRC.

HazardMinds' code will need some rework before it will compile.

I'm sort of green when it comes to arduino programming. I've tried to compile your code, and fixed some errors (HIGH instead of high, missing parentheses etc.) but when I run the code, nothing happends! (im using the typical button wireing, GND, 5v and a 10k)
But I don't understand the debounce part you was talking about. is that why the scetch isn't working properly?

@AWOL
Your thinking of state change detection, this is the simpler version, debounce. Only I want him to understand why it is not working properly, and see if he can fix it.

The code I wrote was mostly Pseudocode, i'm not going to give you the full working code, otherwise you wont learn anything about it.

If your really stuck, then tell me and I will give you the working code.

Ok.
I figured out why the StageChangeDetection acted buggy. The push counter had should have 2 insted of 4 behind the percentage sign.
I really thing arduino programming is intresting, so I just want to ask; witch function has the percentage sign? I tried to google it, but I didn't get any smarter :confused:

I got the help I needed, but I also want to try to get HazardsMind's code up and running :slight_smile: I want to learn more!

witch function has the percentage sign?

modulo
Information is already is on your computer
file:///Applications/Arduino.app/Contents/Resources/Java/reference/Modulo.html

witch function has the percentage sign?

That's the modulo operator

I tried to google it,

When in doubt, start with the reference section.

Ok.
I figured out why the StageChangeDetection acted buggy. The push counter had should have 2 insted of 4 behind the percentage sign.
I really thing arduino programming is intresting, so I just want to ask; witch function has the percentage sign? I tried to google it, but I didn't get any smarter :confused:

Did you read the gray comments, it explains what that %4 means. (% in programing terms is called modulo, kinda like the remainder of a division problem)

%4 = every 4 button presses. if you chage the 4 to a 2 then it becomes every 2 button presses.

Here, this is the working code.

byte LEDpin = 13; //on-board LED
byte ButtonPin = 2; //digital pin 2

int button = LOW;
int lastButtonState = LOW;

long lastDebounceTime = 0;  
long debounceDelay = 50;

boolean latch;

void setup() {
  pinMode(LEDpin, OUTPUT);
  pinMode(ButtonPin, INPUT);
}

void loop() {
  button = digitalRead(ButtonPin);
  if ( button != lastButtonState) {
    lastDebounceTime = millis();
  } 

  if ((millis() - lastDebounceTime) > debounceDelay) {

    if (button == HIGH && latch == false) {
      digitalWrite(LEDpin, HIGH);
      latch ==true;
    }

    if (button == HIGH && latch == true){
      digitalWrite(LEDpin, LOW);
      latch == false;
    }
  }

  else { 
  } //no other options

}

The project I've been working os is about lerarning to interface with IR remotes.
I got it set up and working with an IR library, and I'm able to run diffrent function when I press different buttons on the remote
(Code for remote control below):

#include <IRremote.h>

int rec_pin = 11;
int gnd_pin = 10;
int vcc_pin = 9;

IRrecv irrecv(rec_pin);

decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); 
  pinMode(vcc_pin, OUTPUT);
  pinMode(gnd_pin, OUTPUT);
  digitalWrite(vcc_pin, HIGH);
  digitalWrite(gnd_pin, LOW);

}

void loop() {
   if (irrecv.decode(&results))
   {
    if (results.value == 0xFFA25D) // I/O button
          { Serial.println("Button: I/O   HEX: 0xFFA25D"); }
          
    else if (results.value == 0xFF629D) // Mode button
          { Serial.println("Button: Mode   HEX: 0xFF629D"); }
          
    else if (results.value == 0xFFE21D) // Mute button
          { Serial.println("Button: Mute   HEX: 0xFFE21D"); } 
          
    else if (results.value == 0xFF22DD) // Play_pause button
          { Serial.println("Button: Play   HEX: 0xFF22DD"); }   
          
    else if (results.value == 0xFF02FD) // Previous button
          { Serial.println("Button: Previous   HEX: 0xFF22DD"); }  
          
    else if (results.value == 0xFFC23D) // Next button
          { Serial.println("Button: Next   HEX: 0xFFC23D"); } 
          
    else if (results.value == 0xFFE01F) // EQ button
          { Serial.println("Button: EQ   HEX: 0xFFE01F"); }
          
    else if (results.value == 0xFFA857) // Volume down button
          { Serial.println("Button: Vol_DOWN   HEX: 0xFFA857"); }
          
    else if (results.value == 0xFF906F) // Volume up button
          { Serial.println("Button: Vol_UP   HEX: 0xFF906F"); } 
          
    else if (results.value == 0xFF9867) // Shuffle button
          { Serial.println("Button: Shuffle   HEX: 0xFF9867"); }
          
    else if (results.value == 0xFFB04F) // U/SD button
          { Serial.println("Button: U/SD   HEX: 0xFFB04F"); }   
 
    else if (results.value == 0xFF6897) // 0 button
          { Serial.println("Button: 0   HEX: 0xFF6897"); } 
    
    else if (results.value == 0xFF30CF)// 1 button
          { Serial.println("Button: 1   HEX: 0xFF30CF"); }
    
    else if (results.value == 0xFF18E7)// 2 button
          { Serial.println("Button: 2   HEX: 0xFF18E7"); }
          
    else if (results.value == 0xFF7A85)// 3 button
          { Serial.println("Button: 3   HEX: 0xFF7A85"); }      
    
    else if (results.value == 0xFF10EF)// 4 button
          { Serial.println("Button: 4   HEX: 0xFF10EF"); }
    
    else if (results.value == 0xFF38C7)// 5 button
          { Serial.println("Button: 5   HEX: 0xFF38C7"); }
     
    else if (results.value == 0xFF5AA5)// 6 button
          { Serial.println("Button: 6   HEX: 0xFF5AA5"); }
          
    else if (results.value == 0xFF42BD)// 7 button
          { Serial.println("Button: 7   HEX: 0xFF42BD"); }
          
    else if (results.value == 0xFF4AB5)// 8 button
          { Serial.println("Button: 8   HEX: 0xFF4AB5"); }
          
    else if (results.value == 0xFF52AD)// 9 button
          { Serial.println("Button: 9   HEX: 0xFF52AD"); }
   
     
   delay(250);
   irrecv.resume(); 
   }
}

I want to turn on and turn off an LED with the remote control. The thing is that I have no idea how to combine the button sketch I've modified to work with this IR example. Can anyone help?

const int  buttonPin = 2;   
const int ledPin = 13;

int buttonPushCounter = 0;   
int buttonState = 0;         
int lastButtonState = 0; 

void setup() 
  {
    pinMode(buttonPin, INPUT);
    pinMode(ledPin, OUTPUT);
  }


void loop() 
{
  buttonState = digitalRead(buttonPin);

  if (buttonState != lastButtonState) 
    {
      if (buttonState == HIGH) 
        { buttonPushCounter++; } 
      else {}   
    }

  lastButtonState = buttonState;

  if (buttonPushCounter % 2 == 0) 
    { 
      digitalWrite(ledPin, LOW);
      delay(30);
    } 
  
  else 
    { 
      digitalWrite(ledPin, HIGH);
      delay(30);
    }
   
}

The thing is that I have no idea how to combine the button sketch I've modified to work with this IR example.

Yes you have. Code is a language, you tell the machine what to do each step of the way, that way you can get it to do anything. You must learn the language and techniques and not go asking about every little thing. For example you have the line:-

else {}

What do you think it does? It does nothing, so why have it in?

So if you want to light up an LED when you get a certian button push then add the commands to do it when you see the button push:-

else if (results.value == 0xFF22DD) // Play_pause button
          { 
              Serial.println("Button: Play   HEX: 0xFF22DD");
             digitalWrite(ledPin, HIGH);
 }

Yup, I got that part. But I want to hit the same button again and turn of the LED. That is the part I think is difficult.
Maybe some sort of "wait until something happens" function is whats needed?

you have to include my latch code.

#include <IRremote.h>

int rec_pin = 11;
int gnd_pin = 10;
int vcc_pin = 9;
int LEDpin = 13;

boolean latch = false; // I forgot to put this in
IRrecv irrecv(rec_pin);

decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); 
  pinMode(vcc_pin, OUTPUT);
  pinMode(gnd_pin, OUTPUT);
  digitalWrite(vcc_pin, HIGH);
  digitalWrite(gnd_pin, LOW);
  pinMode(LEDpin, OUTPUT);

}

void loop() {
   if (irrecv.decode(&results))
   {
    if (results.value == 0xFF629D && latch == false) // Mode button pressed once
        { 
        Serial.println("Button: Mode   HEX: 0xFF629D");
        digitalWrite(LEDpin, HIGH);
        latch == true;
         }
          
     else if (results.value == 0xFF629D && latch == true) // Mode button pressed again
        { 
        Serial.println("Button: Mode   HEX: 0xFF629D");
        digitalWrite(LEDpin, LOW);
        latch == false;
         }

     else { }
       
   delay(250);
   irrecv.resume(); 
   }
}

Im not sure if you still need the debounce code for this.