help getting ir and buttons to work at same time

I'm very new to programing and I'm trying to work out how to get leds to work with remote and buttons
i can get them to work till i try to put them together. then the buttons don't work independently

#include <IRremote.h>

#define re_play 0x800F0416
#define re_enter 0x800F8422
#define RECV_PIN 11
#define LED_PIN 13 // choose the pin for the LED
const int play_button = 2; 
const int enter_button = 3;

byte ledState;
IRrecv irrecv(RECV_PIN);
decode_results results;
boolean power_state = LOW;

void setup(){
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(LED_PIN, OUTPUT);  // declare LED as output
  pinMode(play_button, INPUT);
  pinMode(enter_button, INPUT);
  int buttonState = 0;
}

void loop() {
  if (irrecv.decode(&results)) {
    if (results.value == 0x800F0416) { // Sony DVD play
      digitalWrite(LED_PIN, HIGH);
    } 
    else if (results.value == 0x800F8422) { // Sony DVD stop
      digitalWrite(LED_PIN, LOW
     );
   }
     else if (play_button == HIGH) { // Sony DVD play
      digitalWrite(LED_PIN, HIGH);
    } 
    irrecv.resume(); // Receive the next value
  }

}

Hi Mark

else if (play_button == HIGH)

If you are trying to read the state of the pin defined by play_button, this should be ...

else if (digitalRead(play_button) == HIGH)

Also, this statement is inside the if statement ...

  if (irrecv.decode(&results)) {

So the pin will only be checked if the decode function returns true, which I guess happens only when an IR code is received? You probably need to move the pin check so it happens each time round loop().

And what do you want to happen if digitalRead(play_button) returns LOW? There is nothing to handle this at the moment.

Regards

Ray

The button is a simple push button so on high it turns on led and low it just waiting . I might have gone the wrong way for what I want to do. I want to print commands to pc running mediaportal. So when I hit a button on remote it prints that button name and when I push a switch it prints the same command as remote so play on remote and the play momentary button send the same command. So I was trying to turn a led on and off to test but I can get the button to work or the ir but not at same time. I'm going to try a little more because I'm not shore if the difference will lead to different problems

The demo several things at a time might give you some ideas about how to separate the different activities that you are trying to manage.

...R

The button is a simple push button so on high it turns on led and low it just waiting .

Wrong. There is no relationship between the switch state and the LED state EXCEPT as defined in the code. Pressing the switch does NOT cause the LED to turn on.

void loop() {
  if (irrecv.decode(&results)) {
    if (results.value == 0x800F0416) { // Sony DVD play
      digitalWrite(LED_PIN, HIGH);
    } 
    else if (results.value == 0x800F8422) { // Sony DVD stop
      digitalWrite(LED_PIN, LOW
     );
   }
     else if (play_button == HIGH) { // Sony DVD play
      digitalWrite(LED_PIN, HIGH);
    } 
    irrecv.resume(); // Receive the next value
  }

}

Change to:

void loop() {
  if (irrecv.decode(&results)) {
    if (results.value == 0x800F0416) { // Sony DVD play
      digitalWrite(LED_PIN, HIGH);
    } 
    else if (results.value == 0x800F8422) { // Sony DVD stop
      digitalWrite(LED_PIN, LOW);
   }
  irrecv.resume(); // Receive the next value
  }
     if (play_button == HIGH) { // Sony DVD play
      digitalWrite(LED_PIN, HIGH);
    } 
  }

Thanks steinie44 ,Hackscribble ,Robin2 that helped a lot

I'm still a little lost about

Hackscribble:
Also, this statement is inside the if statement ...

  if (irrecv.decode(&results)) {

So the pin will only be checked if the decode function returns true, which I guess happens only when an IR code is received? You probably need to move the pin check so it happens each time round loop().

so if you could talk slow and use little words that would be great :fearful:

this is what i got form that and i think it works but don't know what will happen when i add more to it

/*
 * set-top receiver and real time display: receive hex from remote
 * and buttons on front panel and transmit to windows for use 
 * in media center program
 * An IR detector/demodulator must be connected to the input RECV_PIN.
 * 10-12 buttons be connected to the input BUTTON_PIN; this is the
 * play, stop, pause, ff., rw., power, up, down, left, right, enter.
 * 1-2 leds for stats
 * 1-2 relays for power up and shut down
 * lcd display for time and now playing info
 *
 * The logic is:
 * any button pressed on the remote or button on front panel 
 * will be passed to windows and send id. 
 *It  will check for pc and led will report state
 * relay will power on and off motherboard
 * lcd will print to screen what ever windows passes it
 */
#include <IRremote.h>

#define re_play 0x800F0416
#define re_enter 0x800F8422
#define RECV_PIN 11
const int play_button = 2; 
const int enter_button = 3;
IRrecv irrecv(RECV_PIN);
decode_results results;
boolean power_state = LOW;

void setup(){
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(play_button, INPUT);
  pinMode(enter_button, INPUT);
  int buttonState = 0;
}

void loop() {
  if (irrecv.decode(&results)) {
    if (results.value == 0x800F0416) { // Sony DVD play
      Serial.println("remote play");
    } 
    else if (results.value == 0x800F8422) { // Sony DVD stop
      Serial.println("remote enter");
    }

    irrecv.resume(); // Receive the next value
  }
  else if (digitalRead(play_button) == LOW) {
    Serial.println("play button");
    delay(1000); 
  } 
  else if (digitalRead(enter_button) == LOW) {
    Serial.println("enter_button");
    delay(1000); 
  } 
}

 [code]


also when i was look at some one Else's code 


[code]void loop() {
  if (irrecv.decode(&results)) {  //If IR receive results are detected
    // Serial.println(results.value, HEX);
    switch (results.value) {

      //Power
    case OK_POS1:
      //   Serial.println("Power");
      ledState = 125;
      digitalWrite(LED_PIN, LOW);  // turn LED ON
      break;

    case OK_POS2:
      digitalWrite(LED_PIN, HIGH);  // turn LED ON
      break;

      //Dim
    case VOL_DWN_POS1:
      ledState -= 10;
      if (ledState > 0){
        analogWrite(LED_PIN, ledState);  // Dim LED
      }
      break;

    case VOL_DWN_POS2:
      ledState -= 10;
      if (ledState > 0){
        analogWrite(LED_PIN, ledState);  // Dim LED
      }
      break;
[code]

i cant find what {case} means is that like {if}

thanks again you guys helped a lot

[/code][/code][/code]

There's nothing wrong with this statement in itself.

if (irrecv.decode(&results)) {

The if statement is true when a valid IR code is received (I think).

What I was saying was that, in your original program, the check on whether the play button had been pressed was inside this if statement.

So, if you don't send an IR code, the play button won't be checked either.

The first part of the code you've posted in reply #6 has the play button (and the other button) code in the right place.

Hackscribble:
There's nothing wrong with this statement in itself.

if (irrecv.decode(&results)) {

The if statement is true when a valid IR code is received (I think).

What I was saying was that, in your original program, the check on whether the play button had been pressed was inside this if statement.

So, if you don't send an IR code, the play button won't be checked either.

The first part of the code you've posted in reply #6 has the play button (and the other button) code in the right place.

Cool cause I was staring at for 30 mins trying to got it I thought you ment it had to move ( this is deffently a it me not you thing)

I saw a code and the guy use case instead of if . It at the bottom of the code in my last post and I can't find the definition of case any were any ?

The switch statement (with the cases in it) is equivalent to using if / else if.

So this ...

switch (results.value) {

    case OK_POS1:
      //   Serial.println("Power");
      ledState = 125;
      digitalWrite(LED_PIN, LOW);  // turn LED ON
      break;

    case OK_POS2:
      digitalWrite(LED_PIN, HIGH);  // turn LED ON
      break;

    etc

... works the same as this ...

if (results.value == OK_POS1)
    {
        //   Serial.println("Power");
        ledState = 125;
        digitalWrite(LED_PIN, LOW);  // turn LED ON
    }
    else if (results.value == OK_POS2)
    {
        digitalWrite(LED_PIN, HIGH);  // turn LED ON
    }
    else if   etc