Hello everybody, i made this program for my arduino that basically controls the color of my common cathode RGB led.
I am sending color values (0-255) from my android phone to PWM pins, no hard stuff.
This is an example of command from android: 255,0,255
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0,115);
int port=4545;
int redPin = 11;
int greenPin = 10;
int bluePin = 9;
EthernetServer server(port);
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac, ip);
server.begin();
Serial.println("Server started");
pinMode(redPin,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(bluePin,OUTPUT);
}
void loop()
{
EthernetClient client = server.available();
if (client) {
String command = "";
while(client.connected()) {
if (client.available()) {
char c = client.read();
command+=c;
if (c=='\n') {
Serial.println("Command: "+command); //see the command received
int commaPosition = command.indexOf(',');
String r = command.substring(0,commaPosition);
Serial.println("Rosu: "+r); //debug stuff
analogWrite(redPin,convertToInt(r));
String command2 = command.substring(commaPosition+1, command.length());
Serial.println("Ultimele2: "+command2); //debug stuff
int commaPosition2 = command2.indexOf(',');
String v = command2.substring(0,commaPosition2);
Serial.println("Verde: "+v); //debug stuff
analogWrite(greenPin,convertToInt(v));
String a = command2.substring(commaPosition2+1,command2.length());
Serial.println("Albastru: "+a); //debug stuff
analogWrite(bluePin,convertToInt(a));
command="";
}
}
}
delay(1);
// client.stop(); i will enable this when code is fully functional
}
}
int convertToInt(String value){
char buf[value.length()];
value.toCharArray(buf,value.length());
return atoi(buf);
}
What i did, at least this is what i think i did, was separate the values between comma and assign them to corresponding pins.
My problem is that when i receive a command, my strings (r,g,b) are correct but the LED color is incorrect.
If there is another simpler method to split that command, i am open to every suggestion.
I do something very similar, except my LED is 5 RGB LEDs on a TLC59116. This is my serial reading routine - it expects a string formatted like: "0:128,43,219\r" (\r is character 13 - carriage return, and the 0 is the LED number):
static char inputData[16];
static uint8_t ipos = 0;
while (Serial.available()) {
char inch = Serial.read();
if (inch == '\r') {
if (ipos > 0) {
int chan, r, g, b;
if (sscanf(inputData, "%d:%d,%d,%d", &chan, &r, &g, &b) == 4) {
switch (chan) {
case 0: topLeft.fadeTo(r, g, b); break;
case 1: topMiddle.fadeTo(r, g, b); break;
case 2: topRight.fadeTo(r, g, b); break;
case 3: bottomLeft.fadeTo(r, g, b); break;
case 4: bottomRight.fadeTo(r, g, b); break;
}
}
ipos = 0;
}
} else {
inputData[ipos++] = inch;
inputData[ipos] = 0;
}
}
It works well.
Are you using a common anode or common cathode RGB LED?
Common anode, common pin goes to +5, then PWM 255 = LED off, 0 = LED full on.
Common cathode, common pin goes to Gnd, then PWM 255 full on, 0 = LED full off.
You just need to invert the values you are PWMing.
I specified that my LED id common cathode, my connections are good. I think is something wrong with my code :~
@majenko i see you are using address pointers, i will look into it.
Hello
Serial.println("Albastru: "+a); //debug stuff
analogWrite(bluePin,convertToInt(a));
You are checking the value of the string before the analogWrite(). Have you tried testing the result of convertToInt() before you analogWrite()? Like this ...
Serial.println("Albastru: "+a); //debug stuff
int result = convertToInt(a);
Serial.print("After conversion: ");
Serial.println(result);
analogWrite(bluePin,convertToInt(a));
Regards
Ray
Thanks for replying, i checked as you said, the values are correct after converting to int.
In what way is the colour incorrect? Is it the right kind of colour but the balance out? Or is it completely wrong - green when it should be blue, red when it should be cyan, etc.
- I upload sketch to arduino
- Open up Serial Monitor
- now LED is full bright (all red,green,blue are full bright )
- send a command for red color
- i see the correct values in Serial monitor but LED only dims brightness(looks like all r,g,b are on but with less brightness).
[ send a command for green color---i see the correct values in Serial monitor but LED is fade red color]
[ send a command for blue color---- i see the correct values in Serial monitor but LED is fade magneta color]
In all cases only the first color(command) works. If i send a second command i see it in Serial Monitor but LED doesn't change.
Edit: i also verified the LED with a simple analogWrite program and it works as it should. Also on each pin i am using 300ohm resistors
Later edit: i just fixed the problem, it seems that some of the PWM pins are used by the ethernet shield and can't be used in conjunction with the shield. Too bad that in the Ethernet W5100 description page isn't a Duemilanove section, so i can clearly see which pins are used by the shield.
Thread can be closed, thanks to all that replayed to me. Take care.