forked from coloz/Arduino-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.1.6-SerialRGBLED.ino
65 lines (62 loc) · 1.47 KB
/
5.1.6-SerialRGBLED.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
OpenJumper Example
串口RGB LED调光
奈何col 2013.2.20
www.openjumper.com
*/
int i; //保存PWM需要输出的值
String inString = ""; // 输入的字符串
char LED = ' '; // 用于判断指定LED颜色对应的引脚
boolean stringComplete = false; // 用于判断数据是否读取完成
void setup()
{
//初始化串口
Serial.begin(9600);
}
void loop()
{
if (stringComplete)
{
if (LED == 'A')
{
analogWrite(9, i);
}
else if (LED == 'B')
{
analogWrite(10, i);
}
else if (LED == 'C')
{
analogWrite(11, i);
}
// 清空数据,为下一次读取做准备
stringComplete = false;
inString = "";
LED = ' ';
}
}
//使用串口事件
// 读取并分离字母和数字
void serialEvent()
{
while (Serial.available())
{
// 读取新的字符
char inChar = Serial.read();
//根据输入数据分类
// 如果是数字,则存储到变量inString中
// 如果是英文字符,则存储到变量LED中
// 如果是结束符“\n”,则结束读取,并将inString转换为int
if (isDigit(inChar))
{
inString += inChar;
}
else if (inChar == '\n')
{
stringComplete = true;
i = inString.toInt();
}
else
LED = inChar;
}
}