Pages: [1]   Go Down
Author Topic: Remote control light show!  (Read 1688 times)
0 Members and 1 Guest are viewing this topic.
Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I used an IR remote control I bought to create a light show, totally customizable via the remote.







Code:
/*
Arduino infrared remote control light show

Programmed by Dr. John Liu
Revision: 05/20/2011
Free software for educational and personal uses.
No warrantee!
Commercial use without authorization is prohibited.
Find details of my libraries and hardware or contact Dr. Liu at
http://liudr.wordpress.com/
All rights reserved.

Dependent library:
Ken Shirriff's IRremote library
*/

#include <IRremote.h>

#define myClockPin 10  // The clock pin for the shift registers
#define myLatchPin 11  // The latch pin for the shift registers
#define myDataPin 12  // The data pin for the shift registers

int RECV_PIN = 9;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
  pinMode(myDataPin,OUTPUT);
  pinMode(myClockPin,OUTPUT);
  pinMode(myLatchPin,OUTPUT);
  irrecv.enableIRIn(); // Start the receiver
}

void loop()
{
  static byte last; // Stores last decoded result in case the remote control sends repeat code.
  static byte current; // Stores a number to be displayed on the leds. This has nothing to do with IR remotes.
  static boolean forward_dir=true, run=false, mirror=false;
  if (irrecv.decode(&results))
  {
    byte ret=find_key(results.value);
    if (ret==254) last=254; // flush last key with invalid if invalid input is detected in case invalid remote sends out 0xffffffff that triggers repeat.
    else // For valid for this remote
    {
      if ((ret==255)&&(last!=254)) ret=last; // Make sure last key was not invalid before doing repeat.
      else last=ret;
      
      switch (ret)
      {
        case '0':
        current=0;
        break;
        
        case '1':
        current=1;
        break;
        
        case '2':
        current=B11;
        break;
        
        case '3':
        current=B111;
        break;
        
        case '4':
        current=B1111;
        break;
        
        case '5':
        current=B11111;
        break;
        
        case '6':
        current=B111111;
        break;
        
        case '7':
        current=B1111111;
        break;
        
        case '8':
        current=B11111111;
        break;
        
        case 'C':
        current++;
        break;
        
        case 'c':
        current--;
        break;
        
        case 'R':
        current=~current;
        break;
        
        case 'P':
        if (current==0) current=255;
        else current=0;
        run=false;
        mirror=false;
        break;
        
        case 'v':
        current=(current>>1)|(B10000000*(bitRead(current,0)));
        break;
        
        case 'V':
        current=(current<<1)|(bitRead(current,7));
        break;
        
        case 'L':
        forward_dir=!forward_dir;
        break;
        
        case 'T':
        run=!run;
        break;

        case 'A':
        mirror=!mirror;
        break;
      }
        
      updateLed(current);
    }
    irrecv.resume(); // Receive the next value
  }
  
  if (run)
  {
    if (forward_dir)
    {
      current=(current<<1)|(bitRead(current,7));
    }
    else
    {
      current=(current>>1)|(B10000000*(bitRead(current,0)));
    }
      updateLed(current);
  }
  
  if (mirror)
  {
    current=~current;
    updateLed(current);
  }
  
  delay(100);
}

char find_key(unsigned long code)
{
  const char codes[]="ML1470ST258-Iv369APVUCcR";
  byte col, row, temp;
  if (code==0xffffffff) return (255); // Repeat
  else if ((code&0xffff0000)==0x40bf0000)
  {
    temp=lowByte((code-0x40BF00FF)/0x7f8)<<3;
    for (byte i=0;i<8;i++)
    {
      bitWrite(col,i,bitRead(temp,7-i));
    }
    row=col>>2;
    col=col&B11;
    return (codes[col*6+row]);
  }
  else return 254; // Invalid code. Not from this remote control.
}

void updateLed(unsigned char first8)  // Updates LED status using shift registers.
{
  digitalWrite(myLatchPin, LOW);  // Disable update to the output buffers.
  shiftOut(myDataPin, myClockPin, LSBFIRST, first8);//MSBFIRST when flat LSBFIRST when standing.
  digitalWrite(myLatchPin, HIGH);  // Enable update to the output buffers.
}

« Last Edit: May 21, 2011, 10:56:02 pm by liudr » Logged


Pages: [1]   Go Up
Jump to: