Go Down

Topic: help with button (Read 739 times) previous topic - next topic

HazardsMind

Here, this is the working code.
Code: [Select]
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

}


My GitHub:
https://github.com/AndrewMascolo?tab=repositories

hansibull

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):

Code: [Select]


#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?

Code: [Select]


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);
    }
   
}

Grumpy_Mike

Quote
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:-
Code: [Select]
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:-
Code: [Select]
else if (results.value == 0xFF22DD) // Play_pause button
          {
              Serial.println("Button: Play   HEX: 0xFF22DD");
             digitalWrite(ledPin, HIGH);
}   




hansibull

#18
Dec 13, 2012, 05:33 pm Last Edit: Dec 13, 2012, 05:36 pm by hansibull Reason: 1
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?

HazardsMind

#19
Dec 13, 2012, 05:44 pm Last Edit: Dec 13, 2012, 05:46 pm by HazardsMind Reason: 1
you have to include my latch code.

Code: [Select]

#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.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

hansibull

Saw a tutorial, and i fount out I could use this code insted:

int on = 0;


void loop() {
   if (irrecv.decode(&results))
   {
    if (results.value == 0xFFA25D) // I/O button
          {
           on = !on;
           digitalWrite(ledPin, on ? HIGH : LOW);
          }


Thank you for all the help! :D

Grumpy_Mike

Quote
But I want to hit the same button again and turn of the LED.

Do you mean turn OFF the LED.
Quote
That is the part I think is difficult.

No it is simple.
You have a variable to hold the value of the LED and then to toggle the value. In the digital write statement you write out the variable not high or low.
Code: [Select]

// define a variable at the start of the code;
int LEDstate = HIGH;
//

// then toggle it when you want, lots of ways to do this but this is the easiest to understand
if(LEDstate == HIGH) LEDstate = LOW; else LEDstate = HIGH;
// then write it out
digitalWrite(ledPin, LEDstate);

HazardsMind

Quote
digitalWrite(ledPin, on ? HIGH : LOW);


Hmmm, I wasn't aware that arduino could do that.

Yea, that is MUCH simpler.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Go Up