1_Arduino-ESP32_外部中断,串口,ADC

发布于 2022年 01月 17日 11:29

1_外部中断


#include<Arduino.h>
int pin = 5;
volatile int state = LOW;

void blink()
{
  state = !state;
}

void setup()
{
  pinMode(pin, OUTPUT);
  attachInterrupt(0, blink, FALLING);
}

void loop()
{
  digitalWrite(pin, state);
}

2_串口


#include<Arduino.h>
String  comchar;

void setup() {
  // put your setup code here, to run once:
 Serial.begin(115200); 
 Serial.println("请输入");
 while(Serial.read()>= 0){}//clear serialbuffer

}

void loop() {
  // put your main code here, to run repeatedly:
 while(Serial.available()>0){
  
    delay(100);
    comchar = Serial.readString();
    Serial.print("你输入的是");
    Serial.println(comchar);
     
    }
 
}

设置波特率

monitor_speed = 115200

3_ADC


#include<Arduino.h>
// Potentiometer is connected to GPIO 34 (Analog ADC1_CH6) 
const int potPin = 34;

// variable for storing the potentiometer value
int potValue = 0;

void setup() {
  Serial.begin(115200);
  delay(1000);
}

void loop() {
  // Reading potentiometer value
  potValue = analogRead(potPin);
  Serial.println(potValue);
  delay(500);
}

推荐文章