Arduino教程学习A15-零碎的Arduino串口控制代码
Ansifa 于 2015-6-29 15:21 编辑
串口控制的代码,有串口控制16/32个595级联,串口控制Arduino UNO 18个引脚开关,串口控制红外发送,串口控制315M/433M发送等。
ARDUINO 代码复制打印下载
/串口控制开关By Ansifa2013/6/15说明!必须看完!Arduino UNO总共18个脚全部用完发一串字符串即可单独控制所有引脚开关。H高电平,L低电平,-是保持原来电平状态。发送的时候,记得选择串口监视器里面的行尾增加:(换行和回车)测试:HHHHHHHHHHHHHHHHHHLLLLLLLLLLLLLLLLLL1111111111111111111-1-10111111111100000000000000000001010101010101010110101010101010101/int i, Pins[18] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, A0, A1, A2, A3, A4, A5};String inputString = "";boolean stringComplete = false;void setup(){ Serial.begin(57600); inputString.reserve(50); for (i = 0; i < 18; i++) pinMode(Pins[i], OUTPUT);}void loop(){ if(stringComplete == true) { for (i = 0; i < 18; i++) { if(inputString[i] == 'H' || inputString[i] == 'h' || inputString[i] == '1') { digitalWrite(Pins[i], HIGH); } if(inputString[i] == 'L' || inputString[i] == 'l' || inputString[i] == '0') { digitalWrite(Pins[i], LOW); } } inputString = ""; stringComplete = false; }}void serialEvent(){ while (Serial.available()) { char inChar = (char)Serial.read(); inputString += inChar; if (inChar == '\n' || inChar == '\r') { stringComplete = true; } }}
/*
串口控制开关
By Ansifa
2013/6/15
说明!必须看完!
Arduino UNO总共18个脚全部用完
发一串字符串即可单独控制所有引脚开关。
H高电平,L低电平,-是保持原来电平状态。
发送的时候,记得选择串口监视器里面的行尾增加:(换行和回车)
测试:
HHHHHHHHHHHHHHHHHH
LLLLLLLLLLLLLLLLLL
111111111111111111
1-1-1011111111110
000000000000000000
10101010101010101
10101010101010101
*/
int i, Pins[18] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, A0, A1, A2, A3, A4, A5};
String inputString = "";
boolean stringComplete = false;
void setup()
{
Serial.begin(57600);
inputString.reserve(50);
for (i = 0; i < 18; i++) pinMode(Pins[i], OUTPUT);
}
void loop()
{
if(stringComplete == true)
{
for (i = 0; i < 18; i++)
{
if(inputString[i] == 'H' || inputString[i] == 'h' || inputString[i] == '1')
{
digitalWrite(Pins[i], HIGH);
}
if(inputString[i] == 'L' || inputString[i] == 'l' || inputString[i] == '0')
{
digitalWrite(Pins[i], LOW);
}
}
inputString = "";
stringComplete = false;
}
}
void serialEvent()
{
while (Serial.available())
{
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n' || inChar == '\r')
{
stringComplete = true;
}
}
}
via - 极客工坊