Mission 18 - Remote IR Decoder - LED Brightness
 DuinoKit Essentials   Started by Daniel Alich   2017-06-27 21:23:15 -04:00   Comments: 2    Viewed: 589

  1. Daniel Alich
    Daniel Alich Member Staff Member
    252_1511884234_Mission 18 – Infrared L

    /*
    RemoteDecode sketch
    Infrared remote control signals are decoded to control LED brightness
    The values for keys 0 through 4 are detected and stored when the sketch starts
    key 0 turns the LED off, the brightness increases in steps with keys 1 through 4
    Found here http://www.buildcircuit.com/infrared-sensor-with-pwm/
    */

    #include <IRremote.h> // IR remote control library
    const int irReceivePin = A0; // pin connected to the output of the IR detector
    const int ledPin = 9; // LED is connected to a PWM pin
    const int numberOfKeys = 5; // 5 keys are learned (0 through 4)
    long irKeyCodes[numberOfKeys]; // holds the codes for each key
    IRrecv irrecv(irReceivePin); // create the IR library
    decode_results results; // IR data goes here

    void setup()
    {
    Serial.begin(9600);
    pinMode(irReceivePin, INPUT);
    pinMode(ledPin, OUTPUT);
    irrecv.enableIRIn(); // Start the IR receiver
    learnKeycodes(); // learn remote control key codes
    Serial.println("Press a remote key");
    }

    void loop()
    {
    long key;
    int brightness;
    if (irrecv.decode(&results))
    {
    // here if data is received
    irrecv.resume();
    key = convertCodeToKey(results.value);
    if (key >= 0)
    {
    Serial.print("Got key ");
    Serial.println(key);
    brightness = map(key, 0, numberOfKeys - 1, 0, 255);
    analogWrite(ledPin, brightness);
    }
    }
    }

    /* Get remote control codes */
    void learnKeycodes()
    {
    while (irrecv.decode(&results)) // empty the buffer
    irrecv.resume();
    Serial.println("Ready to learn remote codes");
    long prevValue = -1;
    int i = 0;
    while ( i < numberOfKeys)
    {
    Serial.print("press remote key ");
    Serial.print(i);
    while (true)
    {
    if ( irrecv.decode(&results) )
    {
    if (results.value != -1 && results.value != prevValue)
    {
    showReceivedData();
    irKeyCodes[i] = results.value;
    i = i + 1;
    prevValue = results.value;
    irrecv.resume(); // Receive the next value
    break;
    }
    irrecv.resume(); // Receive the next value
    }
    }
    }
    Serial.println("Learning complete");
    }

    /* converts a remote protocol code to a logical key code (or -1 if no digit received) */
    int convertCodeToKey(long code)
    {
    for ( int i = 0; i < numberOfKeys; i++)
    {
    if ( code == irKeyCodes[i])
    {
    return i; // found the key so return it
    }
    }
    return -1;
    }
    /*
    display the protocol type and value
    */

    void showReceivedData()
    {
    if (results.decode_type == UNKNOWN)
    {
    Serial.println("-Could not decode message");
    }
    else
    {
    if (results.decode_type == NEC) {
    Serial.print("- decoded NEC: ");
    }
    else if (results.decode_type == SONY) {
    Serial.print("- decoded SONY: ");
    }
    else if (results.decode_type == RC5) {
    Serial.print("- decoded RC5: ");
    }
    else if (results.decode_type == RC6) {
    Serial.print("- decoded RC6: ");
    }
    Serial.print("hex value = ");
    Serial.println( results.value, HEX);
    }
    }
    Daniel Alich, 2017-06-27 21:23:15 -04:00
  2. Josh Rawls
    Josh Rawls Member St. Louis, MO
    This looks like the exact code from Mission 17. Is there other code for this project?
    Josh Rawls, 2018-08-02 23:51:38 -04:00
  3. (You must log in or sign up to post here)

Report Post

Write your reason

Log in | Sign up
Terms & Conditions!
Help!