Here is my rendition of your idea. I've not tested it but it does compile. I'm going to dig around and see if I can find an old ps2 keyboard and try it out wit my RGB led.
In this code you do your normal setups and the rest is in the loop.
The main loop accepts keyboard inputs and only handles 0-9, esc, and enter.
When you put in the values for RGB enter them in that order (R, G, B) ie... 2,5,5,2,5,5,2,5,5. As you enter the values they will display on the serial monitor starting with red then green and finally blue (RED:255 GREEN:255 BLUE:255). The way the logic is you have to put in 3 numbers for each color ie... 10 would be entered as 010 and 1 would be 001 and 0 would be 000.
If you make a mistake at any time simply hit escape and start over. if you hit enter before all the numbers for RGB are entered it will display a message accordingly. The serial monitor window will tell you what number you hit each time and what color it was assigned to.
Once all the numbers for the RGB values are entered hit enter and the LED should change to that color.
Code...
#include <PS2Keyboard.h>
#define DATA_PIN 4
PS2Keyboard keyboard;
int ledpinR = 9;
int ledpinG = 11;
int ledpinB = 10;
int RED[3] = {0, 0, 0};
int GREEN[3] = {0, 0, 0};
int BLUE[3] = {0, 0, 0};
int keysIn = 0;
void setup() {
keyboard.begin(DATA_PIN);
Serial.begin(9600);
Serial.println("Enter RGB values. ie... 255 255 255 <enter>");
delay(1000);
}
void loop() {
if(keyboard.available()) {
byte dat = keyboard.read();
byte val = dat - '0';
if(val >= 0 && val <= 9) {
captureKeyboardInput((int)val);
} else if(dat == PS2_KC_ENTER) {
writeToRgbLed();
} else if(dat == PS2_KC_ESC) {
reset();
}
}
}
void reset(){
for (int i = 0; i < 3; i ++) {
RED[i] = 0;
GREEN[i] = 0;
BLUE[i] = 0;
}
keysIn = 0;
Serial.println("Reset!");
Serial.println();
Serial.println();
}
void writeToRgbLed(){
if(keysIn >= 9){
int red = ((RED[0]*100)+(RED[1]*10)+RED[2]);
analogWrite(ledpinR, constrain(red, 0, 255));
int green = ((GREEN[0]*100)+(GREEN[1]*10)+GREEN[2]);
analogWrite(ledpinG, constrain(green, 0, 255));
int blue = ((BLUE[0]*100)+(BLUE[1]*10)+BLUE[2]);
analogWrite(ledpinB, constrain(blue, 0, 255));
Serial.println("Done.");
reset();
}
else{
Serial.println("Not enough numbers were entered.");
}
}
void captureKeyboardInput(int value){
switch (keysIn) {
case 0:
RED[0] = value;
Serial.print("RED:");
Serial.print(RED[0]);
keysIn ++;
break;
case 1:
RED[1] = value;
Serial.print(RED[1]);
keysIn ++;
break;
case 2:
RED[2] = value;
Serial.print(RED[2]);
keysIn ++;
break;
case 3:
GREEN[0] = value;
Serial.print(" GREEN:");
Serial.print(GREEN[0]);
keysIn ++;
break;
case 4:
GREEN[1] = value;
Serial.print(GREEN[1]);
keysIn ++;
break;
case 5:
GREEN[2] = value;
Serial.print(GREEN[2]);
keysIn ++;
break;
case 6:
BLUE[0] = value;
Serial.print(" BLUE:");
Serial.print(BLUE[0]);
keysIn ++;
break;
case 7:
BLUE[1] = value;
Serial.print(BLUE[1]);
keysIn ++;
break;
case 8:
BLUE[2] = value;
Serial.print(BLUE[2]);
Serial.println();
keysIn ++;
break;
}
}