Hi, thanks for paying attention. I'm using an Arduino UNO.
The situation, I need to program my arduino to control an RGB led by serial communication.
Sounds simple, but I need to make it able to control the intensity of each color. I'm able to do that using the analogWrite command and the PWM terminals. I already made something similar but using 3 potentiometers, one for each color value, obviously using map in each variable to change from 0,1023 to 0,255.
I don't understand a lot about serial communication so I wonder how can I do it in just one command with the 3 values together or by sending the 3 different values and let the arduino assign the outputs in the order they've were sent. I've been trying some methods but I'm unable to make it work, so I'll be very grateful if you tell me how to do it and why it works that way (because I want to learn).
I am going to assume that the Uno is on the receiving end of the serial link. I don't know what is on the sending end because you didn't tell us.
You are sending three values of 0 to 255 each. 0 to 255 requires eight bits, so you need 24 bits. A serial character is eight bits, so you cannot do this with a single character.
It is best (for synchronization purposes) that your command have a unique starting character (say, 'C') and a unique ending character (say, '#'). You can simplify this later as you learn more.
I suggest that you send the command one of two ways that you choose:
C#
where each is a single character representing a value of 0 to 255 for a color.
This is fast but risks confusion because could be a 'C' or a '#'.
C,,#
where each represents a number 0 or 1 or 2 or ... or 254 or 255.
This is a little slower (but not much in human terms) and does not risk
confusion with the starting character or the ending character, but you will
have to convert into a number (probably an int) that you can use
with your PWM.
There are other nifty things that you could do (such as add a checksum or an
error correcting code) but this is enough to get you started.
There are many other ways to do this, but this is one way that a beginner might
do it and succeed.
Some things to remember:
Characters arrive at your Uno much slower than the code on the Uno executes.
Many times, you will look for a character and do nothing because it hasn't arrived
yet.
You are likely to want to use the serial I/O pins (0 and 1) for downloading and
for debugging. This implies that you are going to use SoftwareSerial (or an
equivalent) and a couple of other pins.
String (capital S) can get you in trouble. Learn to use c strings (lower case s)
which are arrays of characters terminated with a 0 - and don't go off the end
of the array!
aarg:
Show us the most promising method you've tried.
I deleted and can't remember it.
vaj4088:
2) C,,#
where each represents a number 0 or 1 or 2 or ... or 254 or 255.
This is a little slower (but not much in human terms) and does not risk
confusion with the starting character or the ending character, but you will
have to convert into a number (probably an int) that you can use
with your PWM.
Ok, let's say that I choose that way because it sounds easier. Now for example I want to make orange, which in RGB is 255, 64, 0. That (I assume) means I got to send C<255>,<64>,<0>#. How do I write those values in the output pins?
The symbols <> are not to be taken literally. They mean "something". So a proper example would be:
C255,64,0#
atoi() converts numeric strings to integer values.
Could you give me an example?
I can't really understand what to put in between the braces from analogWrite().
And I don't know how to use atoi() also, I'm kinda noob with this as you can see.
The examples in Serial Input Basics are simple reliable ways to receive data. The 3rd example would be most reliable. It assumes data is sent in the format <val1, val2, val3> and the parse example illustrates how to separate the data and save it into suitable variables.