N
N
Nikita Volkov2020-10-06 23:31:23
Arduino
Nikita Volkov, 2020-10-06 23:31:23

How to control RBG LED through port monitor on Arduino?

I want, for example, when you enter R 255 into the COM port, a signal goes to the pin and the red LED lights up, or I write G 150 and then B 200 and it turns out like a dark blue color, or for example I write R 0 , then G 0 , and B 0 and then the LED goes out.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2020-10-07
@fox_12

Here is a similar program once wrote. Does almost what you need.
The code is as simple and clear as possible.

int red_pin = 11;
int green_pin = 10;
int blue_pin = 9;
int common_pin = A0;
int incoming_data = 0;

void setup() {
  pinMode(red_pin, OUTPUT);
  pinMode(green_pin, OUTPUT);
  pinMode(blue_pin, OUTPUT);
  pinMode(common_pin, OUTPUT);
  digitalWrite(red_pin, HIGH);
  digitalWrite(green_pin, HIGH);
  digitalWrite(blue_pin, HIGH);
  digitalWrite(common_pin, HIGH);
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    incoming_data = Serial.read();
  };
 
  if ((incoming_data & B00000010) != 0) {
    digitalWrite(red_pin, LOW);
  } else {
    digitalWrite(red_pin, HIGH);
  };

  if ((incoming_data & B00000100) != 0) {
    digitalWrite(green_pin, LOW);
  } else {
    digitalWrite(green_pin, HIGH);
  };

  if ((incoming_data & B00001000) != 0) {
    digitalWrite(blue_pin, LOW);
  } else {
    digitalWrite(blue_pin, HIGH);
  };

  if ((incoming_data & B00000001) != 0) {
    digitalWrite(common_pin, LOW);
  } else {
    digitalWrite(common_pin, HIGH);
  }
}

The only difference is that the channels to be lit are determined by the least significant bits of the input number.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question