arduino esp32-c3两位数码管显示设置温度然后开始显示温度传感器读取到的当前温度
时间: 2024-05-15 22:20:08 浏览: 242
以下是一个示例代码,演示如何使用Arduino ESP32-C3控制两位数码管显示当前温度传感器读取到的温度值。
```c++
#include <Adafruit_NeoPixel.h>
// 定义数码管引脚
#define DIGIT1 0
#define DIGIT2 1
#define SEG_A 2
#define SEG_B 3
#define SEG_C 4
#define SEG_D 5
#define SEG_E 6
#define SEG_F 7
#define SEG_G 8
#define SEG_DP 9
// 定义温度传感器引脚
#define TEMPERATURE_PIN A0
// 定义温度显示变量和数码管颜色
float temperature = 0;
Adafruit_NeoPixel pixels(2, 10, NEO_GRB + NEO_KHZ800);
// 数码管显示函数
void displayTemp(float temp) {
int intTemp = int(temp * 10); // 乘以10是为了显示小数点后一位
int digit1 = intTemp / 10;
int digit2 = intTemp % 10;
digitalWrite(DIGIT1, LOW);
digitalWrite(DIGIT2, HIGH);
switch(digit2) {
case 0:
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_B, HIGH);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, HIGH);
digitalWrite(SEG_E, HIGH);
digitalWrite(SEG_F, HIGH);
digitalWrite(SEG_G, LOW);
break;
case 1:
digitalWrite(SEG_A, LOW);
digitalWrite(SEG_B, HIGH);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, LOW);
digitalWrite(SEG_E, LOW);
digitalWrite(SEG_F, LOW);
digitalWrite(SEG_G, LOW);
break;
case 2:
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_B, HIGH);
digitalWrite(SEG_C, LOW);
digitalWrite(SEG_D, HIGH);
digitalWrite(SEG_E, HIGH);
digitalWrite(SEG_F, LOW);
digitalWrite(SEG_G, HIGH);
break;
case 3:
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_B, HIGH);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, HIGH);
digitalWrite(SEG_E, LOW);
digitalWrite(SEG_F, LOW);
digitalWrite(SEG_G, HIGH);
break;
case 4:
digitalWrite(SEG_A, LOW);
digitalWrite(SEG_B, HIGH);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, LOW);
digitalWrite(SEG_E, LOW);
digitalWrite(SEG_F, HIGH);
digitalWrite(SEG_G, HIGH);
break;
case 5:
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_B, LOW);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, HIGH);
digitalWrite(SEG_E, LOW);
digitalWrite(SEG_F, HIGH);
digitalWrite(SEG_G, HIGH);
break;
case 6:
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_B, LOW);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, HIGH);
digitalWrite(SEG_E, HIGH);
digitalWrite(SEG_F, HIGH);
digitalWrite(SEG_G, HIGH);
break;
case 7:
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_B, HIGH);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, LOW);
digitalWrite(SEG_E, LOW);
digitalWrite(SEG_F, LOW);
digitalWrite(SEG_G, LOW);
break;
case 8:
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_B, HIGH);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, HIGH);
digitalWrite(SEG_E, HIGH);
digitalWrite(SEG_F, HIGH);
digitalWrite(SEG_G, HIGH);
break;
case 9:
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_B, HIGH);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, HIGH);
digitalWrite(SEG_E, LOW);
digitalWrite(SEG_F, HIGH);
digitalWrite(SEG_G, HIGH);
break;
}
delay(1);
digitalWrite(DIGIT1, HIGH);
digitalWrite(DIGIT2, LOW);
switch(digit1) {
case 0:
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_B, HIGH);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, HIGH);
digitalWrite(SEG_E, HIGH);
digitalWrite(SEG_F, HIGH);
digitalWrite(SEG_G, LOW);
break;
case 1:
digitalWrite(SEG_A, LOW);
digitalWrite(SEG_B, HIGH);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, LOW);
digitalWrite(SEG_E, LOW);
digitalWrite(SEG_F, LOW);
digitalWrite(SEG_G, LOW);
break;
case 2:
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_B, HIGH);
digitalWrite(SEG_C, LOW);
digitalWrite(SEG_D, HIGH);
digitalWrite(SEG_E, HIGH);
digitalWrite(SEG_F, LOW);
digitalWrite(SEG_G, HIGH);
break;
case 3:
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_B, HIGH);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, HIGH);
digitalWrite(SEG_E, LOW);
digitalWrite(SEG_F, LOW);
digitalWrite(SEG_G, HIGH);
break;
case 4:
digitalWrite(SEG_A, LOW);
digitalWrite(SEG_B, HIGH);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, LOW);
digitalWrite(SEG_E, LOW);
digitalWrite(SEG_F, HIGH);
digitalWrite(SEG_G, HIGH);
break;
case 5:
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_B, LOW);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, HIGH);
digitalWrite(SEG_E, LOW);
digitalWrite(SEG_F, HIGH);
digitalWrite(SEG_G, HIGH);
break;
case 6:
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_B, LOW);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, HIGH);
digitalWrite(SEG_E, HIGH);
digitalWrite(SEG_F, HIGH);
digitalWrite(SEG_G, HIGH);
break;
case 7:
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_B, HIGH);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, LOW);
digitalWrite(SEG_E, LOW);
digitalWrite(SEG_F, LOW);
digitalWrite(SEG_G, LOW);
break;
case 8:
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_B, HIGH);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, HIGH);
digitalWrite(SEG_E, HIGH);
digitalWrite(SEG_F, HIGH);
digitalWrite(SEG_G, HIGH);
break;
case 9:
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_B, HIGH);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, HIGH);
digitalWrite(SEG_E, LOW);
digitalWrite(SEG_F, HIGH);
digitalWrite(SEG_G, HIGH);
break;
}
delay(1);
}
void setup() {
// 设置数码管引脚为输出模式
pinMode(DIGIT1, OUTPUT);
pinMode(DIGIT2, OUTPUT);
pinMode(SEG_A, OUTPUT);
pinMode(SEG_B, OUTPUT);
pinMode(SEG_C, OUTPUT);
pinMode(SEG_D, OUTPUT);
pinMode(SEG_E, OUTPUT);
pinMode(SEG_F, OUTPUT);
pinMode(SEG_G, OUTPUT);
pinMode(SEG_DP, OUTPUT);
// 设置温度传感器引脚为输入模式
pinMode(TEMPERATURE_PIN, INPUT);
// 设置数码管颜色
pixels.begin();
pixels.setPixelColor(0, pixels.Color(255, 0, 0));
pixels.setPixelColor(1, pixels.Color(255, 0, 0));
pixels.show();
}
void loop() {
// 读取温度传感器
int sensorValue = analogRead(TEMPERATURE_PIN);
temperature = (sensorValue / 1023.0) * 3.3; // 电压值
temperature = (temperature - 0.5) * 100; // 温度值
// 数码管显示温度
displayTemp(temperature);
// 数码管颜色变化
int colorValue = int(temperature * 2.55); // 温度值转换为颜色值
pixels.setPixelColor(0, pixels.Color(255 - colorValue, colorValue, 0));
pixels.setPixelColor(1, pixels.Color(255 - colorValue, colorValue, 0));
pixels.show();
delay(1000);
}
```
该代码首先定义了数码管引脚和温度传感器引脚。然后在`setup()`函数中,将数码管引脚和温度传感器引脚设置为相应的输入/输出模式,并初始化数码管颜色。
在`loop()`函数中,首先读取温度传感器的值,并将其转换为温度值。然后调用`displayTemp()`函数,将温度值显示在两位数码管上。接着,将温度值转换为RGB颜色值,并将其设置为数码管的颜色。最后,延时1秒钟,等待下一次循环。
需要注意的是,该代码中使用了`Adafruit_NeoPixel`库来控制数码管的颜色。如果您没有安装该库,可以在Arduino IDE菜单中选择“工具->库管理器”,搜索“Adafruit_NeoPixel”,并安装该库。
阅读全文