根据板卡原理RGB三色LED对应引脚:
int LEDR=12、int LEDG=14、int LEDB=13;
设置串口波特率为115200
Serial.begin(115200);
源代码如下所示:
/*
名称:串口控制RGB亮灭实验
功能:通过串口输入R、G、B三个字母来点亮对应的LED灯,关闭其他另外两个LED灯。
板卡:LYU-IOT-ESP12F
作者:gflytu@163.com
时间:2023.1.18
*/
int inByte = 0; // incoming serial byte
int LEDR=12;
int LEDG=14;
int LEDB=13;
void setup() {
// start serial port at 9600 bps:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
pinMode(LEDG, OUTPUT);
pinMode(LEDR, OUTPUT);
pinMode(LEDB, OUTPUT);
pinMode(2, OUTPUT); // digital sensor is on digital pin 2
establishContact(); // send a byte to establish contact until receiver responds
}
void loop() {
digitalWrite(LED_BUILTIN, LOW);
// if we get a valid byte, read analog ins:
if (Serial.available() > 0) {
// get incoming byte:
inByte = Serial.read();
if(inByte=='G')
{
Serial.print("\r\n点亮绿灯\r\n");
digitalWrite(LEDG,HIGH);
digitalWrite(LEDR,LOW);
digitalWrite(LEDB,LOW);delay(10);
}
if(inByte=='R')
{
Serial.print("\r\n点亮红灯\r\n");
digitalWrite(LEDG,LOW);
digitalWrite(LEDR,HIGH);
digitalWrite(LEDB,LOW);delay(10);
}
if(inByte=='B')
{
Serial.print("\r\n点亮蓝灯\r\n");
digitalWrite(LEDG,LOW);
digitalWrite(LEDR,LOW);
digitalWrite(LEDB,HIGH);delay(10);
}
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.print("welcome to Arduino\r\n");
delay(300);
}
}
操作过程串口输出下图所示: