const int rLedPin = 6; //引脚R
const int gLedPin = 5; //引脚G
const int bLedPin = 3; //引脚B
 
int ledR  = 0; //R Led 亮度
int ledG  = 0; //G Led 亮度
int ledB  = 0; //B Led 亮度
 
void setup() {
  pinMode(rLedPin, OUTPUT);
  pinMode(gLedPin, OUTPUT);
  pinMode(bLedPin, OUTPUT);
  Serial.begin(9600);
  Serial.println("请输入RGB的值,例如r128g100b20。");
}
 
void loop() {
  
  if (Serial.available()>0) {  //如果返回串口缓冲区中当前剩余的字符个数大于0
    char serialCmdChar = Serial.read();  //读取字符作为字符型变量serialCmdChar
    serialCmd(serialCmdChar);       //执行函数
  }
  delay(50);  

}
 
void serialCmd(char serialCmdChar) {  //r128g100b20
  switch (serialCmdChar){
    case 'r':
      ledR = Serial.parseInt(); //解析字符串,返回整数
      break;
 
    case 'g':
      ledG = Serial.parseInt();
      break;
 
    case 'b':
      ledB = Serial.parseInt();
      break;    
 
    default:
      ledR = 0;
      ledG = 0;
      ledB = 0;
  }
 
  analogWrite(rLedPin, ledR);
  delay(100);
  analogWrite(gLedPin, ledG);
  delay(100);
  analogWrite(bLedPin, ledB); 
  delay(100); 
  
  Serial.print ("红灯亮度 = ");
  Serial.println (ledR);
  Serial.print ("绿灯亮度 = ");
  Serial.println (ledG);
  Serial.print ("蓝灯亮度 = ");
  Serial.println (ledB);
  Serial.println ("-----分割线-----");      
}
$abcdeabcde151015202530fghijfghij