public
Last active

PWM control signals using Arduino and Infrared

  • Download Gist
gistfile1.ino
Arduino
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
/*
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
*/
#include <IRremote.h> // IR remote control library
const int irReceivePin = 2; // 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);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.