Created
January 27, 2012 10:57
-
-
Save kgsnipes/1688269 to your computer and use it in GitHub Desktop.
IR Remote control for arduino. Hex string Command is read serially and then transmitted from IRlED on pin 14
This file contains 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
/* | |
This is the final implementation of reading the command from the serial as string | |
repeating it to the irled | |
*/ | |
int IRled = 14; //this is the IR LED | |
int Begin = 13; //this indicates the status of the command sent | |
int serIn; // var that will hold the bytes-in read from the serialBuffer | |
char serInString[100]; // array that will hold the different bytes 100=100characters; | |
int serInIndx = 0; // index of serInString[] in which to insert the next incoming byte | |
int serOutIndx = 0; // index of the outgoing serInString[] array; | |
int commandLen=0; | |
void setup() | |
{ | |
Serial.begin(9600); | |
// Setup IRLed and Begin Led Pins as outputs | |
pinMode(IRled, OUTPUT); // sets the digital pin as output | |
pinMode(Begin, OUTPUT); // sets the digital pin as output | |
} | |
void loop() | |
{ | |
readSerialString(); | |
delay(10); | |
sendCodeToSonyIR(); | |
} | |
void readSerialString() { | |
int sb; | |
if (Serial.available()) { | |
//Serial.print("reading Serial String: "); //optional confirmation | |
while (Serial.available()) { | |
sb = Serial.read(); | |
serInString[serInIndx] = sb; | |
serInIndx++; | |
commandLen++; | |
//serialWrite(sb); //optional confirmation | |
} | |
//Serial.println(); | |
} | |
} | |
void printSerialString() { | |
if (serInIndx > 0) { | |
Serial.print("Arduino memorized that you said: "); | |
//loop through all bytes in the array and print them out | |
for (serOutIndx = 0; serOutIndx < serInIndx; serOutIndx++) { | |
Serial.print(serInString[serOutIndx]); //print out the byte at the specified index | |
//serInString[serOutIndx] = ""; //optional: flush out the content | |
} | |
//reset all the functions to be able to fill the string back with content | |
serOutIndx = 0; | |
serInIndx = 0; | |
Serial.println(); | |
} | |
} | |
void sendCodeToSonyIR() | |
{ | |
if(commandLen>0){ | |
int len=commandLen; | |
int command[len*4]; | |
for(int i=0;i<len;i++) | |
{ | |
if(serInString[i]=='0') | |
{ | |
command[(i*4)+0]=0; | |
command[(i*4)+1]=0; | |
command[(i*4)+2]=0; | |
command[(i*4)+3]=0; | |
} | |
else if(serInString[i]=='1') | |
{ | |
command[(i*4)+0]=0; | |
command[(i*4)+1]=0; | |
command[(i*4)+2]=0; | |
command[(i*4)+3]=1; | |
} | |
else if(serInString[i]=='2') | |
{ | |
command[(i*4)+0]=0; | |
command[(i*4)+1]=0; | |
command[(i*4)+2]=1; | |
command[(i*4)+3]=0; | |
} | |
else if(serInString[i]=='3') | |
{ | |
command[(i*4)+0]=0; | |
command[(i*4)+1]=0; | |
command[(i*4)+2]=1; | |
command[(i*4)+3]=1; | |
} | |
else if(serInString[i]=='4') | |
{ | |
command[(i*4)+0]=0; | |
command[(i*4)+1]=1; | |
command[(i*4)+2]=0; | |
command[(i*4)+3]=0; | |
} | |
else if(serInString[i]=='5') | |
{ | |
command[(i*4)+0]=0; | |
command[(i*4)+1]=1; | |
command[(i*4)+2]=0; | |
command[(i*4)+3]=1; | |
} | |
else if(serInString[i]=='6') | |
{ | |
command[(i*4)+0]=0; | |
command[(i*4)+1]=1; | |
command[(i*4)+2]=1; | |
command[(i*4)+3]=0; | |
} | |
else if(serInString[i]=='7') | |
{ | |
command[(i*4)+0]=0; | |
command[(i*4)+1]=1; | |
command[(i*4)+2]=1; | |
command[(i*4)+3]=1; | |
} | |
else if(serInString[i]=='8') | |
{ | |
command[(i*4)+0]=1; | |
command[(i*4)+1]=0; | |
command[(i*4)+2]=0; | |
command[(i*4)+3]=0; | |
} | |
else if(serInString[i]=='9') | |
{ | |
command[(i*4)+0]=1; | |
command[(i*4)+1]=0; | |
command[(i*4)+2]=0; | |
command[(i*4)+3]=1; | |
} | |
else if(serInString[i]=='a') | |
{ | |
command[(i*4)+0]=1; | |
command[(i*4)+1]=0; | |
command[(i*4)+2]=1; | |
command[(i*4)+3]=0; | |
} | |
else if(serInString[i]=='b') | |
{ | |
command[(i*4)+0]=1; | |
command[(i*4)+1]=0; | |
command[(i*4)+2]=1; | |
command[(i*4)+3]=1; | |
} | |
else if(serInString[i]=='c') | |
{ | |
command[(i*4)+0]=1; | |
command[(i*4)+1]=1; | |
command[(i*4)+2]=0; | |
command[(i*4)+3]=0; | |
} | |
else if(serInString[i]=='d') | |
{ | |
command[(i*4)+0]=1; | |
command[(i*4)+1]=1; | |
command[(i*4)+2]=0; | |
command[(i*4)+3]=1; | |
} | |
else if(serInString[i]=='e') | |
{ | |
command[(i*4)+0]=1; | |
command[(i*4)+1]=1; | |
command[(i*4)+2]=1; | |
command[(i*4)+3]=0; | |
} | |
else if(serInString[i]=='f') | |
{ | |
command[(i*4)+0]=1; | |
command[(i*4)+1]=1; | |
command[(i*4)+2]=1; | |
command[(i*4)+3]=1; | |
} | |
} | |
for(int i=0;i<(len*4);i++) | |
{ | |
Serial.println(command[i]); | |
} | |
digitalWrite(Begin, HIGH); // Signal Begin LED ON | |
for (int i=1; i <= 3; i++) // Send Command 3 times as per Sony Specs | |
{ | |
header(); // Send the Start header | |
for (int i=0; i < (len*4); i++) // Loop to send the bits | |
{ | |
if(command[i] == 1) // Is Data_is_One to be sent ? | |
{ | |
Data_is_One(); // Yes, send a Data_is_One bit | |
} | |
else // No, Data_is_Zero to be sent | |
{ | |
Data_is_Zero(); // Send a Data_is_Zero bit | |
} | |
} | |
delay(11); // Delay Padding to give approx 45mS between command starts | |
} | |
digitalWrite(Begin, LOW); // Begin LED OFF | |
commandLen=0; | |
} | |
} | |
// Routine to give the 40kHz burst signal | |
void burst() // 40KHz burst | |
{ | |
digitalWrite(IRled, HIGH); // sets the pin on | |
delayMicroseconds(10); // pauses for 13 microseconds (fudged to 10uS Delay) | |
digitalWrite(IRled, LOW); // sets the pin off | |
delayMicroseconds(8); // pauses for 12 microseconds (fudged to 8uS Delay) | |
} | |
// Routine to give a quiet period of data | |
void quiet() // Quiet burst | |
{ | |
digitalWrite(IRled, LOW); // sets the pin off | |
delayMicroseconds(10); // pauses for 13 microseconds (fudged to 10uS Delay) | |
digitalWrite(IRled, LOW); // sets the pin off | |
delayMicroseconds(8); // pauses for 12 microseconds (fudged to 8uS Delay) | |
} | |
// Routine to send header data burst | |
// This allows the IR reciever to set its AGC (Gain) | |
// Header Burst Timing is 96 * 0.025uS = 2.4mS | |
// Quiet Timing is 24 * 0.025uS = 600uS | |
void header() | |
{ | |
for (int i=1; i <= 96; i++){ | |
burst(); // 40kHz burst | |
} | |
for (int i=1; i <= 24; i++){ | |
quiet(); // No 40 kHz | |
} | |
} | |
// Routine to send one data burst | |
// Burst Timing is 48 * 0.025uS = 1.2mS | |
// Quiet Timing is 24 * 0.025uS = 600uS | |
void Data_is_One() | |
{ | |
for (int i=1; i <= 48; i++){ | |
burst(); // 40kHz burst | |
} | |
for (int i=1; i <= 24; i++){ | |
quiet(); // No 40 kHz | |
} | |
} | |
// Routine to send zero data burst | |
// Burst Timing is 24 * 0.025uS = 600uS | |
// Quiet Timing is 24 * 0.025uS = 600uS | |
void Data_is_Zero() | |
{ | |
for (int i=1; i <= 24; i++){ | |
burst(); // 40 kHz burst | |
} | |
for (int i=1; i <= 24; i++){ | |
quiet(); // No 40 kHz | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment