我從串口監視器發送 數字123,arduino得到的是1,2,3,獨立的三個數字 。
如果需要發送一個字符串怎么發送?arduino又要怎樣接收字符串?
截圖:
代碼:
/*
verify the function and serial port.
*/
int isodd(int a )
{
if (a % 2 == 0)
return 1;
else
return 0;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available())
{
int a = Serial.read() - '0';
if (isodd(a))
{
digitalWrite(13, HIGH);
Serial.print(a);
Serial.println(" 是偶數");
}
else
{
Serial.print(a);
Serial.println(" 是奇數");
digitalWrite(13, LOW);
}
}
}
|