[Follow me Season 2 Episode 2] Smart Home - Smart Study Room - Getting Started Tasks
[Copy link]
This post was last edited by Fresh Mango on 2024-9-8 15:11
1. Task Requirements
Build the environment and start the first step Blink / Serial port print Hello EEWorld!
2. Arduino development environment construction
- Download Arduino IDE
- Unzip and extract the downloaded Arduino IDE to disk.
- Open the unzipped folder and double-click to run Arduino IDE.exe
The first time you open the Arduino IDE, it will look like this:
3. Code Implementation
/**
* FollowMe 2-2 任务1:
* 1. 搭建环境并开启第一步Blink
* 2. 串口打印 Hello EEWorld!
*/
#define MIN_VAL 0 // PWM分辨率最小值
#define MAX_VAL 255 // PWM分辨率最大值
uint32_t tick = 0; // loop 循环次数
uint32_t fadeValue = 0; // 呼吸灯PWM占空比
uint8_t fadeStep = 5; // 呼吸灯步长
bool isFadeIn = true; // 当前呼吸灯状态,true:表示由暗转亮,false:表示由亮转暗
void setup() {
// 初始化串口
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
delay(1000);
// 串口打印
Serial.println("Hello EEWorld!");
}
void loop(){
if(0 == (tick % 40)) { // 每隔1秒串口打印一次loop函数当前循环次数。loop中每次执行延时25毫秒,因此,loop每执行40次正好是1秒钟。
Serial.print("loop count: "); // 打印loop函数当前循环次数
Serial.println(tick);
}
// PWM输出驱动板载LED
analogWrite(LED_BUILTIN, fadeValue);
// 呼吸灯由暗转亮
if(isFadeIn) {
fadeValue += fadeStep;
if(MAX_VAL <= fadeValue) {
// 当呼吸灯PWM占空比大于等于 PWM 分辨率最大值时,翻转呼吸灯状态。
fadeValue = MAX_VAL;
isFadeIn = !isFadeIn;
}
} else { // 呼吸灯由亮转暗
fadeValue -= fadeStep;
if(MIN_VAL >= fadeValue) {
// 当呼吸灯PWM占空比小于等于 PWM 分辨率最小值时,翻转呼吸灯状态。
fadeValue = MIN_VAL;
isFadeIn = !isFadeIn;
}
}
delay(25); // 每30毫秒更新一次PWM占空比
tick ++;
}
4. Effect display
|