Here are the files to use a Flash front end/gui to send 3 x PWM values to the Arduino to control an RGB led connected to it..
values sent are in the following format:
*(using a starting and ending character)
*(splitting data string into name token and value tokens)
*(doing a string compare after the 'split')
<r=xxx>
<g=xxx>
<b=xxx>
example:<r=100><g=5><b=55>
Is how the data/string is sent form Flash to PHP, and hence to the COM PORT.
Wiring diagram for RGB:
Arduino Code:
#define SOP '<'
#define EOP '>'
bool started = false;
bool ended = false;char inData[5];
byte index;int redLed = 9; // pwm test pin
int redValue = 0;
int blueLed = 10; // pwm test pin
int blueValue = 0;
int greenLed = 11; // pwm test pin
int greenValue = 0;void setup(){
Serial.begin(9600);
}void loop(){
// Read all serial data available, as fast as possible
while(Serial.available() > 0){
char inChar = Serial.read();
//Serial.println(inChar);
if(inChar == SOP){
index = 0;
inData[index] = '\0';
started = true;
ended = false;
}
else if(inChar == EOP){
ended = true;
break;
}
else{
if(index < 5){
inData[index] = inChar;
index++;
inData[index] = '\0';
}
}
}// Packet data done...parse/evaluate data
if(started && ended){//putstring("VALUE TOKEN: ");
Serial.println(inData);
char *nameToken = strtok(inData, "=");
if(nameToken){
//check if red data coming over
if(strcmp(nameToken, "r") == 0){
char *valueToken = strtok(NULL, "\0");
if(valueToken){
//putstring("VALUE TOKEN: ");
//Serial.println(valueToken);
redValue = atoi(valueToken);
analogWrite(redLed, (255 - redValue));
}
else{
//putstring("DEFAULT VALUE USED: ");
//if param is missing, default to 0
redValue = 0;
analogWrite(redLed, (255 - redValue));
}
}
//check if green data coming over
if(strcmp(nameToken, "g") == 0){
char *valueToken = strtok(NULL, "\0");
if(valueToken){
//putstring("VALUE TOKEN: ");
//Serial.println(valueToken);
greenValue = atoi(valueToken);
analogWrite(greenLed, (255 - greenValue));
}
else{
//putstring("DEFAULT VALUE USED: ");
//if param is missing, default to 0
greenValue = 0;
analogWrite(greenLed, (255 - greenValue));
}
}
//check if blue data coming over
if(strcmp(nameToken, "b") == 0){
char *valueToken = strtok(NULL, "\0");
if(valueToken){
//putstring("VALUE TOKEN: ");
//Serial.println(valueToken);
blueValue = atoi(valueToken);
analogWrite(blueLed, (255 - blueValue));
}
else{
//putstring("DEFAULT VALUE USED: ");
//if param is missing, default to 0
blueValue = 0;
analogWrite(blueLed, (255 - blueValue));
}
}
}// Reset for the next packet
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
}
PHP script used to forward data to COM PORT of choice:
<?PHP
$pwmValue=$_POST["pwmValue"];
exec("mode COM4 BAUD=9600 PARITY=N data=8 stop=1 xon=off");
$fp = fopen("com4", "w");
if (!$fp) {
echo "Not open";
}else {
fwrite($fp, $pwmValue);
fclose($fp);
}
?>
ActionScript code: (w/.swf & .fla)
//set stepper boundaries
redStepper.minimum = 0;
redStepper.maximum = 255;
blueStepper.minimum = 0;
blueStepper.maximum = 255;
greenStepper.minimum = 0;
greenStepper.maximum = 255;
//create load vars instance
var pwmData:LoadVars = new LoadVars();
sendBtn_mc.onRelease = function(){
//pwmData.pwmValue = "<"+redStepper.value+">";
//pwmData.pwmValue = "<r"+redStepper.value+">"+"<g"+greenStepper.value+">"+"<b"+blueStepper.value+">";
//pwmData.pwmValue = "<r-"+redStepper.value+"r>"+"<g-"+greenStepper.value+"g"+"<"+blueStepper.value+">";
pwmData.pwmValue = "<r="+redStepper.value+">"+"<g="+greenStepper.value+">"+"<b="+blueStepper.value+">";
trace("DATA CHECK: "+pwmData.pwmValue);
//pwmData.sendAndLoad("flash_to_phpSerial.php", pwmData, "POST");
pwmData.send("flash_to_phpSerial.php", "_blank", "POST");
}
Attached is all files you would need to test this demo out:
- phpSerial_pwmRGBv2.pde (goes in your Arduino sketchbook folder.... upload/write sketch to your Arduino)
--These files all go into your 'www' directory in your Wamp Server installation (C:\wamp\www)
- flash_to_phpSerial.php - this is he PHP script that handles the data passing form Flash to the Serial Com Port
- flash_to_phpSerial_rgb.html
- flash_to_phpSerial_rgb.swf
- AC_RunActiveContent.js
- flash_to_phpSerial_rgb.fla - Flash front end source file
Let me know if everything works for you...or if there are better ways to handle/do these things..etc.
flash_php_arduino_rgb.zip (137 KB)