Well, here is my final code for the Sony Control-S I couldn't wrap my head around a better way to do it so I literally used excel to automate the writing of the functions for the receiver control. I used Excel to turn the Binary string for each control code into the function that calls the series of functions that represent the logical 1's and 0's. If I need to add a new function I can now just type the binary string into excel and it spits out a complete function for the command. its not pretty but it works.
Ignore the light switch stuff this project is just the beginning of a bigger home automation project.
#include <X10Firecracker.h>
int cpin = A0; //control s-pin
int ls1 = A1; //Light Switch Pins
int ls2 = A2;
int ls3 = A3;
int ls1state = 0;
int ls1last = 0;
int ls2state = 0;
int ls2last = 0;
int ls3state = 0;
int ls3last = 0;
int ls1count = 0;
long lastDebounceTime = 0;
long debounceDelay = 50;
int lastls1state = LOW;
void setup() {
pinMode(cpin,OUTPUT);
digitalWrite(ls1,HIGH);
digitalWrite(ls2,HIGH);
digitalWrite(ls3,HIGH);
X10.init( 2, 3, 1 ); //Initalize X10 Cm17a
Serial.begin(9600); //Initalize serial Port
pinMode(cpin, OUTPUT);
digitalWrite(cpin, LOW); // Make sure CPIN is LOW on startup
ls1last =digitalRead(ls1);
delay(1000);
}
void loop()
{
if (Serial.available() > 0) {
int inByte = Serial.read();
//int lastOn = 0;
switch (inByte) {
case 'A': //ON = Uppercase
X10.sendCmd( hcA, 1, cmdOn );
Serial.println("A1 ON");
break;
case 'B':
X10.sendCmd( hcA, 2, cmdOn );
Serial.println("A2 ON");
break;
case 'C':
X10.sendCmd( hcA, 3, cmdOn );
Serial.println("A3 ON");
break;
case 'D':
X10.sendCmd( hcA, 4, cmdOn );
Serial.println("A4 ON");
break;
case 'E':
X10.sendCmd( hcA, 5, cmdOn );
Serial.println("A5 ON");
break;
case 'F':
X10.sendCmd( hcA, 5, cmdOn );
Serial.println("A6 ON");
break;
case 'G':
X10.sendCmd( hcA, 7, cmdOn );
Serial.println("A7 ON");
break;
case 'H':
X10.sendCmd( hcA, 8, cmdOn );
Serial.println("A8 ON");
break;
case 'I':
X10.sendCmd( hcA, 9, cmdOn );
Serial.println("A9 ON");
break;
case 'J':
X10.sendCmd( hcA, 10, cmdOn );
Serial.println("A10 ON");
break;
case 'K':
X10.sendCmd( hcA, 11, cmdOn );
Serial.println("A11 ON");
break;
case 'L':
X10.sendCmd( hcA, 12, cmdOn );
Serial.println("A12 ON");
break;
case 'M':
X10.sendCmd( hcA, 13, cmdOn );
Serial.println("A13 ON");
break;
case 'N':
X10.sendCmd( hcA, 14, cmdOn );
Serial.println("A14 ON");
break;
case 'O':
X10.sendCmd( hcA, 15, cmdOn );
Serial.println("A15 ON");
break;
case 'P':
X10.sendCmd( hcA, 16, cmdOn );
Serial.println("A16 ON");
break;
case 'a': //OFF = Lowercase
X10.sendCmd( hcA, 1, cmdOff );
Serial.println("A1 OFF");
break;
case 'b':
X10.sendCmd( hcA, 2, cmdOff );
Serial.println("A2 OFF");
break;
case 'c':
X10.sendCmd( hcA, 3, cmdOff );
Serial.println("A3 OFF");
break;
case 'd':
X10.sendCmd( hcA, 4, cmdOff );
Serial.println("A4 OFF");
break;
case 'e':
X10.sendCmd( hcA, 5, cmdOff );
Serial.println("A5 OFF");
break;
case 'f':
X10.sendCmd( hcA, 6, cmdOff );
Serial.println("A6 OFF");
break;
case 'g':
X10.sendCmd( hcA, 7, cmdOff );
Serial.println("A7 OFF");
break;
case 'h':
X10.sendCmd( hcA, 8, cmdOff );
Serial.println("A8 OFF");
break;
case 'i':
X10.sendCmd( hcA, 9, cmdOff );
Serial.println("A9 OFF");
break;
case 'j':
X10.sendCmd( hcA, 10, cmdOff );
Serial.println("A10 OFF");
break;
case 'k':
X10.sendCmd( hcA, 11, cmdOff );
Serial.println("A11 OFF");
break;
case 'l':
X10.sendCmd( hcA, 12, cmdOff );
Serial.println("A12 OFF");
break;
case 'm':
X10.sendCmd( hcA, 13, cmdOff );
Serial.println("A13 OFF");
break;
case 'n':
X10.sendCmd( hcA, 14, cmdOff );
Serial.println("A14 OFF");
break;
case 'o':
X10.sendCmd( hcA, 15, cmdOff );
Serial.println("A15 OFF");
break;
case 'p':
X10.sendCmd( hcA, 16, cmdOff );
Serial.println("A16 OFF");
break;
case '+': //bright
X10.sendCmd( hcA, 1, cmdBright );
Serial.println("A BRIGHT OFF");
break;
case '-': //dim
X10.sendCmd( hcA, 1, cmdDim );
Serial.println("A DIM OFF");
break;
case '0':
poweroff();
Serial.println("Sony Power Off");
break;
case '1':
poweron();
Serial.println("Sony Power On");
break;
case '2':
mute();
break;
case '3':
vid2();
break;
case '4':
tuner();
break;
case '5':
tunescandown();
break;
case '6':
tunescanup();
break;
case '7':
{
int z=0;
while (z < 6){
volup();
delay (50);
z++;
}
}
break;
case '8':
{
int y=0;
while (y < 6){
voldn();
delay (50);
y++;
}
}
break;
case '9':
CD();
break;
///for testing
case 'Q':
X10.sendCmd( hcB, 1, cmdOn);
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
break;
case 'q':
X10.sendCmd( hcB, 1, cmdOff );
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
break;
}
}
}
/////////Functions For Sony Control S////////
/// Defining what logic "1" and logic "0" are
//////////////////////////
int L1(){ //Logic 1
//digitalWrite(cpin,LOW);
delayMicroseconds(600);//space
digitalWrite(cpin, HIGH);
delayMicroseconds(1200);
digitalWrite(cpin,LOW);
}
void L0 () { //Logic 0
//digitalWrite(cpin,LOW);
delayMicroseconds(600);//space
digitalWrite(cpin, HIGH);
delayMicroseconds(600);
digitalWrite(cpin, LOW);
}
void SB (){ //start bit transmit
digitalWrite(cpin,LOW);
delayMicroseconds(600);
digitalWrite(cpin, HIGH);
delayMicroseconds(2400);
digitalWrite(cpin, LOW);
}
////Control Functions
//// Send string
void tuneup () { int x=0; while (x < 4){SB();L0();L0();L0();L0();L1();L0();L0();L0();L0();L0();L0();L1();delay (25);x++;}}
void tunedown () { int x=0; while (x < 4){SB();L1();L0();L0();L0();L1();L0();L0();L0();L0();L0();L0();L1();delay (25);x++;}}
void volup () { int x=0; while (x < 4){SB();L0();L1();L0();L0();L1();L0();L0();L0();L0();L0();L0();L1();delay (25);x++;}}
void voldn () { int x=0; while (x < 4){SB();L1();L1();L0();L0();L1();L0();L0();L0();L0();L0();L0();L1();delay (25);x++;}}
void power () { int x=0; while (x < 4){SB();L1();L0();L1();L0();L1();L0();L0();L0();L0();L0();L0();L1();delay (25);x++;}}
void poweron () { int x=0; while (x < 4){SB();L0();L1();L1();L1();L0();L1();L0();L0();L0();L0();L0();L1();delay (25);x++;}}
void poweroff () { int x=0; while (x < 4){SB();L1();L1();L1();L1();L0();L1();L0();L0();L0();L0();L0();L1();delay (25);x++;}}
void tv () { int x=0; while (x < 4){SB();L1();L0();L1();L1();L1();L0();L0();L0();L0();L0();L0();L1();delay (25);x++;}}
void vid2 () { int x=0; while (x < 4){SB();L0();L1();L1();L1();L1();L0();L0();L0();L0();L0();L0();L1();delay (25);x++;}}
void tuner () { int x=0; while (x < 4){SB();L1();L0();L0();L0();L0();L1();L0();L0();L0();L0();L0();L1();delay (25);x++;}}
void CD () { int x=0; while (x < 4){SB();L1();L0();L1();L0();L0();L1();L0();L0();L0();L0();L0();L1();delay (25);x++;}}
void mute () { int x=0; while (x < 4){SB();L0();L0();L1();L0();L1();L0();L0();L0();L0();L0();L0();L1();delay (25);x++;}}
void tunerpreset () { int x=0; while (x < 4){SB();L0();L1();L1();L0();L1();L0();L0();L0();L0();L0();L0();L1();delay (25);x++;}}
void tunescanup () { int x=0; while (x < 4){SB();L0();L1();L0();L0();L1();L0();L0();L1();L0();L1();L1();L0();delay (25);x++;}}
void tunescandown () { int x=0; while (x < 4){SB();L1();L1();L0();L0();L1();L0();L0();L1();L0();L1();L1();L0();delay (25);x++;}}
///FEEDBACk Function
void flash (){
int x;
while (x < 6){
digitalWrite(13,HIGH);
delay (100);
digitalWrite (13,LOW);
x++;
}
}