【Follow me第二季第2期】--任务汇总
本帖最后由 upc_arm 于 2024-11-2 11:14 编辑## 感谢
感谢论坛,通过这次活动,让我对Arduino UNO R4 WiFi的使用有了深入全面的了解,为后面的开发打开基础。
## 我的任务汇总
### 第一部分:演示视频
所有任务的视频如下:
cfd26ae22e86ac69b3f61feeb830bb97<br/>
### 第二部分:任务实现详情
### 任务简介
总共3个任务,分别是:
- (1) 入门程序Hello World
- (2) DAC生成波形图,MPAMP放大后ADC采集,再在点阵和串口显示波形
- (3) 用温湿度传感器采集数据,通过MQTT协议,在HA和MQTT工具上显示数据
### 物料清单
- (1) Arduino UNO R4 WiFi
- (2) SHT40温湿度传感器扩展板
- (3) Qwiic连接线
### 实物图
### 设计思路
3个任务设计思路基本类似:第一步设置各种外设;第二步初始化全局变量、WiFI、外设等;第三步大循环,读取温湿度传感器数据,处理,显示,通过MQTT发送到远端。
### 软件流程图
程序设计流程图如下图所示:
### 任务介绍
任务1
主要代码片段
```c
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH);// turn the LED on (HIGH is the voltage level)
Serial.println("LED ON\r\n");
delay(3000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
Serial.print("LED OFF\r\n");
delay(1000); // wait for a second
}
```
分帖链接:
[任务1帖子链接](https://bbs.eeworld.com.cn/thread-1295359-1-1.html "任务1帖子链接")
任务2
主要代码片段
```c
// Create an instance of the analogWave class, using the DAC pin
analogWave wave(DAC);
// LEDMatrix
ArduinoLEDMatrix matrix;
byte frame = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
// These constants won't change. They're used to give names to the pins used:
const int analogInPin = A0;// Analog input pin that the potentiometer is attached to
const int analogOutPin = 9;// Analog output pin that the LED is attached to
int freq = 1;
int time_idx = 0;
int sensorValue = 0;// value read from the pot
int outputValue = 0;// value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(19200);
//change to 14-bit resolution
analogReadResolution(10);
// Generate a sine wave with the initial frequency
wave.sine(freq);
wave.amplitude(0.5);
// LED
matrix.begin();
if(false)
{
// activate OPAMP, default channel 0
if (!OPAMP.begin(OPAMP_SPEED_HIGHSPEED)) {
Serial.println("Failed to start OPAMP!");
}
bool const isRunning = OPAMP.isRunning(0);
if (isRunning) {
Serial.println("OPAMP running on channel 0!");
} else {
Serial.println("OPAMP channel 0 is not running!");
}
}
}
void set_matrix(int idx, int num)
{
for(int i=0; i<8; i++)
{
for(int j=0;j<11;j++)
{frame = frame;}
}
num = num / 16;
for(int i=0; i<num; i++)
{frame = 1;}
}
void loop()
{
int reading = analogRead(A3);
//int serial_num = reading / 64;
int serial_num = map(reading, 0, 1023, 0, 255);
Serial.println(String(serial_num));
if(time_idx>=12)
{time_idx=0;}
//Serial.println("time_idx");
//Serial.println(String(time_idx));
//Serial.println("serial_num");
//Serial.println(String(serial_num));
set_matrix(10, serial_num);
matrix.renderBitmap(frame, 8, 12);
delay(50);
time_idx = time_idx + 1;
}
```
分帖链接:
[任务2帖子链接](https://bbs.eeworld.com.cn/thread-1295405-1-1.html "任务2帖子链接")
任务3
主要代码片段
```c
Adafruit_SHT4x sht4 = Adafruit_SHT4x();
//arduino_secrets.h header file
#define SECRET_SSID "yournetwork"
#define SECRET_PASS "yourpassword"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key index number (needed only for WEP)
int led =LED_BUILTIN;
int status = WL_IDLE_STATUS;
WiFiServer server(80);
void setup() {
Serial.begin(9600);
while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens
Serial.println("Adafruit SHT4x test");
if (! sht4.begin()) {
Serial.println("Couldn't find SHT4x");
while (1) delay(1);
}
Serial.println("Found SHT4x sensor");
Serial.print("Serial number 0x");
Serial.println(sht4.readSerial(), HEX);
// You can have 3 different precisions, higher precision takes longer
sht4.setPrecision(SHT4X_HIGH_PRECISION);
switch (sht4.getPrecision()) {
case SHT4X_HIGH_PRECISION:
Serial.println("High precision");
break;
case SHT4X_MED_PRECISION:
Serial.println("Med precision");
break;
case SHT4X_LOW_PRECISION:
Serial.println("Low precision");
break;
}
// You can have 6 different heater settings
// higher heat and longer times uses more power
// and reads will take longer too!
sht4.setHeater(SHT4X_NO_HEATER);
switch (sht4.getHeater()) {
case SHT4X_NO_HEATER:
Serial.println("No heater");
break;
case SHT4X_HIGH_HEATER_1S:
Serial.println("High heat for 1 second");
break;
case SHT4X_HIGH_HEATER_100MS:
Serial.println("High heat for 0.1 second");
break;
case SHT4X_MED_HEATER_1S:
Serial.println("Medium heat for 1 second");
break;
case SHT4X_MED_HEATER_100MS:
Serial.println("Medium heat for 0.1 second");
break;
case SHT4X_LOW_HEATER_1S:
Serial.println("Low heat for 1 second");
break;
case SHT4X_LOW_HEATER_100MS:
Serial.println("Low heat for 0.1 second");
break;
}
}
void loop() {
sensors_event_t humidity, temp;
uint32_t timestamp = millis();
sht4.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
timestamp = millis() - timestamp;
Serial.print("Temperature: "); Serial.print(temp.temperature); Serial.println(" degrees C");
Serial.print("Humidity: "); Serial.print(humidity.relative_humidity); Serial.println("% rH");
Serial.print("Read duration (ms): ");
Serial.println(timestamp);
delay(1000);
}
void printWiFiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print where to go in a browser:
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
}
```
分帖链接:
[任务3帖子链接](https://bbs.eeworld.com.cn/thread-1295433-1-1.html "任务3帖子链接")
### 本次活动心得体会
通过这次活动,让我对Arduino UNO R4 WiFi的ADC、DAC、串口、点阵、WIFI等模块的使用有了深入的学习,后面开发的话,可以直接基于这些项目进行。最后感谢论坛组织的这次活动!
### 第三部分:可以编译下载的代码:
页:
[1]