7 segment BCD binary counter

Hi, I just started learning arduino coding, so I am really new at this. I tried and searched as much as i could, but i cant figure it out. So the thing is really simple, i need to use LEDs to display binary numbers, and then use the 7-segment with a decoder to display the current state (1,2,3, etc.). The problem is I dont know how to incorpoate the binary one, which is something like digitalWrite(led,HIGH/LOW) then delay to add more. So far i have this, but again, i really dont know how to incorporate the code above… :frowning:


const int A=13;
const int B=12;
const int C=11;
const int D=10;

int led0 = 9;
int led1 = 8;
int led2 = 7;
int led3 = 6;
int led4 = 5;
int led5 = 4;
int led6 = 3;
int led7 = 2;

void setup() {
  pinMode(A3, INPUT);
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);

  pinMode(led0, OUTPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
}

int count = 0; //variabila folosita pentru afisarea numarului

void loop() {
  if (digitalRead(A3) == LOW) //daca butonul e apasat
    {
    count++; 
    delay(200); //the delay prevent from button bouncing
    if (count == 10) //we want to count from 0 to 9!
      count = 0;
    to_BCD(); //convert to binary
    }
   
  if (count == 10)
    count = 0;

digitalWrite(led0, LOW);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
digitalWrite(led6, HIGH);
digitalWrite(led7, HIGH);
delay(229.4);

digitalWrite(led0, LOW);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
digitalWrite(led7, HIGH);
delay(7.4);

digitalWrite(led0, LOW);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
digitalWrite(led7, LOW);
delay(118.4);

digitalWrite(led0, LOW);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, HIGH);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
digitalWrite(led7, LOW);
delay(59.2);

digitalWrite(led0, LOW);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, HIGH);
digitalWrite(led5, LOW);
digitalWrite(led6, HIGH);
digitalWrite(led7, HIGH);
delay(81.4);

digitalWrite(led0, LOW);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
digitalWrite(led4, LOW);
digitalWrite(led5, HIGH);
digitalWrite(led6, LOW);
digitalWrite(led7, HIGH);
delay(155.4);

digitalWrite(led0, LOW);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
digitalWrite(led7, LOW);
delay(0);

digitalWrite(led0, LOW);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
digitalWrite(led7, LOW);
delay(118.4);
}

void to_BCD()
{
    if (count == 0) //write 0000
	{
      digitalWrite(A, LOW);
      digitalWrite(B, LOW);
      digitalWrite(C, LOW);
      digitalWrite(D, LOW);
    }
    
    if (count == 1) //write 0001
	{
      digitalWrite(A, HIGH);
      digitalWrite(B, LOW);
      digitalWrite(C, LOW);
      digitalWrite(D, LOW);
    }
    
    if (count == 2) //write 0010
	{
      digitalWrite(A, LOW);
      digitalWrite(B, HIGH);
      digitalWrite(C, LOW);
      digitalWrite(D, LOW);
    }
    
    if (count == 3) //write 0011
	{
      digitalWrite(A, HIGH);
      digitalWrite(B, HIGH);
      digitalWrite(C, LOW);
      digitalWrite(D, LOW);
    }
    
    if (count == 4) //write 0100
	{
      digitalWrite(A, LOW);
      digitalWrite(B, LOW);
      digitalWrite(C, HIGH);
      digitalWrite(D, LOW);
    }
    
    if (count == 5) //write 0101
	{
      digitalWrite(A, HIGH);
      digitalWrite(B, LOW);
      digitalWrite(C, HIGH);
      digitalWrite(D, LOW);
    }
    
    if (count == 6) //write 0110
	{
      digitalWrite(A, LOW);
      digitalWrite(B, HIGH);
      digitalWrite(C, HIGH);
      digitalWrite(D, LOW);
    }
    
    if (count == 7) //write 0111
	{
      digitalWrite(A, HIGH);
      digitalWrite(B, HIGH);
      digitalWrite(C, HIGH);
      digitalWrite(D, LOW);
    }
    
    if (count == 8) //write 1000
	{
      digitalWrite(A, LOW);
      digitalWrite(B, LOW);
      digitalWrite(C, LOW);
      digitalWrite(D, HIGH);
    }
    
    if (count == 9) //write 1001
	{
      digitalWrite(A, HIGH);
      digitalWrite(B, LOW);
      digitalWrite(C, LOW);
      digitalWrite(D, HIGH);
    } 
}

Welcome,

You can do something like this

const uint8_t ledPins[] = { 13, 12, 11, 10 };
const uint8_t segmentsPins[] = { 9, 8, 7, 6, 5, 4, 3 }; //ABCDEFG

void setup()
{
  for ( uint8_t i = 0; i < 4; i++ )
  {
    pinMode( ledPins[i], OUTPUT );
  }
  
  for ( uint8_t i = 0; i < 7; i++ )
  {
    pinMode( segmentsPins[i], OUTPUT );
  }
}

void loop()
{
  static uint8_t digit = 0;
  
  displayDigit( digit );

  if ( digit++ == 9 )
  {
    digit = 0;
  }
  
  delay( 1000 );
}

