So I want to variably set the voltage going to one of RGB_LED's pins . The way I thought of doing it is to write the quantificator into the serial monitor with the first byte representing the variable equivalent to the pin. For example :
if (Serial.read() == /* first byte(character)*/ r)
{
float i = SerialRead(); /* but the bytes from 2nd space to the end of the variable*/
}
Simplified: If I type into the serial monitor : 'r1.5' , then the if loop recognizes the first character "r" as an valid condition, thus setting the voltage to Red pin at 50%, while ignoring the first character. I also want to display the value it has been set to, but that should be easy.
the whole program code - (It applies the voltage equivalent to the value the potentiometer is letting through on the corresponding RGB_LED pin) :
#define LED_R 3
#define LED_B 5
#define LED_G 6
//Serial.write legenda: A0 A1 A2 RED BLUE GREEN A0_F A1_F A2_F
void setup()
{
pinMode(LED_R, OUTPUT);
pinMode(LED_B, OUTPUT);
pinMode(LED_G, OUTPUT);
Serial.begin(9600);
}
void loop()
{
int RED = analogRead(A0)/ 4;
int BLUE = analogRead(A1)/4;
int GREEN = analogRead(A2)/4;
Serial.print(analogRead(A0));//vypisovanie surovych (vstup) hodnot z R_potenciometra
Serial.print(" ");
Serial.print(analogRead(A1));//vypisovanie surovych (vstup) hodnot z B_potenciometra
Serial.print(" ");
Serial.print(analogRead(A2));//vypisovanie surovych (vstup) hodnot z G_potenciometra
Serial.print(" ");
Serial.print(RED); //vypisovanie hodnot (vystup) z R_potenciometra
Serial.print(" ");
Serial.print(BLUE); //vypisovanie hodnot (vystup) z B_potenciometra
Serial.print(" ");
Serial.print(GREEN);//vypisovanie hodnot (vystup) z G_potenciometra
Serial.print(" ");
float A0_F = (((float)RED)/(1023))*(5); //prevod hodnoty analogoveho vystupu A0 na (LED-spracovatelnu) Voltaz A0_F (Final)
float A1_F = (((float)BLUE)/(1023))*(5); //prevod hodnoty analogoveho vystupu A1 na (LED-spracovatelnu) Voltaz A1_F (Final)
float A2_F = (((float)GREEN)/(1023))*(5);//prevod hodnoty analogoveho vystupu A1 na (LED-spracovatelnu) Voltaz A2_F (Final)
Serial.print(A0_F);//vypisovanie konecnych hodnot na LED_R
Serial.print(" ");
Serial.print(A1_F);//vypisovanie konecnych hodnot na LED_B
Serial.print(" ");
Serial.println(A2_F);//vypisovanie konecnych hodnot na LED_G
analogWrite(LED_R, A0_F);//pripisovanie hodnoty na LED_R
analogWrite(LED_B, A1_F);//pripisovanie hodnoty na LED_B
analogWrite(LED_G, A2_F);//pripisovanie hodnoty na LED_G
}
//este urob zosilnovac
Please don't do it for me, i just want an confirmation bias or even better : some advice and guidance. Meanwhile i'll keep looking for the answer. Thanks!
First time posting me, if something's wrong, then please notify me.
e1: The Serial.read() function is not implemented yet, I'm stuck at the sentence in the question. If I get an answer to that, I'll program the whole chunk after.
3 Answers 3
Here's some quick pseudocode for what you're looking to do:
byte message = Serial.read()
if message == 'r', set red LED as one to change, else if 'g' for green, else if 'b' for blue
message = Serial.read()
double Value = message
message = Serial.read(), if message != '.', Value*= 10, value += message //if you want to allow for values over 10
if message == '.', message = Serial.read, Value+= double(message/10) //repeat if you want more decimal places
A0_F/A1_F/A2_F *= Value
analogWrite(LED_R/LED_G/LED_B, A0_F/A1_F/A2_F)
You'll probably want to add other stuff, like max/min value checks and invalid input checks.
-
1Using
Serial.parseFloat()
may be easier than parsing the number manually.Edgar Bonet– Edgar Bonet02/17/2025 17:03:41Commented Feb 17 at 17:03
You seem to mix both the LED and the potentiometers on the same Arduino, in that case there is no use for the Serial at all. So if you want to do what I expect (have a local and remote control), you should have a loop where you
- Check for Serial and read it if available. I suggest reading a line (you can use a space as a separator, but in the end, it's not obvious for debugging) that would contain R999, B77, or G09, for example. No need for floats.
- Check for sensible variations (more than 50 for example) on your pots/analog inputs. When modified, change the level and Serial.print('R');Serial.println(RED) if you want to inform the other side.
As requested, I purposely don't include full program, up to you to make it ;-)
This answer does not provide a complete solution, but nudges you in some direction that can work.
Your code snippet is almost what you want. Just replace SerialRead()
with Serial.parseFloat()
. The documentation has the details. Please read it, you should always know what you use.
if (Serial.read() == /* first byte(character)*/ r)
{
float i = Serial.parseFloat(); /* but the bytes from 2nd space to the end of the variable*/
}
If you enter a line via the Serial Monitor, it will be sent as a complete line. Until you hit Enter, nothing is sent.
Serial
implements a stream. This means that characters read from the stream will not be available for following reads. So the call of Serial.read()
reads exactly one character, and the following Serial.parseFloat()
starts from the second character. It skips any non-number characters by default, but you might want to use the argument lookahead
to change this behavior.
Please be aware that the rest of the line after the float will be unread. You need to take care of that, for example read all characters including the end of the line.
Just another note: Since you want to differentiate between the LEDs via the first character, cache the first character in a variable to be able to use multiple if
s. Or consider to use switch
.
Serial.read()
in your code, so I do not understand your problem.Serial.read()
returns -1, if there's nothing to read yet. Returns a character code (e.g. 49 == '1' ), if there is.