// TM1637 SevenSegment Counter Wokwi Example
//
// https://wokwi.com/projects/339227323398095442
#include <TM1637.h>
const int CLK = 2;
const int DIO = 3;
#define DURATION_IN_MILLIS 3000
#define OUTPUT_LED 6
#define BLINK_LED 7
#define BUTTON1_PIN 8
#define BUTTON2_PIN 9
#define BUTTON3_PIN 10
TM1637 tm(CLK, DIO);
boolean toggle1 = 0;
unsigned int counter = 0;
unsigned int TimerCnt = 0;
int lastState1 = HIGH;
int lastState2 = HIGH;
int lastState3 = HIGH;
long prev = 0;
unsigned long onTime=0;
unsigned long onTime1=0;
int buttonState = 0;
int lastReading = HIGH;
int count;
int startTimer =0;
void setup()
{
pinMode(BUTTON1_PIN, INPUT_PULLUP);
pinMode(BUTTON2_PIN, INPUT_PULLUP);
pinMode(BUTTON3_PIN, INPUT_PULLUP);
//set pins as outputs
pinMode(OUTPUT_LED, OUTPUT);
pinMode(BLINK_LED, OUTPUT);
pinMode(A0, INPUT);
tm.init();
tm.set(BRIGHT_TYPICAL);
cli();//stop interrupts
//set timer1 interrupt at 1Hz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 1hz increments
OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS12 and CS10 bits for 1024 prescaler
TCCR1B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
Serial.begin(115200);
sei();//allow interrupts
}
void loop()
{
buttonState = digitalRead(BUTTON1_PIN);
// Quick presses
if (buttonState == LOW && lastReading == HIGH)
{
onTime = millis();
Serial.print("\n");
Serial.print("old");
Serial.print(onTime );
Serial.print("\n");
counter = counter+1;
tm.display(0, (counter / 1000) % 10);
tm.display(1, (counter / 100) % 10);
tm.display(2, (counter / 10) % 10);
tm.display(3, counter % 10);
}
//button being held for half second
if (buttonState == LOW && lastReading == LOW )
{
if ((millis() - onTime) > 3000 ) // half second hold time
{
delay(200); // this delay is just for "count"
TimerCnt = counter;
Serial.println(TimerCnt);
lastReading = HIGH;
onTime1 =millis();
Serial.print("\n");
Serial.print("new");
Serial.print(onTime1);
Serial.print("\n");
startTimer = 1;
}
}
lastReading = buttonState;
int value1 = digitalRead((BUTTON2_PIN));
if (lastState2 != value1)
{
lastState2 = value1;
if (value1== LOW)
{
if(counter !=0)
{
counter = counter-1;
}
tm.display(0, (counter / 1000) % 10);
tm.display(1, (counter / 100) % 10);
tm.display(2, (counter / 10) % 10);
tm.display(3, counter % 10);
}
}
int analogValue = analogRead(A0);
float Voltage = (analogValue * 5.0)/1023;
//Serial.print("Voltage: ");
//Serial.print(Voltage);
//Serial.print("\n ");
delay(100);
}
ISR(TIMER1_COMPA_vect)
{//timer1 interrupt 1Hz toggles pin 13 (LED)
//generates pulse wave of frequency 1Hz/2 = 0.5kHz (takes two cycles for full wave- toggle high then toggle low)
if(startTimer !=0)
{
if(TimerCnt !=0)
{
TimerCnt = TimerCnt-1;
if (toggle1)
{
digitalWrite(BLINK_LED,HIGH);
toggle1 = 0;
}
else
{
digitalWrite(BLINK_LED,LOW);
toggle1 = 1;
}
}
else
{
digitalWrite(BLINK_LED,LOW);
digitalWrite(OUTPUT_LED,HIGH);
cli();//stop interrupts
}
tm.display(0, (TimerCnt / 1000) % 10);
tm.display(1, (TimerCnt / 100) % 10);
tm.display(2, (TimerCnt / 10) % 10);
tm.display(3, TimerCnt % 10);
}
}