void displayDigit( uint8_t digit )
{
  // binary
  for ( uint8_t i = 0; i < 4; i++ )
  {
    digitalWrite( ledPins[i], (digit >> i) & 1 ? HIGH : LOW );
  }
  
  // 7-segments
  uint8_t segmentsData[] =
  {
    //GFEDCBA
    0b0111111, // '0'
    0b0000110, // '1'
    0b1011011, // '2'
    0b1001111, // '3'
    0b1100110, // '4'
    0b1101101, // '5'
    0b1111101, // '6'
    0b0000111, // '7'
    0b1111111, // '8'
    0b1100111, // '9'
  };
  
  uint8_t digitData = segmentsData[digit];
 
  for ( uint8_t i = 0; i < 7; i++ )
  {
    digitalWrite( segmentsPins[i], (digitData >> i) & 1 ? LOW : HIGH );
  }
}

to_BCD can be simplified to:

void to_BCD()
{
  digitalWrite(A, count & 1);
  digitalWrite(B, count & 2);
  digitalWrite(C, count & 4);
  digitalWrite(D, count & 8);
}

Similarly, you could pass a binary value to a function to set all 8 LED pins:

void to_LEDs(byte mask)
{
  digitalWrite(led0, mask & 0b00000001);
  digitalWrite(led1, mask & 0b00000010);
  digitalWrite(led2, mask & 0b00000100);
  digitalWrite(led3, mask & 0b00001000);
  digitalWrite(led4, mask & 0b00010000);
  digitalWrite(led5, mask & 0b00100000);
  digitalWrite(led6, mask & 0b01000000);
  digitalWrite(led7, mask & 0b10000000);
}
1 Like

Hi, i am really new at coding, so i don’t really know how to deal with this. So i got this scheme which consists of 8 LEDs and a 7-segment display with a BCD decoder. I need 8 states each using the 8 LEDs to light up so they show a binary number for every state (state 1 lights up the LEDs to show 0001; state 2 1101;etc.) and the 7-segment display to show the number of the state ( 1,2,3,etc. until 8).
This is the scheme

So far i came up this but it’s not working

const int A=13;
const int B=12;
const int C=11;
const int D=10;

int led0 = 9;
int led1 = 8;
int led2 = 7;
int led3 = 6;
int led4 = 5;
int led5 = 4;
int led6 = 3;
int led7 = 2;

void setup() {
  pinMode(A3, INPUT);
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);

  pinMode(led0, OUTPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
}

int count = 0; //variabila folosita pentru afisarea numarului

void loop() {
  if (digitalRead(A3) == LOW) //daca butonul e apasat
    {
    count++; 
    delay(200); //the delay prevent from button bouncing
    if (count == 10) //we want to count from 0 to 9!
      count = 0;
    to_BCD(); //convert to binary
    }
   
  if (count == 10)
    count = 0;

digitalWrite(led0, LOW);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
digitalWrite(led6, HIGH);
digitalWrite(led7, HIGH);
delay(229.4);

digitalWrite(led0, LOW);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
digitalWrite(led7, HIGH);
delay(7.4);

digitalWrite(led0, LOW);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
digitalWrite(led7, LOW);
delay(118.4);

digitalWrite(led0, LOW);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, HIGH);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
digitalWrite(led7, LOW);
delay(59.2);

digitalWrite(led0, LOW);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, HIGH);
digitalWrite(led5, LOW);
digitalWrite(led6, HIGH);
digitalWrite(led7, HIGH);
delay(81.4);

digitalWrite(led0, LOW);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
digitalWrite(led4, LOW);
digitalWrite(led5, HIGH);
digitalWrite(led6, LOW);
digitalWrite(led7, HIGH);
delay(155.4);

digitalWrite(led0, LOW);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
digitalWrite(led7, LOW);
delay(0);

digitalWrite(led0, LOW);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
digitalWrite(led7, LOW);
delay(118.4);
}

void to_BCD()
{
    if (count == 0) //write 0000
	{
      digitalWrite(A, LOW);
      digitalWrite(B, LOW);
      digitalWrite(C, LOW);
      digitalWrite(D, LOW);
    }
    
    if (count == 1) //write 0001
	{
      digitalWrite(A, HIGH);
      digitalWrite(B, LOW);
      digitalWrite(C, LOW);
      digitalWrite(D, LOW);
    }
    
    if (count == 2) //write 0010
	{
      digitalWrite(A, LOW);
      digitalWrite(B, HIGH);
      digitalWrite(C, LOW);
      digitalWrite(D, LOW);
    }
    
    if (count == 3) //write 0011
	{
      digitalWrite(A, HIGH);
      digitalWrite(B, HIGH);
      digitalWrite(C, LOW);
      digitalWrite(D, LOW);
    }
    
    if (count == 4) //write 0100
	{
      digitalWrite(A, LOW);
      digitalWrite(B, LOW);
      digitalWrite(C, HIGH);
      digitalWrite(D, LOW);
    }
    
    if (count == 5) //write 0101
	{
      digitalWrite(A, HIGH);
      digitalWrite(B, LOW);
      digitalWrite(C, HIGH);
      digitalWrite(D, LOW);
    }
    
    if (count == 6) //write 0110
	{
      digitalWrite(A, LOW);
      digitalWrite(B, HIGH);
      digitalWrite(C, HIGH);
      digitalWrite(D, LOW);
    }
    
    if (count == 7) //write 0111
	{
      digitalWrite(A, HIGH);
      digitalWrite(B, HIGH);
      digitalWrite(C, HIGH);
      digitalWrite(D, LOW);
    }
    
    if (count == 8) //write 1000
	{
      digitalWrite(A, LOW);
      digitalWrite(B, LOW);
      digitalWrite(C, LOW);
      digitalWrite(D, HIGH);
    }
    
    if (count == 9) //write 1001
	{
      digitalWrite(A, HIGH);
      digitalWrite(B, LOW);
      digitalWrite(C, LOW);
      digitalWrite(D, HIGH);
    } 
}

The digitalWrite highs and lows in the void loop represent each binary number i need the LEDs to show for each state.

here’s some code. a button pressed increments the counter and its value is intended to be displayed on the 7-seg display and in binary on the 4 LEDs

this code examines binary bits rather than having explicit code setting LED states depending on the value.

it wasn’t sure which pins controlled which segments. these can easily be redefined in pinsSeq []

segDigit defines the segments for 10 digits.

enum { Off = HIGH, On = LOW };

const int A=13;
const int B=12;
const int C=11;
const int D=10;

byte pinsLed [] = { 13, 12, 11, 10 };
#define N_LED  sizeof(pinsLed)

// segments pins: A B C D E F G DP
byte pinsSeg [] = { 9, 8, 7, 6, 5, 4, 3, 2 };
#define N_SEG  sizeof(pinsSeg)

enum {
    SegA  = 0x01,
    SegB  = 0x02,
    SegC  = 0x04,
    SegD  = 0x08,

    SegE  = 0x10,
    SegF  = 0x20,
    SegG  = 0x40,
    SegDP = 0x80,
};

int segDigit [] = {
    SegA | SegB | SegC | SegD | SegE | SegF       ,    // 0
           SegB | SegC                            ,    // 1
    SegA | SegB |        SegD | SegE |        SegG,    // 2
    SegA | SegB | SegC | SegD |               SegG,    // 3
           SegB | SegC |               SegF | SegG,    // 4

    SegA |        SegC | SegD |        SegF | SegG,    // 5
    SegA |        SegC | SegD | SegE | SegF | SegG,    // 6
    SegA | SegB | SegC                            ,    // 7
    SegA | SegB | SegC | SegD | SegE | SegF | SegG,    // 8
    SegA | SegB | SegC | SegD |        SegF | SegG,    // 9
};

// -----------------------------------------------------------------------------
void setup() {
    Serial.begin (9600);

    pinMode(A3, INPUT_PULLUP);

    for (unsigned n = 0; n < N_LED; n++)  {
        digitalWrite (pinsLed [n], Off);
        pinMode      (pinsLed [n], OUTPUT);
    }

    for (unsigned n = 0; n < N_SEG; n++)  {
        digitalWrite (pinsSeg [n], Off);
        pinMode      (pinsSeg [n], OUTPUT);
    }

}

// -----------------------------------------------------------------------------
void
dispLeds (
    int  digit )
{
    for (unsigned n = 0; n < N_LED; n++)  {
        digitalWrite (pinsLed [n], digit & (1 << n) ? On : Off);
    }
}

// -----------------------------------------------------------------------------
void
dispDigit (
    int  digit )
{
    int segs = segDigit [digit];

#if 1
    // set each led segment
    for (unsigned n = 0; n < N_SEG; n++)  {
        digitalWrite (pinsSeg[n], segs & (1 << n) ? On : Off);
    }
#else   // this is how i tried testing
    char s [40];
    sprintf (s, " 0x%02x", segs);
    Serial.print   (s);

    for (unsigned n = 0; n < N_SEG; n++)  {
        sprintf (s, ", %d %d", pinsSeg[n], segs & (1 << n) ? On : Off);
        Serial.print   (s);
    }
    Serial.println ();
#endif

}

// -----------------------------------------------------------------------------
int count = 0; //variabila folosita pentru afisarea numarului

void loop() {
    static byte butLst = Off;
           byte but = digitalRead(A3);
    if (butLst != but)  {
        butLst = but;

        if (LOW == but)  {
            count = (count + 1) % 10;
            dispLeds (count);
            dispDigit(count);
        }
    }
}
1 Like

I’ve merged the " Code for a 7-segment display sequence with LEDs " topic with this one, since it appears to be a continuation of the original discussion, in which case the previous context may be valuable to the reader.

Thank you so much guys, now i really got a hang of it and managed to get some things working. Thx for the help @guix @gcjr @johnwasser .

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.