Controlling Multi-Color LED with Serial Inputs

const int greenLEDPin = 9;
const int redLEDPin = 11;
const int blueLEDPin = 10;

String red = "";
String blue = "";
String green = "";

int redValue = 0;
int greenValue = 0;
int blueValue = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  pinMode(greenLEDPin, OUTPUT);
  pinMode(redLEDPin, OUTPUT);
  pinMode(blueLEDPin, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("Enter red");
  red = Serial.readString();
  while (Serial.available() == 0) {}
  blue = Serial.readString();
  Serial.println("Enter blue");
  while (Serial.available() == 0) {}
  Serial.println("Enter green");
  green = Serial.readString();
  while (Serial.available() == 0) {}
  Serial.println("R:" + red);
  Serial.println("B:" + blue);
  Serial.println("G:" + green);
  while (Serial.available() == 0) {}
  red.toInt();
  blue.toInt();
  green.toInt();
  analogWrite(redLEDPin, red);
  analogWrite(greenLEDPin, green);
  analogWrite(blueLEDPin, blue);
}

So I'm trying to make an LED light up with set red, blue, and green inputs into the serial monitor. I'm having two problems. First, when my inputs are printed back into the serial monitor, red is empty, whatever I put for red goes into blue, and whatever I put for blue goes into green. And Secondly, I'm not sure how to convert those Strings (my inputs for RGB) into Integers that I can then use as outputs for my LED. The current code gives me an error:

cannot convert 'String' to 'int' for argument '2' to 'void analogWrite(uint8_t, int)'

I'm pretty new to this, so please help nicely :slight_smile: Thanks.

const int greenLEDPin = 9;
const int redLEDPin = 11;
const int blueLEDPin = 10;

String red = "";
String blue = "";
String green = "";

int redValue = 0;
int greenValue = 0;
int blueValue = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  pinMode(greenLEDPin, OUTPUT);
  pinMode(redLEDPin, OUTPUT);
  pinMode(blueLEDPin, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("Enter red");
  red = Serial.readString();
  while (Serial.available() == 0) {}
  blue = Serial.readString();
  Serial.println("Enter blue");
  while (Serial.available() == 0) {}
  Serial.println("Enter green");
  green = Serial.readString();
  while (Serial.available() == 0) {}
  Serial.println("R:" + red);
  Serial.println("B:" + blue);
  Serial.println("G:" + green);
  while (Serial.available() == 0) {}
  red.toInt();
  blue.toInt();
  green.toInt();
  analogWrite(redLEDPin, red);
  analogWrite(greenLEDPin, green);
  analogWrite(blueLEDPin, blue);
  
}

So I'm trying to make an LED light up with set red, blue, and green inputs into the serial monitor. I'm having two problems. First, when my inputs are printed back into the serial monitor, red is empty, whatever I put for red goes into blue, and whatever I put for blue goes into green. And Secondly, I'm not sure how to convert those Strings (my inputs for RGB) into Integers that I can then use as outputs for my LED. The current code gives me an error:

 In function 'void loop()':

color_mixer:40:29: error: cannot convert 'String' to 'int' for argument '2' to 'void analogWrite(uint8_t, int)'

   analogWrite(redLEDPin, red);

                             ^

color_mixer:41:33: error: cannot convert 'String' to 'int' for argument '2' to 'void analogWrite(uint8_t, int)'

   analogWrite(greenLEDPin, green);

                                 ^

color_mixer:42:31: error: cannot convert 'String' to 'int' for argument '2' to 'void analogWrite(uint8_t, int)'

   analogWrite(blueLEDPin, blue);

                               ^

exit status 1

cannot convert 'String' to 'int' for argument '2' to 'void analogWrite(uint8_t, int)'

analogWrtite expects a number from 0 to 255 for the 2nd argument.
You need to enter some digits, which will come in as characters:
'1', '2', '3'.

Take those, subtract 48 to turn them each into a number

Combine them into a number:
incoming Number = (digit1 * 100) + (digit2 * 10) + digit3;

Then you will have number you can use with analogWrite.

Read through Robin2's excellent tutorial Serial Basics to see a better way to read in numbers, parse them and put them to use.

Try this sketch. See Text I/O for the Real World for more details.

// install SafeString V3 from Arduino Library manager
#include <SafeString.h>
#include <SafeStringReader.h>

createSafeStringReader(sfReader, 20, " ,\r\n");

const int greenLEDPin = 9;
const int redLEDPin = 11;
const int blueLEDPin = 10;

int redValue = 0;
int greenValue = 0;
int blueValue = 0;

int waitingFor = 0; // 0, red, 1, blue, 2 green

void setup() {
  Serial.begin(9600);
  for (int i = 10; i > 0; i--) {
    Serial.print(i); Serial.print(' ');
    delay(500);
  }
  Serial.println();  sfReader.connect(Serial);
  sfReader.echoOn();
  sfReader.setTimeout(100); // incase Monitor has No line Ending set
  pinMode(greenLEDPin, OUTPUT);
  pinMode(redLEDPin, OUTPUT);
  pinMode(blueLEDPin, OUTPUT);
  promptForInput(0); // prompt for red
}

void promptForInput(int waitingFor) {
  if (waitingFor == 0) {
    Serial.println(" Enter value for Red in the range 0 to 255");
  } else if (waitingFor == 1) {
    Serial.println(" Enter value for Blue in the range 0 to 255");
  } else {
    Serial.println(" Enter value for Green in the range 0 to 255");
  }
}

void setValue(int waitingFor, int value) {
  if (waitingFor == 0) {
    redValue = value;
    analogWrite(redLEDPin, redValue);
  } else if (waitingFor == 1) {
    blueValue = value;
    analogWrite(blueLEDPin, blueValue);
  } else {
    greenValue = value;
    analogWrite(greenLEDPin, greenValue);
  }
}

void getSettings() {
  if (sfReader.read()) {
    // see if the input is valid
    int setting = 0;
    if (sfReader.toInt(setting)) {
      // got one
      if ((setting < 0) || (setting > 255)) {
        Serial.println(" number out of range 0 to 255");
      } else {
        setValue(waitingFor, setting);
        waitingFor++;
        if (waitingFor > 2) {
          waitingFor = 0; // back to red
        }
        promptForInput(waitingFor);
      }
    } else {
      Serial.println(" not a valid integer");
    }
  }
}

void loop() {
  getSettings();
}

Put your while() loops before your readString() lines, not after.

redValue = red.toInt();
analogWrite (redLEDPin,  redValue);

Duplicate topics moved to here and merged

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a timeout from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.