Arduino 串口多操作通信示例1

8/10/2022 8:44:47 PM
752
0
#include <SPI.h>

int led = 2;
char led_recv_status = 0;
void setup() {
  Serial.begin(9600);

  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
}

void loop() {
 
  if (Serial.available() > 0)
  {
    led_recv_status  = Serial.read();
    if (led_recv_status  == 'o')
    {
      digitalWrite(led, HIGH); //led on
      Serial.println("led on");
    }
    else if (led_recv_status  == 'f')
    {
      digitalWrite(led, LOW); //led off
      Serial.println("led off");
    }

  }
}

全部评论



提问