请用arduino写一个两个二位数码管的时钟
时间: 2023-08-05 10:04:11 浏览: 107
以下是使用Arduino Uno编写的双位数字时钟代码:
```
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_LEDBackpack.h>
// Define the two 7-segment displays
Adafruit_7segment disp1 = Adafruit_7segment();
Adafruit_7segment disp2 = Adafruit_7segment();
void setup() {
// Initialize the two 7-segment displays
disp1.begin(0x70);
disp2.begin(0x71);
}
void loop() {
// Get the current time
unsigned long currentMillis = millis();
unsigned long seconds = currentMillis / 1000;
unsigned long minutes = seconds / 60;
unsigned long hours = minutes / 60;
// Calculate the display values
uint8_t hour1 = hours / 10;
uint8_t hour2 = hours % 10;
uint8_t min1 = minutes / 10;
uint8_t min2 = minutes % 10;
// Display the values on the two 7-segment displays
disp1.writeDigitNum(0, hour1, false);
disp1.writeDigitNum(1, hour2, true);
disp1.writeDisplay();
disp2.writeDigitNum(0, min1, false);
disp2.writeDigitNum(1, min2, true);
disp2.writeDisplay();
// Delay to update the display every second
delay(1000);
}
```
这个代码使用两个Adafruit 7段数码管模块来显示小时和分钟。两个模块通过I2C总线连接到Arduino Uno上。该代码使用millis()函数来获取当前时间,并将其转换为小时和分钟值。然后,它将这些值写入到两个7段数码管模块中,并使用delay()函数来使时钟每秒更新一次。
阅读全文