Here is the receiver code
int redpin = 3;
int greenpin = 5;
int bluepin = 6;
int amberpin = 9;
int pinkpin = 10;
int outA = 2;
int outB = 4;
int outC = 7;
int outD = 8;
int outE = 11;
int outF = 12;
int outG = 13;
int red = 0;
int green = 0;
int blue = 0;
int amber = 0;
int pink = 0;
int valuecmd = 0;
void setup ()
{
Serial.begin(9600);
pinMode (redpin, OUTPUT);
pinMode (greenpin, OUTPUT);
pinMode (bluepin, OUTPUT);
pinMode (amberpin, OUTPUT);
pinMode (pinkpin, OUTPUT);
pinMode (outA, OUTPUT);
pinMode (outB, OUTPUT);
pinMode (outC, OUTPUT);
pinMode (outD, OUTPUT);
pinMode (outE, OUTPUT);
pinMode (outF, OUTPUT);
}
void loop ()
{
readserial ();
}
void readserial ()
{
while (Serial.available() > 0)
{
red = Serial.parseInt();
green = Serial.parseInt();
blue = Serial.parseInt();
amber = Serial.parseInt();
pink = Serial.parseInt();
valuecmd = Serial.parseInt();
if (Serial.read() == ';')
{
output128 ();
output64 ();
output32 ();
output16 ();
output8 ();
output4 ();
output2 ();
output1 ();
}
}
}
void output128 ()
{
if (valuecmd > 128)
{
valuecmd = valuecmd - 128;
digitalWrite (outA, HIGH);
}
else
{
digitalWrite (outA, LOW);
}
}
void output64 ()
{
if (valuecmd > 64)
{
valuecmd = valuecmd - 64;
digitalWrite (outB, HIGH);
}
else
{
digitalWrite (outB, LOW);
}
}
void output32 ()
{
if (valuecmd > 32)
{
valuecmd = valuecmd - 32;
digitalWrite (outC, HIGH);
}
else
{
digitalWrite (outC, LOW);
}
}
void output16 ()
{
if (valuecmd > 16)
{
valuecmd = valuecmd - 16;
digitalWrite (outD, HIGH);
}
else
{
digitalWrite (outD, LOW);
}
}
void output8 ()
{
if (valuecmd > 8)
{
valuecmd = valuecmd - 8;
digitalWrite (outE, HIGH);
}
else
{
digitalWrite (outE, LOW);
}
}
void output4 ()
{
if (valuecmd > 4)
{
valuecmd = valuecmd - 4;
digitalWrite (outF, HIGH);
}
else
{
digitalWrite (outF, LOW);
}
}
void output2 ()
{
if (valuecmd > 2)
{
valuecmd = valuecmd - 2;
digitalWrite (outF, HIGH);
}
else
{
digitalWrite (outF, LOW);
}
}
void output1 ()
{
if (valuecmd > 0)
{
red = red / 4;
red = constrain (red, 0, 255);
analogWrite (redpin, red);
green = green / 4;
green = constrain (green, 0, 255);
analogWrite (greenpin, green);
blue = blue / 4;
blue = constrain (blue, 0, 255);
analogWrite (bluepin, blue);
amber = amber / 4;
amber = constrain (amber, 0, 255);
analogWrite (amberpin, amber);
pink = pink / 4;
pink = constrain (pink, 0, 255);
analogWrite (pinkpin, pink);
}
else
{
analogWrite (redpin, 0);
analogWrite (greenpin, 0);
analogWrite (bluepin, 0);
analogWrite (amberpin, 0);
analogWrite (pinkpin, 0);
}
}
and here is the transmitter code
int bcd1 = 2;
int bcd2 = 3;
int bcd4 = 4;
int bcd8 = 5;
int analogpinred = 0;
int analogpingreen = 1;
int analogpinblue = 2;
int analogpinamber = 3;
int analogpinpink = 4;
int red = 0;
int green = 0;
int blue = 0;
int amber = 0;
int pink = 0;
int valuecmd = 0;
void setup ()
{
Serial.begin(9600);
pinMode (bcd1, INPUT);
digitalWrite (bcd1, HIGH);
pinMode (bcd2, INPUT);
digitalWrite (bcd2, HIGH);
pinMode (bcd4, INPUT);
digitalWrite (bcd4, HIGH);
pinMode (bcd8, INPUT);
digitalWrite (bcd8, HIGH);
}
void loop ()
{
readanalog ();
buildbcd ();
serialoutput ();
}
void readanalog ()
{
red = analogRead(analogpinred);
green = analogRead(analogpingreen);
blue = analogRead(analogpinblue);
amber = analogRead(analogpinamber);
pink = analogRead(analogpinpink);
}
void buildbcd ()
{
valuecmd = 0;
if (digitalRead(bcd1) == LOW)
{
valuecmd = valuecmd + 1;
}
if (digitalRead(bcd2) == LOW)
{
valuecmd = valuecmd + 2;
}
if (digitalRead(bcd4) == LOW)
{
valuecmd = valuecmd + 4;
}
if (digitalRead(bcd8) == LOW)
{
valuecmd = valuecmd + 8;
}
}
void serialoutput ()
{
Serial.print(red);
Serial.print(",");
Serial.print(green);
Serial.print(",");
Serial.print(blue);
Serial.print(",");
Serial.print(amber);
Serial.print(",");
Serial.print(pink);
Serial.print(",");
Serial.print(valuecmd);
Serial.print(";");
delay (300);
}
Notice the delay 300. It I decrease it to 200 the receiver no longer works.
For the uninformed, 9600 baud using FSK for data transmission is the limit at 900 Mhz since you must modulate the carrier wave. Going up to 2.4 Ghz radios will not work because while the data rate can go up considerably, the range falls apart becasue 2.4 Ghz will not penetrate more than 1 wall reliably. I need to transmit from inside the house to receivers in the yard ( several receivers actually ).
I am going to go to the single long integer. I will use 5 groups of 4 bits to run 5 outputs in PWM which will give 16 steps of dimming to those outputs. Then I will use 7 more bits to control the remaining outputs as on and off only.
I am going to try the example at the Arduino web site http://arduino.cc/en/Tutorial/Dimmer
Do you all hate that example also.
Moderator edit:
[code] [/code] tags added.