概要
時計(lumi time)が壊れたので、arduinoで作ってみる。
写真
サンプルコード
# define stb 		10
# define clk 		9
# define dio 		8
uint8_t a = 0;
uint8_t b = 0;
uint8_t c = 0;
uint8_t dhor = 12;
uint8_t dmin = 34;
uint8_t dsec = 00;
int tOffset;
boolean latch = false;
void up() {
	uint8_t u = dhor / 10;
	uint8_t w = dhor % 10;
	uint8_t x = dmin / 10;
	uint8_t y = dmin % 10;
	uint8_t t;
	uint8_t a[] = {1, 0, 1, 1, 0, 1, 0, 1, 1, 1};
	uint8_t b[] = {1, 1, 1, 1, 1, 0, 0, 1, 1, 1};
	uint8_t c[] = {1, 1, 0, 1, 1, 1, 1, 1, 1, 1};
	uint8_t d[] = {1, 0, 1, 1, 0, 1, 1, 0, 1, 0};
	uint8_t e[] = {1, 0, 1, 0, 0, 0, 1, 0, 1, 0};
	uint8_t f[] = {1, 0, 0, 0, 1, 1, 1, 0, 1, 1};
	uint8_t g[] = {0, 0, 1, 1, 1, 1, 1, 0, 1, 1};
	uint8_t h[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
	digitalWrite(stb, LOW);
	shiftOut(dio, clk, LSBFIRST, 0x40);
	digitalWrite(stb, HIGH);
	digitalWrite(stb, LOW);
	shiftOut(dio, clk, LSBFIRST, 0xc0);
	t = a[y] << 4;
	t = t | a[x] << 5;
	t = t | a[w] << 6;
	t = t | a[u] << 7;
	shiftOut(dio, clk, LSBFIRST, t);
	shiftOut(dio, clk, LSBFIRST, 0x01);
	t = b[y] << 4;
	t = t | b[x] << 5;
	t = t | b[w] << 6;
	t = t | b[u] << 7;
	shiftOut(dio, clk, LSBFIRST, t);
	shiftOut(dio, clk, LSBFIRST, 0x01);
	t = c[y] << 4;
	t = t | c[x] << 5;
	t = t | c[w] << 6;
	t = t | c[u] << 7;
	shiftOut(dio, clk, LSBFIRST, t);
	shiftOut(dio, clk, LSBFIRST, 0x01);
	t = d[y] << 4;
	t = t | d[x] << 5;
	t = t | d[w] << 6;
	t = t | d[u] << 7;
	shiftOut(dio, clk, LSBFIRST, t);
	shiftOut(dio, clk, LSBFIRST, 0x01);
	t = e[y] << 4;
	t = t | e[x] << 5;
	t = t | e[w] << 6;
	t = t | e[u] << 7;
	shiftOut(dio, clk, LSBFIRST, t);
	shiftOut(dio, clk, LSBFIRST, 0x01);
	t = f[y] << 4;
	t = t | f[x] << 5;
	t = t | f[w] << 6;
	t = t | f[u] << 7;
	shiftOut(dio, clk, LSBFIRST, t);
	shiftOut(dio, clk, LSBFIRST, 0x01);
	t = g[y] << 4;
	t = t | g[x] << 5;
	t = t | g[w] << 6;
	t = t | g[u] << 7;
	shiftOut(dio, clk, LSBFIRST, t);
	shiftOut(dio, clk, LSBFIRST, 0x01);
	t = h[y] << 4;
	t = t | h[x] << 5;
	t = t | h[w] << 6;
	t = t | h[u] << 7;
	shiftOut(dio, clk, LSBFIRST, t);
	shiftOut(dio, clk, LSBFIRST, 0x01);
	digitalWrite(stb, HIGH);
}
void setup() {
	Serial.begin(9600);
	pinMode(stb, OUTPUT);
	pinMode(clk, OUTPUT);
	pinMode(dio, OUTPUT);
	digitalWrite(stb, LOW);
	shiftOut(dio, clk, LSBFIRST, 0x88);
	digitalWrite(stb, HIGH);
	tOffset = 500 - millis() % 1000;
	Serial.print("ok");
}
void loop() {
	uint8_t v;
	uint8_t btn = 20;
	digitalWrite(stb, LOW);
	shiftOut(dio, clk, LSBFIRST, 0x42);
	pinMode(dio, INPUT);
	v = shiftIn(dio, clk, LSBFIRST);
	if (v == 0x02)
	{
		btn = 1;
	}
	else if (v == 0x04)
	{
		btn = 7;
	}
	else if (v == 0x20)
	{
		btn = 2;
	}
	else if (v == 0x40)
	{
		btn = 8;
	}
	v = shiftIn(dio, clk, LSBFIRST);
	v = shiftIn(dio, clk, LSBFIRST);
	v = shiftIn(dio, clk, LSBFIRST);
	pinMode(dio, OUTPUT);
	digitalWrite(stb, HIGH);
	if (btn == 1)
	{
		dmin--;
	}
	if (btn == 2)
	{
		dhor--;
	}
	if (btn == 7)
	{
		dmin++;
	}
	if (btn == 8)
	{
		dhor++;
	}
	if ((millis() - tOffset) % 1000 < 500)
	{
		latch = true;
	}
	if (((millis() - tOffset) % 1000 >= 500) && latch)
	{
		latch = false;
		dsec++;
		if (dsec == 60)
		{
			dsec = 0;
			dmin++;
			if (dmin == 60)
			{
				dmin = 0;
				dhor++;
				if (dhor == 24)
				{
					dhor = 0;
				}
			}
		}
	}
	up();
	delay(300);
}
以上。
