極シンプルな電子時計の作り方(プログラム編)

IMG_1099

ビデオ

準備:

arduino uno         一つ

Adafruit I2C通信の7セグメント4桁ディスプレイ (赤色)  一つ

導線      若干

タクトスイッチ 二つ

作り方:

プログラミングの方:

// pin CLK->A5 DAT->A4 GND VCC+
#include <Wire.h>
#include “Adafruit_LEDBackpack.h”
#include “Adafruit_GFX.h”
//————metro timer——————————–

struct _timer {
int min;
int sec;
int minx10_set;
int min_set;
int secx10_set;
int sec_set;
}
timer;

class _metro
{
public:
unsigned long long timestamp;
unsigned int count;
boolean flg_pause;
_metro(unsigned int _count) {
count = _count;
timestamp = millis();
flg_pause = true;
}
void set(unsigned int _count) {
count = _count;
timestamp = millis();
}
boolean check() {
if ( flg_pause ) {
return false;
}
if ( millis() – timestamp >= count ) {
timestamp = millis();
return true;
}
else {
return false;
}
}
void pause() {
flg_pause = true;
}
void start() {
flg_pause = false;
}
}
metro(1000);
//————metro timer——————————–

Adafruit_7segment matrix = Adafruit_7segment();

int decHours;
int hours;
int decMinutes;
int minutes;

//数える用
int seconds = 0;
int i = 0; // minutes
int j = 0; //hours

void setup() {
metro.set(1000);
metro.start();
Serial.begin(9600);
Serial.println(“7 Segment Backpack Test”);
matrix.begin(0x70);

//——–pause switch—————————–
pinMode(7, INPUT);
//——–start switch—————————–
pinMode(8, INPUT);

}

void loop() {
//————delay clock——————-
/* if (metro.check() == true) {
for (int j = 0; j < 24 ; j++)
{

for (int i = 0; i < 60; i++)
{

decHours = (j / 10) % 10;
hours = j % 10;
decMinutes = (i / 10) % 10;
minutes = i % 10;

matrix.drawColon(true);
matrix.writeDigitNum(0, decHours, false);
matrix.writeDigitNum(1, hours, false);
matrix.writeDigitNum(3, decMinutes, false);
matrix.writeDigitNum(4, minutes, false);
matrix.writeDisplay();
delay(100);
}
}
}*/
//——————-delay clock———————————-
//——————–metro control clock————————-
switchPauseControl();
switchStartControl();
if (metro.check() == true) {
seconds++;
if (seconds > 59) {
seconds = 0;
i++;
if (i > 59)
{
i = 0;
j++;
if (j > 23)
{
j = 0;
}
}
}
decHours = (j / 10) % 10;
hours = j % 10;
decMinutes = (i / 10) % 10;
minutes = i % 10;
/*
Serial.print(decHours);
Serial.print(hours);
Serial.print(“:”);
Serial.print(decMinutes);
Serial.print(minutes);
Serial.print(“:”);
Serial.print(seconds);
Serial.println();
*/
if (seconds % 2 == 0)
{
matrix.drawColon(false);
}
else {
matrix.drawColon(true);
}
matrix.writeDigitNum(0, decHours, false);
matrix.writeDigitNum(1, hours, false);
matrix.writeDigitNum(3, decMinutes, false);
matrix.writeDigitNum(4, minutes, false);
matrix.writeDisplay();
}
//——————–metro control clock———————
}
void switchPauseControl()
{
//スイッチを押すと一時停止になる
if (digitalRead(7) == HIGH)
{
metro.pause();
seconds++;
if (seconds > 59) {
seconds = 0;
i++;
if (i > 59)
{
i = 0;
j++;
if (j > 23)
{
j = 0;
}
}
}

decMinutes = (i / 10) % 10;
minutes = i % 10;
decHours = (j / 10) % 10;
hours = j % 10;
matrix.drawColon(false);
matrix.writeDigitNum(0, decHours, false);
matrix.writeDigitNum(1, hours, false);
matrix.writeDigitNum(3, decMinutes, false);
matrix.writeDigitNum(4, minutes, false);
matrix.writeDisplay();

}
}

void switchStartControl()
{
if (digitalRead(8) == HIGH)
{
metro.set(1000);
metro.start();
matrix.drawColon(true);
matrix.writeDigitNum(0, decHours, false);
matrix.writeDigitNum(1, hours, false);
matrix.writeDigitNum(3, decMinutes, false);
matrix.writeDigitNum(4, minutes, false);
matrix.writeDisplay();
seconds = 0;
}
}

ページ立ち上がったばかりで、ちょろちょろ更新します。

広告