IR and Push Button Dimming a LED

when i upload the code for two push button to increase and decrease the brightness of the LED, it works fine but when i started to add the code for IR to control the same command as the push button do it doesn’t work. why it is not working. did i miss something in the code???
:confused: :confused: :confused: :confused: :confused:

when i press the button i see output in serial monitor
1
2
3
4
5
6
7
6

#include <IRremote.h> // IR remote control library

int ledc = 9; //Led Cool White:
int ledw = 10; //Led Warm White:
int inb = 7;//Increase Button:
int deb = 8;//Decrease Button:
int RECV_PIN = 11;

//Variable that change

int brightness = 0;
int maxbright = 10;//step of the brightness
int brightup = 1;//Increment of Brightness
int before;
IRrecv irrecv(RECV_PIN); // create the IR library
decode_results results; // IR data goes here


void setup() {

  pinMode(ledc, OUTPUT);
  pinMode(ledw, OUTPUT);
  pinMode(inb, INPUT);
  pinMode(deb, INPUT);
  pinMode(RECV_PIN, INPUT);
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the IR receiver
  IRrecv irrecv(RECV_PIN);//receiver pin
  before = 0; //LED is turned off
  decode_results results;//Decode


}

void loop() {


  if (irrecv.decode(&results)); {

    if (results.value == 0x20DF8D72) { //Code to turn the LED ON/OFF
      if (before == 0) { // if the LED was turned off, then we turn it on
        digitalWrite(ledc, HIGH);
        before = 1; //LED is now turned on
      }
    }
    else {
      digitalWrite(ledc, LOW); //if the LED was turned on, then we turn it off
      before = 0;
    }
    //code where ir increase brightness //
    if (results.value == 0xA90  && brightness < maxbright && before == 1) {

      brightness = brightness + brightup;
      Serial.println(brightness);
      pinMode(ledc, OUTPUT);
    }
    //code where ir decrease brightness://
    if (results.value == 0xA90 && brightness > 0 && before == 1) {
      brightness = brightness - brightup;
      Serial.println(brightness);
    }
    irrecv.resume();
  }


  if (digitalRead(inb) == HIGH && brightness < maxbright ) {
    brightness = brightness + brightup;
    Serial.println(brightness);
    pinMode(ledc, OUTPUT);

  }

  if (digitalRead(deb) == HIGH && brightness > 0) {
    brightness = brightness - brightup;
    Serial.println(brightness);
  }
  delay(100);
  analogWrite(ledc, map(brightness, 0, maxbright, 0, 255)); // map it to the range of the analog out:
}

You didn’t use code tags to post your code. It’s important that you use them - if you don’t, the forum software sometimes interprets sequences of characters in the code as directives to format text in some way. It won’t display those sequences, and it will unexpectedly reformat the rest of the text. When that happens, and someone tries to copy your code and paste it into the IDE, it often throws an error, and readers will complain that the code fails to compile. Using code tags also makes it easier to read, and can be copied with a single mouse click.

Unless the sketch is too large, it’s better if you post your code, rather than attach it. When it’s attached, we have to download it, open it in a text editor, then copy it to the IDE. It’s much easier to just view the code in your post.

The code tags make the code look

like this

when posting source code files.
If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. There are many other things that programmers do to make their code understandable. You haven’t done them. Please learn to do them today, as a courtesy to the members who volunteer their time to help you here. One is to use a standard indentation to clearly show the code blocks. Never put more than one statement per line. Place any brackets by themselves on a separate line. Before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the [code] and [/code] metatags.

When you are finished that, please read this post:

How to use this forum - please read.

You designed your code to work with 2 buttons which were connected directly to the Arduino. Now you no longer use the buttons and have added an infrared receiver and modified the program accordingly. When you press the appropriate buttons on your infrared remote control unit, you see the results in at least one of the Serial.print statements in your code. Is that right ?

If that is the case, comment out all the Serial.print statements except the one in this code block to see if it gets here, or alternatively change them so you know at least which Serial.print statements are printing to the console. Also print out the value you are getting from the map() function.

      if (digitalRead(deb) == HIGH && brightness > 0) {
        brightness = brightness - brightup;
        Serial.println(brightness);
        }
      delay(100);
      analogWrite(ledc, map(brightness, 0, maxbright, 0, 255)); // map it to the range of the analog out:

I don't get any output on led but i get data when i pressed the push button not in ir.

i'm just starting learning how to code.

Does anybody know what to add to make this code work

I am newbie too but have managed to get the RF to receive a signal from nearly any RF remote controller ,once i did this the code was easy to alter so i can control LED,Servo and moters…

I found it important to use serial print to give me the (i believe is HEX code ?) numbers that match the button i pressed on the Remote control

Button 1 on my remote control (controller is for my TV) gives me 0x687CBE2L
Button 2 on my remote control (controller is for my TV) gives me 0x687CBEAL

different remote controls give different numbers to find out which button give which code i use serial print …

This is an example of how i used the code from Remote control to turn a servo sweep on

Please note this is not my code i have left the comments etc in pointing to the original coders to whom i thank…

// example 32.1 - IR receiver code repeater

// http://tronixstuff.com/tutorials > chapter 32

// based on code by Ken Shirriff - http://arcfn.com

#include <IRremote.h> // use the library
#include <Servo.h>

Servo myservo; // create servo object to control a servo
int receiver = 11; // pin 1 of IR receiver to Arduino digital pin 11

IRrecv irrecv(receiver); // create instance of ‘irrecv’

decode_results results;
int pos = 0; // variable to store the servo position

void setup()

{
myservo.attach(9);
Serial.begin(9600); // for serial monitor output

irrecv.enableIRIn(); // Start the receiver

}

void loop()

{

if (irrecv.decode(&results)) // have we received an IR signal?

{
{
if (results.value == 0x687CBE2L) { // 0x687CBE2L button press on remote control
Serial.write(“ON\n”);
myservo.write(170); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}

else if (results.value == 0x687CBEAL) {
Serial.write(“off\n”);
myservo.write(0); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position

}
}
Serial.println(results.value, HEX); // display it on serial monitor in hexadecimal

irrecv.resume(); // receive the next value

} // Your loop can do other things while waiting for an IR command

}