其他分享
首页 > 其他分享> > arduino pwm按键控制灯的明暗

arduino pwm按键控制灯的明暗

作者:互联网

int pwm = 128;
void setup() {
  Serial.begin(9600);
  Serial.println("串口打开!!");
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(6, OUTPUT);
  
}

void loop() {
  boolean a;
  boolean b;
  a = digitalRead(2);
  b = digitalRead(3);
  Serial.println(pwm);
  if (a == 0 && pwm > 0 ){
    analogWrite(6, pwm);
    pwm--;
  }else if(b == 0 && pwm < 255){
    analogWrite(6, pwm);
    pwm++;
  }
  delay(20);
}

在这里插入图片描述

标签:arduino,pinMode,boolean,digitalRead,PULLUP,Serial,pwm,明暗
来源: https://blog.csdn.net/nanxiang11/article/details/118857074