Simple test program for KY-009 and KY-016

I've just performed a simple program to run with a simple RGB-LED to lab with it.

Enjoy!

The Code:

//CC MvG 2019

int redpin = 9; //select the pin for the red LED
int bluepin =10; // select the pin for the blue LED
int greenpin = 11;// select the pin for the green LED

byte red = 0;
byte green = 0;
byte blue = 0;
byte R0 = 0;
byte R1 = 0;
byte R2 = 0;
byte R3 = 0;
byte G1 = 0;
byte G2 = 0;
byte G3 = 0;
byte L1 = 0;
byte L2 = 0;
byte L3 = 0;

void setup() {
pinMode(redpin, OUTPUT);
pinMode(bluepin, OUTPUT);
pinMode(greenpin, OUTPUT);
Serial.begin(9600);

delay(10);
Serial.println("Write digits between 000-255 for the specific colour intensity!!!!");
Serial.println("Use a terminal program like the Serialmonitor in the IDE. Take away all ending sending characters like CR+LF.");
Serial.println("Just transfer the string of numbers! For example 127. [Without CR+LF in the end]!");

}

void loop()
{
{// send data only when you receive data:

Serial.println(" ");
Serial.println("Red: ");

while (Serial.available() <= 0)
{
delay(100);
}

if (Serial.available() > 0) { // read the incoming bytes:

R1 = Serial.read();
R2 = Serial.read();
R3 = Serial.read();
}

R1 = R1-48;
Serial.print("R1 = ");
Serial.println(R1);
R2 = R2-48;
Serial.print("R2 = ");
Serial.println(R2);
R3 = R3-48;
Serial.print("R3 = ");
Serial.println(R3);

red = R1100+R210+R3;

Serial.print("RED = ");

Serial.println(red);

}

{// send data only when you receive data:
Serial.println("Green: ");

while (Serial.available() <= 0)
{
delay(100);
}

if (Serial.available() > 0) { // read the incoming bytes:

G1 = Serial.read();
G2 = Serial.read();
G3 = Serial.read();
}

G1 = G1-48;
Serial.print("G1 = ");
Serial.println(G1);
G2 = G2-48;
Serial.print("G2 = ");
Serial.println(G2);
G3 = G3-48;
Serial.print("G3 = ");
Serial.println(G3);
Serial.print("GREEN = ");

green = G1100+G210+G3;

Serial.println(green);

}

{// send data only when you receive data:
Serial.println("Blue: ");

while (Serial.available() <= 0)
{
delay(100);
}

if (Serial.available() > 0) { // read the incoming bytes:

L1 = Serial.read();
L2 = Serial.read();
L3 = Serial.read();
}

L1 = L1-48;
Serial.print("L1 = ");
Serial.println(L1);
L2 = L2-48;
Serial.print("L2 = ");
Serial.println(L2);
L3 = L3-48;
Serial.print("L3 = ");
Serial.println(L3);
Serial.print("BLUE = ");

blue = L1100+L210+L3;

Serial.println(blue);

}

analogWrite(redpin, red); //set PWM value for red
analogWrite(bluepin, blue); //set PWM value for blue
analogWrite(greenpin, green); //set PWM value for green

delay(100);
}

I'm not enjoying it because you're breaking forum rules!

Yes, I was tempted to post the block explanation of how to post code but - I figure it should be easy enough to find. :roll_eyes:

if (Serial.available() > 0) { // read the incoming bytes:
 
    G1 = Serial.read();
    G2 = Serial.read();
    G3 = Serial.read();
  }

So if you have one or more bytes in the serial buffer then read three bytes from it. See any problems with that?