// TM1637 SevenSegment Counter Wokwi Example
//
// https://wokwi.com/projects/339227323398095442
#include <TM1637.h>
#include "pitches.h"
const int CLK = 2;
const int DIO = 3;
TM1637 tm(CLK, DIO);
void setup() {
tm.init();
tm.set(BRIGHT_TYPICAL);
Serial.begin(9600);
}
int a0;
int a1;
int a2;
int a3;
int from_a;
int from_set = 0;
int from_cur;
void loop() {
static unsigned long timer = millis();
a0 = analogRead(0);
a0 = map(a0, 0, 1023, 0, 9);
a1 = analogRead(1);
a1 = map(a1, 0, 1023, 0, 9);
a2 = analogRead(2);
a2 = map(a2, 0, 1023, 0, 9);
a3 = analogRead(3);
a3 = map(a3, 0, 1023, 0, 9);
from_a = a0 + a1*10 + a2*100 + a3*1000;
if (millis() - timer >= 1000) {
Serial.println(String(a3) + String(a2) + String(a1) + String(a0) );
if (from_a == from_set) {
if (from_cur > 0) {
from_cur -= 1;
} else {
tone(8, NOTE_D5);
delay(300);
noTone(8);
}
} else {
from_cur = from_a;
from_set = from_a;
}
timer += 1000;
tm.display(0, (from_cur / 1000) % 10);
tm.display(1, (from_cur / 100) % 10);
tm.display(2, (from_cur / 10) % 10);
tm.display(3, from_cur % 10);
}
}