Skip to content

Instantly share code, notes, and snippets.

@kgsnipes
Created January 27, 2012 21:52
Show Gist options
  • Save kgsnipes/1691124 to your computer and use it in GitHub Desktop.
Save kgsnipes/1691124 to your computer and use it in GitHub Desktop.
s107 control . Reads 8 hex chars on serial and converts to 32 bit instruction
/*
This is the final implementation of reading the command from the serial as string
repeating it to the irled
3b3fff2f -- this is the vertical lift code
*/
int IRledPin = 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[32]; // 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 pulseLength = 0;
int commandLen=0;
void setup()
{
Serial.begin(9600);
// Setup IRLed and Begin Led Pins as outputs
pinMode(IRledPin, OUTPUT); // sets the digital pin as output
pinMode(Begin, OUTPUT); // sets the digital pin as output
}
void loop()
{
readSerialString();
delay(50);
SendCode();
}
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 pulseIR(long microsecs) {
cli(); // this turns off any background interrupts
while (microsecs > 0) {
// 38 kHz is about 13 microseconds high and 13 microseconds low
digitalWrite(IRledPin, HIGH); // this takes about 3 microseconds to happen
delayMicroseconds(10); // hang out for 10 microseconds
digitalWrite(IRledPin, LOW); // this also takes about 3 microseconds
delayMicroseconds(10); // hang out for 10 microseconds
// so 26 microseconds altogether
microsecs -= 26;
}
sei(); // this turns them back on
}
void Zero() {
pulseIR(300);
delayMicroseconds(300);
pulseLength += 600;
}
void One() {
pulseIR(300);
delayMicroseconds(600);
pulseLength += 900;
}
void sendPulseValue(int pulseValue) {
if (pulseValue == 1) One();
else Zero();
}
void SendCode() {
if(commandLen>0){
Serial.println("--");
Serial.println(commandLen);
Serial.println("--");
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);
pulseIR(4000);
delayMicroseconds(2000);
pulseLength = 6000;
for(int i=0;i<(len*4);i++)
{
sendPulseValue(command[i]);
}
//Footer
pulseIR(360);
delayMicroseconds((28600 - pulseLength));
digitalWrite(Begin, LOW);
commandLen=0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment