Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!

A Tiny Counter

Building a simple 7 segment display counter using ATtiny microcontroller.

BeginnerFull instructions provided1 hour1,333
A Tiny Counter

Things used in this project

Story

Read more

Schematics

Final Circuit

Code

code for 1st attiny

Arduino
int m = 0;//here m is defined as a counter
int LEDs[] = {0,1,2,3};// the outpins of the attiny
//{a,b,c,d} led states of the 7 segment display
int one[] = {1, 0, 0, 1};
int two[] = {0, 0, 1, 0};
int three[] = {0, 0, 0, 00};
int four[] = {1, 0, 0, 1};
int five[] = {0, 1, 0, 0};
int six[] = {0, 1, 0, 0};
int seven[] = {0, 0, 0, 1};
int eight[] = {0, 0, 0, 0};
int nine[] = {0, 0, 0, 0};
int zero[] = {0, 0, 0, 0};
int nil[] = {1, 1, 1, 1}; 

void setup() {
  //setting up the pinmodes for the output and input pins
  for (int i = 0; i<4; i++) pinMode(LEDs[i], OUTPUT);
  pinMode(4,INPUT_PULLUP);
}
//defining a function for each number to be displayed
void a()
{
  for (int i = 0; i<4; i++) digitalWrite(LEDs[i], one[i]);
}
void b()
{
  for (int i = 0; i<4; i++) digitalWrite(LEDs[i], two[i]);
}
void c()
{
  for (int i = 0; i<4; i++) digitalWrite(LEDs[i], three[i]);
}
void d()
{
  for (int i = 0; i<4; i++) digitalWrite(LEDs[i], four[i]);
}
void e()
{
  for (int i = 0; i<4; i++) digitalWrite(LEDs[i], five[i]);
}
void f()
{
  for (int i = 0; i<4; i++) digitalWrite(LEDs[i], six[i]);
}
void g()
{
  for (int i = 0; i<4; i++) digitalWrite(LEDs[i], seven[i]);
}
void h()
{
  for (int i = 0; i<4; i++) digitalWrite(LEDs[i], eight[i]);
}
void i()
{
  for (int i = 0; i<4; i++) digitalWrite(LEDs[i], nine[i]);
}
void j()
{
  for (int i = 0; i<4; i++) digitalWrite(LEDs[i], zero[i]);
}
//a function that switches off all the LEDs
void nothing()
{
  for (int i = 0; i<4; i++) digitalWrite(LEDs[i], nil[i]);
}

void loop()
{
  if(digitalRead(4)==LOW)
  {
    while(digitalRead(4)==LOW){}//empty loop for waiting untill the button is unpressed
    m=m+1;//increments the value of m for each button press
  }
  //calling the corresponding function for displaying the number
  if(m==1){
    a();
  }
  if(m==2){
    b();
  }
  if(m==3){
    c();
  }
  if(m==4){ 
    d();
  }
  if(m==5){
    e();
  }
  if(m==6){
    f();
  }
  if(m==7){
    g();
  }
  if(m==8){
    h();
  }
  if(m==9){
    i();
  }
  if(m==0){
    j();
  }
  m=m % 10;//when m reaches 10, it will be changed back to 0
}
 

code for 2nd attiny

Arduino
int m = 0;
int LEDs[] = {0,1,2};//defining the output pins for the attiny
//{e,f,g} led states of the 7 segment display
int one[] = {1, 1, 1};
int two[] = {0, 1, 0};
int three[] = {1, 1, 0};
int four[] = {1, 0, 0};
int five[] = {1, 0, 0};
int six[] = {0, 0, 0};
int seven[] = {1, 1, 1};
int eight[] = {0, 0, 0};
int nine[] = {1, 0, 0};
int zero[] = {0, 0, 1};
int nil[] = {1, 1, 1}; 

void setup() {
  //setting up the pinmodes for the output and input pins
  for (int i = 0; i<3; i++) pinMode(LEDs[i], OUTPUT);
  pinMode(4,INPUT_PULLUP);
}
//defining a function for each number to be displayed
void a()
{
  for (int i = 0; i<3; i++) digitalWrite(LEDs[i], one[i]);
}
void b()
{
  for (int i = 0; i<3; i++) digitalWrite(LEDs[i], two[i]);
}
void c()
{
  for (int i = 0; i<3; i++) digitalWrite(LEDs[i], three[i]);
}
void d()
{
  for (int i = 0; i<3; i++) digitalWrite(LEDs[i], four[i]);
}
void e()
{
  for (int i = 0; i<3; i++) digitalWrite(LEDs[i], five[i]);
}
void f()
{
  for (int i = 0; i<3; i++) digitalWrite(LEDs[i], six[i]);
}
void g()
{
  for (int i = 0; i<3; i++) digitalWrite(LEDs[i], seven[i]);
}
void h()
{
  for (int i = 0; i<3; i++) digitalWrite(LEDs[i], eight[i]);
}
void i()
{
  for (int i = 0; i<3; i++) digitalWrite(LEDs[i], nine[i]);
}
void j()
{
  for (int i = 0; i<3; i++) digitalWrite(LEDs[i], zero[i]);
}
//a function that switches off all the LEDs
void nothing()
{
  for (int i = 0; i<3; i++) digitalWrite(LEDs[i], nil[i]);
}

void loop()
{
  if(digitalRead(4)==LOW)
  {
    while(digitalRead(4)==LOW){}//empty loop for waiting untill the button is unpressed
    m=m+1;//increments the value of m for each button press
  }
  //calling the corresponding function for displaying the number
  if(m==1){
    a();
  }
  if(m==2){
    b();
  }
  if(m==3){
    c();
  }
  if(m==4){ 
    d();
  }
  if(m==5){
    e();
  }
  if(m==6){
    f();
  }
  if(m==7){
    g();
  }
  if(m==8){
    h();
  }
  if(m==9){
    i();
  }
  if(m==0){
    j();
  }
  m=m % 10;//when m reaches 10, it will be changed back to 0
}
 

Credits

Electronics Club, CFI, IITM
2 projects • 6 followers
We are a group of electronics enthusiasts whose mission is to explore the various realms of electronics and spread our knowledge.
Skandhan K me21b194
1 project • 1 follower
jayaram hemachander.
1 project • 1 follower
Santhosh V me21b180
2 projects • 2 followers

Comments