Components and supplies
IR Receiver Tsop2238 - 38 KHZ
Breadboard and Wire Kit
Lego Power Function Remote
10 100 ohm 1/4w resistor
Arduino Micro
Project description
Code
Code snippet #1
arduino
1/* 2 * IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv 3 * An IR detector/demodulator must be connected to the input RECV_PIN. 4 * Version 0.1 July, 2009 5 * Copyright 2009 Ken Shirriff 6 * http://arcfn.com 7 */ 8 9/* 10 * Testing code for receiving data from the Lego Power Function IR Remote Control http://bit.ly/1Gq18RG 11 * If you want to deepen the knowledge of Lego Power Functions Modules, http://www.philohome.com/pf/pf.htm 12 * 13 * TODO: 14 * - test if LEGO® Power Functions IR Speed Remote Control uses the same protocol http://bit.ly/1wDQ13l 15 * - sort out some extra codes received 16 * 17 * Davide Gomba, 16/12/2014 18 *Arturo Guadalupi, 17/12/2014 19 * 20 *Available Macros: 21 *CH1_LEFT_UP 22 *CH1_LEFT_DOWN 23 *CH1_RIGHT_UP 24 *CH1_RIGHT_DOWN 25 *CH1_BREAK 26 * 27 *CH2_LEFT_UP 28 *CH2_LEFT_DOWN 29 *CH2_RIGHT_UP 30 *CH2_RIGHT_DOWN 31 *CH2_BREAK 32 * 33 *CH3_LEFT_UP 34 *CH3_LEFT_DOWN 35 *CH3_RIGHT_UP 36 *CH3_RIGHT_DOWN 37 *CH3_BREAK 38 * 39 *CH4_LEFT_UP 40 *CH4_LEFT_DOWN 41 *CH4_RIGHT_UP 42 *CH4_RIGHT_DOWN 43 */ 44 45#include <IRremote.h> 46#include "codes.h" 47 48const int RECV_LED = 12; 49const int RECV_PIN = 3; // Digital IO pin connected to the IR receiver 50IRrecv irrecv(RECV_PIN); 51 52long unsigned startTime = millis(); 53 54decode_results results; 55 56void setup() 57{ 58 pinMode(RECV_LED, OUTPUT); 59 Serial.begin(9600); 60 irrecv.enableIRIn(); // Start the receiver 61 62} 63 64void loop() { 65 if(millis() - startTime > 200) 66 digitalWrite(RECV_LED, LOW); 67 else 68 digitalWrite(RECV_LED, HIGH); 69 70 if (irrecv.decode(&results)) { 71 if(legoCode(results.value) != -1) //known code 72 startTime = millis(); 73 // Channel #1 74 75 if (legoCode(results.value) == CH1_LEFT_UP) 76 { 77 Serial.println(" CH - 1 // LEFT UP"); 78 } 79 else if (legoCode(results.value) == CH1_LEFT_DOWN) 80 { 81 Serial.println(" CH - 1 // LEFT DOWN"); 82 } 83 else if (legoCode(results.value) == CH1_RIGHT_UP) { 84 Serial.println(" CH - 1 // RIGHT UP"); 85 } 86 else if (legoCode(results.value) == CH1_RIGHT_DOWN) { 87 Serial.println(" CH - 1 // RIGHT DOWN"); 88 } 89 else if (legoCode(results.value) == CH1_BREAK) { 90 Serial.println(" CH - 1 // BREAK"); 91 } 92 93 // Channel #2 94 95 else if (legoCode(results.value) == CH2_LEFT_UP) { 96 Serial.println(" CH - 2 // LEFT UP"); 97 } 98 else if (legoCode(results.value) == CH2_LEFT_DOWN) { 99 Serial.println(" CH - 2 // LEFT DOWN"); 100 } 101 else if (legoCode(results.value) == CH2_RIGHT_UP){ 102 Serial.println(" CH - 2 // RIGHT UP"); 103 } 104 else if (legoCode(results.value) == CH2_RIGHT_DOWN){ 105 Serial.println(" CH - 2 // RIGHT DOWN"); 106 } 107 else if (legoCode(results.value) == CH2_BREAK) { 108 Serial.println(" CH - 2 // BREAK"); 109 } 110 111 // Channel #3 112 113 else if (legoCode(results.value) == CH3_LEFT_UP) { 114 Serial.println(" CH - 3 // LEFT UP"); 115 } 116 else if (legoCode(results.value) == CH3_LEFT_DOWN) { 117 Serial.println(" CH - 3 // LEFT DOWN"); 118 } 119 else if (legoCode(results.value) == CH3_RIGHT_UP) { 120 Serial.println(" CH - 3 // RIGHT UP"); 121 } 122 else if (legoCode(results.value) == CH3_RIGHT_DOWN){ 123 Serial.println(" CH - 3 // RIGHT DOWN"); 124 } 125 else if (legoCode(results.value) == CH3_BREAK) { 126 Serial.println(" CH - 3 // BREAK"); 127 } 128 129 // Channel #4 130 131 else if (legoCode(results.value) == CH4_LEFT_UP) { 132 Serial.println(" CH - 4 // LEFT UP"); 133 } 134 else if (legoCode(results.value) == CH4_LEFT_DOWN) { 135 Serial.println(" CH - 4 // LEFT DOWN"); 136 } 137 else if (legoCode(results.value) == CH4_RIGHT_UP) { 138 Serial.println(" CH - 4 // RIGHT UP"); 139 } 140 else if (legoCode(results.value) == CH4_RIGHT_DOWN) { 141 Serial.println(" CH - 4 // RIGHT DOWN"); 142 } 143 else if (legoCode(results.value) == CH4_BREAK) { 144 Serial.println(" CH - 4 // BREAK"); 145 } 146 irrecv.resume(); // Receive the next value 147 } 148}
Comments
Only logged in users can leave comments