Arduino+四位数码管显示电压大小
时间: 2023-07-02 07:17:03 浏览: 251
好的,你可以参考以下代码实现:
```C++
int pinA = 2; // 数码管引脚A连接的Arduino引脚
int pinB = 3; // 数码管引脚B连接的Arduino引脚
int pinC = 4; // 数码管引脚C连接的Arduino引脚
int pinD = 5; // 数码管引脚D连接的Arduino引脚
int pinE = 6; // 数码管引脚E连接的Arduino引脚
int pinF = 7; // 数码管引脚F连接的Arduino引脚
int pinG = 8; // 数码管引脚G连接的Arduino引脚
int pinDP = 9; // 数码管引脚DP连接的Arduino引脚
void setup() {
// 设置引脚为输出模式
pinMode(pinA, OUTPUT);
pinMode(pinB, OUTPUT);
pinMode(pinC, OUTPUT);
pinMode(pinD, OUTPUT);
pinMode(pinE, OUTPUT);
pinMode(pinF, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode(pinDP, OUTPUT);
}
void loop() {
// 读取电压值并转换为字符串
float voltage = analogRead(A0) * 0.00488; // A0为连接电压传感器的Arduino引脚
String voltageStr = String(voltage, 2); // 保留两位小数
// 显示电压值
display(voltageStr);
delay(1000);
}
void display(String str) {
int len = str.length();
for (int i = 0; i < len; i++) {
// 显示当前数字
showDigit(str.charAt(i));
// 点亮小数点
if (i == len - 3) {
digitalWrite(pinDP, HIGH);
}
delay(5); // 延时一段时间
// 关闭数码管
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
digitalWrite(pinDP, LOW);
}
}
void showDigit(char c) {
switch (c) {
case '0':
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, LOW);
break;
case '1':
digitalWrite(pinA, LOW);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
break;
case '2':
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, LOW);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, LOW);
digitalWrite(pinG, HIGH);
break;
case '3':
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, HIGH);
break;
case '4':
digitalWrite(pinA, LOW);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
break;
case '5':
digitalWrite(pinA, HIGH);
digitalWrite(pinB, LOW);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, LOW);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
break;
case '6':
digitalWrite(pinA, HIGH);
digitalWrite(pinB, LOW);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
break;
case '7':
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
break;
case '8':
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
break;
case '9':
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, LOW);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
break;
case '.':
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, HIGH);
break;
}
}
```
注意,以上代码中的数码管引脚连接顺序可能与你实际连接的顺序不同,需要根据实际情况进行修改。另外,电压传感器的输出电压范围可能不同,需要根据具体型号进行调整。
阅读全文