Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

MengFanjun的博客

一、电路图

在这里插入图片描述

二、程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
boolean pushButton1;
boolean pushButton2;
int ledpin=9;
int brightness=128;
void setup() {
// put your setup code here, to run once:
pinMode(2,INPUT_PULLUP);
pinMode(8,INPUT_PULLUP);
pinMode(ledpin,OUTPUT);
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
pushButton1=digitalRead(2);//变暗按键
pushButton2=digitalRead(8);//变亮按键
if(!pushButton1 && brightness > 0)
{
brightness--;
}
if(!pushButton2 && brightness < 255)
{
brightness++;
}
analogWrite(ledpin,brightness);
Serial.println(brightness);
delay(10);

}

三、结果

在这里插入图片描述 按下按键,灯的亮度发生了变化

评论