Created
February 28, 2012 18:11
-
-
Save anonymous/1934041 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//IRMusicPlayer | |
//Jennifer Schumaker | |
//22.Feb.12 | |
// PIND is an Arduino command that gets a byte containing the input | |
// status of bytes 0 through 7. In our case we want the bit | |
// corresponding to pin 2. | |
// NOTE: If you are seeking input from ports 8 - 15 use PINDB. | |
// For analong pins 0 - 5 use PINC | |
// | |
// | |
#include "pitches.h" | |
#define IRInputPin0 1 | |
#define IRInputPin1 2 | |
#define IRInputPin2 4 | |
#define IRInputPin3 8 | |
#define IRInputPin4 16 | |
#define IRInputPin5 32 | |
#define IRInputPin6 64 | |
#define IRInputPin7 128 | |
#define IRpin_PIN PIND | |
// PIND brings in a byte each bit of which holds the INPUT status | |
// of the Arduino's pins 0 through 7. In the following definition | |
// we select pin 2 to be our input pin for the IR detector. The | |
// binary value of the number 4 is all zeros except for the bit | |
// that corresponds to pin 2. To determine if the IR detector is | |
// seeing infrared we logically combine PIND and IRpin_PIN and test | |
// this bit. | |
// Pin to be tested Mask in binary Mask in DEC | |
// 0 00000001 1 | |
// 1 00000010 2 | |
// 2 00000100 4 | |
// 3 00001000 8 | |
// 4 00010000 16 | |
// 5 00100000 32 | |
// 6 01000000 64 | |
// 7 10000000 128 | |
#define IRpin IRInputPin2 | |
#define NUMBITS 12 | |
#define RES 50 | |
#define LOWUS 600 | |
#define HIGHUS 1200 | |
#define MAXPULSE 5000 | |
#define MAXREST 20000 | |
#define soundPin 4 | |
uint16_t currMessage = 0; | |
#define HIGHP true | |
#define LOWP false | |
void setup(){ | |
Serial.begin(9600); | |
} | |
void loop(){ | |
if(readIR()) | |
{ | |
printFull(currMessage); | |
} | |
} | |
int wait(boolean type, int timeout) | |
{ | |
int num = 0; // we wait in increments of RES in microseconds. | |
while( ( type == HIGHP && (IRpin_PIN & IRpin) ) | |
|| | |
( type == LOWP && !(IRpin_PIN & IRpin ) ) ) | |
{ | |
// pin is still HIGH | |
delayMicroseconds(RES); | |
num++; | |
// if the pulse is too long we 'timed out' | |
if(num*RES >= timeout) | |
{ | |
return -1; | |
} | |
} | |
return num*RES; // return the duration of the pulse | |
} | |
// Data is collected here. | |
boolean readIR() | |
{ | |
currMessage = 0; | |
if( wait(HIGHP, MAXREST) == -1) | |
{//timeout ( before sees a low pulse) | |
return false; | |
} | |
if(wait(LOWP, MAXPULSE) == -1) //start pulse | |
{ | |
return false; //timeout | |
} | |
//start decoding message | |
for(int i = 0;i<NUMBITS;i++) | |
{ | |
if(wait(HIGHP, MAXPULSE) == -1) | |
{return false;} | |
int data = wait(LOWP, MAXPULSE); | |
if(data == -1) | |
{ return false; } //timeout | |
// Here the pulse is put in it's | |
// appropriate bit position. | |
if(data * 2 > LOWUS + HIGHUS) | |
{ | |
//one | |
//currMessage = currMessage | (1 << (15-i)); | |
currMessage = currMessage | (1 << i); | |
} | |
else | |
{ | |
//zero | |
} | |
} | |
return true; | |
} | |
void printFull(uint16_t n){ | |
uint8_t command = n & 127; | |
uint16_t address = (n >> 7); | |
if(command == 4){ | |
tone(soundPin,NOTE_A4); | |
} | |
else{ | |
noTone(soundPin); | |
} | |
} | |
//////////// END OF PROGRAM ///////////////// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment