Pages: [1]   Go Down
Author Topic: LED RGB from Serial using PWM  (Read 882 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I'm new in this forum and in Arduino World. I try to change RGB color with the function analogWrite.
My purpose is to send an array like [X,Y,Z] where {X,Y,Z}={0-255,0-255,0-255} to set the different color in real time with serial port communication using Bluetooth.
I tried to set the intensity color to each color and it's OK, but when I add another color it isn't work smiley-sad
Below I attach my code for only one led...someone can help me?

    int ledB=9; 
    int VarByte;
    char VarText;
    void setup(){
        pinMode(ledB,OUTPUT);
      Serial.begin(115200);
      }

    void loop(){
      if(Serial.available()>0){   
        VarByte= Serial.read();
        VarTesto=char(VarByte);
        analogWrite(ledB, VarText);
      }
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8699
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So you are sending three bytes to go to Red, Green, and Blue PWM channels?

Code:
const  int ledRpin=3;  // Pin number for Red PWM
const  int ledGpin=5;  // Pin number for Green PWM
const  int ledBpin=9;  // Pin number for Blue PWM

void setup()
    {
    pinMode(ledRpin,OUTPUT);
    pinMode(ledGpin,OUTPUT);
    pinMode(ledBpin,OUTPUT);
    Serial.begin(115200);
    }

void loop()
   {
   if (Serial.available() >= 3)
      {
      analogWrite(ledRpin, Serial.read());
      analogWrite(ledGpin, Serial.read());
      analogWrite(ledBpin, Serial.read());
      }
   }

You will have a problem if you EVER miss a byte because you have no indication of start and end of groups of three.  Because the three data bytes can be any value it will be hard to add framing without splitting the data values across multiple bytes an putting them back together when received.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8699
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Arrays are not referenced by magic.  You have to say which element of the array you are using!

Code:
   int led[]={9,10,11}; 
   int inByte[3];
       
   void setup(){
     for(int i=0;i<2;i++){           ////  Only two if the three???
        pinMode(led,OUTPUT);     ////  not led[i] ????
        Serial.begin(115200);
       }
   }
  void loop(){
     if(Serial.available()>0){           /// testing for >0 only allows you to read 1 byte safely.  If you read more you might get -1 (no data in buffer)
        for(int i=0;i<3;i++){
             inByte = Serial.read();      ///  not inByte[i] ???
           analogWrite(led,inByte);    /// not led[i] or inByte[i] ???
         }
       }
    }
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25802
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if(Serial.available()>0){
        for(int i=0;i<3;i++){
             inByte = Serial.read();
           analogWrite(led,inByte);
}
And if Serial.available () returns 1?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8699
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yeah, you're right I wrote it in a hurry, but I assure you that the Firmware was right and....it didn't work

So what does the code look like now that you have fixed it and it still doesn't work?  Perhaps there is a mistake that you don't recognize.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8699
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Searching I found this library: http://arduino.cc/playground/Code/Messenger.

According to you, can be used for this purpose?? It seems interesting...

Yes, just change the callback function to:
Code:
void messageReady() {
    analogWrite(9, message.readInt());
    analogWrite(10, message.readInt());
    analogWrite(11, message.readInt());
}

Your sending program will have to send values in ascii decimal separated by spaces and ending with a CR:

"255 0 0\r" = Pin 9 on full, pins 10 and 11 off.
"128 128 255\r" = Pins 9 and 10 at half power, Pin 11 on full.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Pages: [1]   Go Up
Jump to: