Program:
const int a = 13;
const int b = 12;
const int c = 11;
const int d = 10;
const int e = 9;
const int f = 8;
const int g = 7;
int k = 0;
const int upPin = 2;
const int DownPin = 3;
int x = 0;
int upx = 0; //up
int upy = 0; //down
int downx = 0; //up
int downy = 0; //down
void setup() {
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
pinMode(f, OUTPUT);
pinMode(g, OUTPUT);
pinMode( 2, INPUT);
pinMode( 3, INPUT);
segmentdigit(x);
}
void loop() {
upx = digitalRead(2);
downx = digitalRead(3);
checkupxButtonPress();
checkdownxButtonPress();
if( k ){
k = 0;
turnOff();
segmentdigit(x);
}
}
void checkupxButtonPress()
{
if (upx != upy) {
if (upx == LOW) {
k = 1;
x++;
if(x > 9) x =0 ;
} else {
}
delay(50);
}
upy = upx;
}
void checkdownxButtonPress()
{
if (downx != downy) {
if (downx == LOW) {
k = 1;
x--;
if( x < 0) x =9 ;
} else
delay(50);
}
downy = downx;
}
void segmentdigit(int digit)
{
if(digit!=1 && digit != 4)
digitalWrite(a,HIGH);
if(digit != 5 && digit != 6)
digitalWrite(b,HIGH);
if(digit !=2)
digitalWrite(c,HIGH);
if(digit != 1 && digit !=4 && digit !=7)
digitalWrite(d,HIGH);
if(digit == 2 || digit ==6 || digit == 8 || digit==0)
digitalWrite(e,HIGH);
if(digit != 1 && digit !=2 && digit!=3 && digit !=7)
digitalWrite(f,HIGH);
if (digit!=0 && digit!=1 && digit !=7)
digitalWrite(g,HIGH);
}
void turnOff()
{
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
}
CODE:
#include <TM1637Display.h>
const int buttonIncrement = 2;
const int buttonDecrement = 3;
const int CLK = 11; // Clock pin
const int DIO = 10; // Data pin
TM1637Display display(CLK, DIO);
int count = 0;
void setup() {
// Initialize buttons
pinMode(buttonIncrement, INPUT_PULLUP);
pinMode(buttonDecrement, INPUT_PULLUP);
// Initialize the display
display.setBrightness(0x0f); // Set the display to maximum brightness
}
void loop() {
// Check the increment button
if (digitalRead(buttonIncrement) == LOW) {
delay(200); // Debounce delay
count++;
}
// Check the decrement button
if (digitalRead(buttonDecrement) == LOW) {
delay(200); // Debounce delay
count--;
}
// Display the count
display.showNumberDec(count, true); // Display the number with leading zero
suppression
}