Arduino_Scuola
Published © GPL3+

Simple Arduino Based Lego Power Function Receiver

This lesson aims to show how to make a very simple Lego Power Function Receiver.

IntermediateFull instructions provided15 minutes12,655
Simple Arduino Based Lego Power Function Receiver

Things used in this project

Hardware components

Arduino Micro
Arduino Micro
×1
IR Receiver Tsop2238 - 38 KHZ
×1
10 100 ohm 1/4w resistor
×1
Breadboard and Wire Kit
×1
Lego Power Function Remote
×1

Story

Read more

Code

Code snippet #1

Arduino
/*
 * IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv
 * An IR detector/demodulator must be connected to the input RECV_PIN.
 * Version 0.1 July, 2009
 * Copyright 2009 Ken Shirriff
 * http://arcfn.com
 */

/*
 * Testing code for receiving data from the Lego Power Function IR Remote Control http://bit.ly/1Gq18RG
 * If you want to deepen the knowledge of Lego Power Functions Modules, http://www.philohome.com/pf/pf.htm
 *
 * TODO:
 * - test if LEGO® Power Functions IR Speed Remote Control uses the same protocol http://bit.ly/1wDQ13l
 * - sort out some extra codes received
 *
 * Davide Gomba, 16/12/2014
 *Arturo Guadalupi, 17/12/2014
 *
 *Available Macros:
 *CH1_LEFT_UP
 *CH1_LEFT_DOWN
 *CH1_RIGHT_UP
 *CH1_RIGHT_DOWN
 *CH1_BREAK
 *
 *CH2_LEFT_UP
 *CH2_LEFT_DOWN
 *CH2_RIGHT_UP
 *CH2_RIGHT_DOWN
 *CH2_BREAK
 *
 *CH3_LEFT_UP
 *CH3_LEFT_DOWN
 *CH3_RIGHT_UP
 *CH3_RIGHT_DOWN
 *CH3_BREAK
 *
 *CH4_LEFT_UP
 *CH4_LEFT_DOWN
 *CH4_RIGHT_UP
 *CH4_RIGHT_DOWN
 */
 
#include <IRremote.h>
#include "codes.h"

const int RECV_LED = 12;
const int RECV_PIN = 3; // Digital IO pin connected to the IR receiver
IRrecv irrecv(RECV_PIN);

long unsigned startTime = millis();

decode_results results;

void setup()
{
  pinMode(RECV_LED, OUTPUT);
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver

}

void loop() {
  if(millis() - startTime > 200)
    digitalWrite(RECV_LED, LOW);
  else
    digitalWrite(RECV_LED, HIGH);
    
  if (irrecv.decode(&results)) {
    if(legoCode(results.value) != -1) //known code
      startTime = millis();
    // Channel #1
   
    if (legoCode(results.value) == CH1_LEFT_UP)
    {
      Serial.println(" CH - 1 // LEFT UP");
    }
    else if (legoCode(results.value) == CH1_LEFT_DOWN)
  {
    Serial.println(" CH - 1 // LEFT DOWN");
    }
    else  if (legoCode(results.value) == CH1_RIGHT_UP) {
    Serial.println(" CH - 1 // RIGHT UP");
    }
    else if (legoCode(results.value) == CH1_RIGHT_DOWN) {
    Serial.println(" CH - 1 // RIGHT DOWN");
    }
    else if (legoCode(results.value) == CH1_BREAK) {
    Serial.println(" CH - 1 // BREAK");
    }

    // Channel #2

    else if (legoCode(results.value) == CH2_LEFT_UP) {
    Serial.println(" CH - 2 // LEFT UP");
    }
    else if (legoCode(results.value) == CH2_LEFT_DOWN) {
    Serial.println(" CH - 2 // LEFT DOWN");
    }
    else  if (legoCode(results.value) == CH2_RIGHT_UP){
    Serial.println(" CH - 2 // RIGHT UP");
    }
    else if (legoCode(results.value) == CH2_RIGHT_DOWN){
    Serial.println(" CH - 2 // RIGHT DOWN");
    }
    else if (legoCode(results.value) == CH2_BREAK) {
    Serial.println(" CH - 2 // BREAK");
    }
    
    // Channel #3
    
    else if (legoCode(results.value) == CH3_LEFT_UP) {
    Serial.println(" CH - 3 // LEFT UP");
    }
    else if (legoCode(results.value) == CH3_LEFT_DOWN) {
    Serial.println(" CH - 3 // LEFT DOWN");
    }
    else  if (legoCode(results.value) == CH3_RIGHT_UP) {
    Serial.println(" CH - 3 // RIGHT UP");
    }
    else if (legoCode(results.value) == CH3_RIGHT_DOWN){
    Serial.println(" CH - 3 // RIGHT DOWN");
    }
    else if (legoCode(results.value) == CH3_BREAK) {
    Serial.println(" CH - 3 // BREAK");
    }

    // Channel #4

    else if (legoCode(results.value) == CH4_LEFT_UP) {
    Serial.println(" CH - 4 // LEFT UP");
    }
    else if (legoCode(results.value) == CH4_LEFT_DOWN) {
    Serial.println(" CH - 4 // LEFT DOWN");
    }
    else  if (legoCode(results.value) == CH4_RIGHT_UP) {
    Serial.println(" CH - 4 // RIGHT UP");
    }
    else if (legoCode(results.value) == CH4_RIGHT_DOWN) {
    Serial.println(" CH - 4 // RIGHT DOWN");
    }
    else if (legoCode(results.value) == CH4_BREAK) {
    Serial.println(" CH - 4 // BREAK");
    }
    irrecv.resume(); // Receive the next value
  }
}

Credits

Arduino_Scuola

Arduino_Scuola

32 projects • 155 followers

Comments