int r=6;
int y=7;
int g=8;
char led;
void setup()
{
Serial.begin(9600);
pinMode(r,OUTPUT);
pinMode(y,OUTPUT);
pinMode(g,OUTPUT);
}
void loop()
{
if (Serial.available()>0)
{
char i='0';
delay(100);
int numChar=Serial.available();
if (numChar>10) numChar=10;
while (numChar!=0)
{
led=Serial.read();
if(led!=' '){
i=led;
}
numChar=numChar-1;
}
splitString(i);
Serial.flush();
}
}
void splitString(char hi)
{
Serial.print("Read is:");
Serial.println(hi);
if (hi=='R'||hi=='r'){
digitalWrite(r,HIGH);
digitalWrite(g,LOW);
digitalWrite(y,LOW);
}
else if(hi=='G'||hi=='g'){
digitalWrite(g,HIGH);
digitalWrite(r,LOW);
digitalWrite(y,LOW);
}
else if(hi=='Y'||hi=='y'){
digitalWrite(y,HIGH);
digitalWrite(g,LOW);
digitalWrite(r,LOW);
}
else {
Serial.println("you are stupid");
digitalWrite(y,LOW);
digitalWrite(g,LOW);
digitalWrite(r,LOW);
}
}
arduino过滤空格
最新推荐文章于 2023-08-10 10:13:38 发布