// TM1637 SevenSegment Counter Wokwi Example
//
// https://wokwi.com/projects/339227323398095442
#include <TM1637.h>
const int CLK = 6;
const int DIO = 3;
#define PIN_BLUE_BTN 5
#define PIN_GREEN_BTN 4
TM1637 tm(CLK, DIO);
int countapezzi = 0;
bool prevClock = true;
uint8_t prevSwitchState;
// codifica binaria dei simboli da 0 a 9
#define SG0 63
#define SG1 6
#define SG2 91
#define SG3 79
#define SG4 116
#define SG5 109
#define SG6 125
#define SG7 7
#define SG8 127
#define SG9 111
uint8_t readSwitchState() {
bool a1s = digitalRead(A1);
bool a2s = digitalRead(A2);
bool a3s = digitalRead(A3);
return (a1s << 2) | (a2s << 1) | (a3s << 0);
}
void writeSevSeg(uint8_t digit) {
uint8_t pinA = 7;
for (uint8_t i=0; i < pinA; i++) {
if (digit & 1) {
digitalWrite(pinA+i, HIGH);
} else {
digitalWrite(pinA+i, LOW);
}
digit >>= 1;
}
}
void setup() {
Serial.begin(115200);
tm.init();
tm.set(BRIGHT_TYPICAL);//BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;
pinMode(PIN_GREEN_BTN, INPUT_PULLUP);
pinMode(A1, INPUT_PULLUP);
pinMode(A2, INPUT_PULLUP);
pinMode(A3, INPUT_PULLUP);
//writeSevSeg(SG1);
}
void loop() {
uint8_t swState = readSwitchState();
if (swState != prevSwitchState) {
prevSwitchState = swState;
Serial.println(swState);
if (swState == 3)
writeSevSeg(SG1);
else if (swState == 5)
writeSevSeg(SG2);
else if (swState == 6)
writeSevSeg(SG3);
else if (swState == 7)
writeSevSeg(SG0);
}
bool clock = digitalRead (PIN_GREEN_BTN);
if (!clock && prevClock ) { // qui incremento
countapezzi++;
tm.displayNum(countapezzi);
}
prevClock = clock;
}