Go Down

Topic: 2 digit 7 segment Help (Read 1 time) previous topic - next topic

gregchill

Hi I am trying to get a 2 digit 7 segment to work with 2 shift registers and a button.

I have used the following sketch as a starting point, it works with one register and one digit can anybody point me in the right direction to make a 2 digit display with 2 shift registers to increment 0 - 99 on a  button press.

I have a 2 digit common cathoid 2 digit 7 segment  display.

Any help would be great.





septillion

I have no clue why that guy only lights one bar at a time.... He can just shift out the byte from numbers and be done!

For two segments, just shift out the first, then the second and only then toggle latch :)
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

gregchill

Thanks for the reply.

do i need to create another number int set of (10 numbers.)?
Quote
For two segments, just shift out the first, then the second
Not sure how to shift out the second? do you mean.
something like this?

Code: [Select]
digitalWrite(latchPin, LOW);                  // Set latchPin LOW while clocking these 8 bits in to the register
   shiftOut(dataPin, clockPin, LSBFIRST, data);
   shiftOut(dataPin, clockPin, LSBFIRST, data);
   digitalWrite(latchPin, HIGH);           



original code

Code: [Select]




const int latchPin = 5;  // Pin connected to Pin 12 of 74HC595 (Latch)
const int dataPin  = 6;  // Pin connected to Pin 14 of 74HC595 (Data)
const int clockPin = 7;  // Pin connected to Pin 11 of 74HC595 (Clock)
int upPin = 12;      // pushbutton attached to pin 12
int downPin = 13;      // pushbutton attached to pin 13
int currUpState = 1;   // initialise currUpState as HIGH
int currDownState = 1;   // initialise currDownState as HIGH
int prevUpState = 0;
int prevDownState = 0;
int counter = 0;          // initialise counter as zero
int numbers[10] =  // Describe each digit in terms of display segments  0, 1, 2, 3, 4, 5, 6, 7, 8, 9
  {
    B11111100,
    B01100000,
    B11011010,
    B11110010,
    B01100110,
    B10110110,
    B10111110,
    B11100000,
    B11111110,
    B11100110,
  };
 
void setup()
  {
    pinMode(latchPin, OUTPUT);   // set SR pins to output
    pinMode(clockPin, OUTPUT);
    pinMode(dataPin, OUTPUT);
    pinMode(upPin, INPUT);   // sets pin 12 as pushbutton INPUT
    pinMode(downPin, INPUT);   // sets pin 13 as pushbutton INPUT
  }
 
 void loop()
  {
    currUpState = digitalRead(upPin);
    if (prevUpState != currUpState)             // has the state changed from
    {                                           // HIGH to LOW or vice versa
      prevUpState = currUpState;
      if (currUpState == HIGH)                  // If the button was pressed
        counter++;   
  if(counter > 8) counter = 0;      // increment the counter by one
 
    }
    if(counter > 8)
      counter--;
    show(numbers[counter]); // display the current digit
    currDownState = digitalRead(downPin);
    if (prevDownState != currDownState)         // has the state changed from
    {                                           // HIGH to LOW or vice versa
      prevDownState = currDownState;
      if (currDownState == HIGH)                // If the button was pressed
        counter--;                             // decrement the counter by one
 
    }
    if(counter < 0)
      counter++;
    show(numbers[counter]); // display the current digit
  }
 
  void show( byte number)
   {
    // Use a loop and a bitwise AND to move over each bit that makes up
    // the seven segment display (from left to right, A => G), and check
    // to see if it should be on or not
    for(int j = 0; j <= 7; j++)
     {
      byte toWrite = number & (B10000000 >> j);
      if(!toWrite) {
        continue;
       }                 // If all bits are 0 then no point writing it to the shift register,so break out and
       //move on to next segment.
       shiftIt(toWrite); // Otherwise shift it into the register
     }
   }

 void shiftIt (byte data)
 {
   digitalWrite(latchPin, LOW);                  // Set latchPin LOW while clocking these 8 bits in to the register
   shiftOut(dataPin, clockPin, LSBFIRST, data);
   digitalWrite(latchPin, HIGH);                //set latchPin to high to lock and send data

  // put delay here if you want to see the multiplexing in action!
  //  delay(10);
 }





Wawa

This page (and sub pages) might be interesting.
They use the TPIC6C596, which is the same (coding) as the 74HC595, but with higher voltage/current outputs.
https://www.sparkfun.com/products/13279
Leo..

Grumpy_Mike

Quote
Not sure how to shift out the second? do you mean. something like this?
Yes but send out a different byte of data in the second shift out.

septillion

And
do i need to create another number int set of (10 numbers.)?
No, the second display needs to display the same 10 digits doesn't it? One the single digits and the other the dozens. Aka, the first "value % 10" and the second "value / 10" :)
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

PaulRB

Quote
dozens
Only if the OP want to display the number in duodecimal format!

septillion

Haha, you're right! I'll never trust Google again when in doubt about a word :D I just meant the multiples of ten
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

gregchill

Thanks eveyone

Quote
Yes but send out a different byte of data in the second shift out.
Do i need to create a new void function in order to do this?

septillion

Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

PaulRB

#10
Nov 17, 2016, 12:03 pm Last Edit: Nov 17, 2016, 12:04 pm by PaulRB
No, you just need to split your 0..99 number into tens and units as septillion described.

septillion

But again, I see no use is sending each bar seperate so the code is a bit weird...

Quick edit (so not perfect) to give you an idea of the weird code....
Code: [Select]

//did some edits to remove the stupid parts...
const byte  latchPin = 5;  // Pin connected to Pin 12 of 74HC595 (Latch)
const byte dataPin  = 6;  // Pin connected to Pin 14 of 74HC595 (Data)
const byte clockPin = 7;  // Pin connected to Pin 11 of 74HC595 (Clock)
const byte upPin = 12;      // pushbutton attached to pin 12
const byte downPin = 13;      // pushbutton attached to pin 13
bool prevUpState = 0;
bool prevDownState = 0;
int counter = 0;          // initialise counter as zero

//Min and max value of the counter
const byte maxCount = 99;
const byte minCount = 0;

byte numberOfDigits = 2;


const byte numbers[10] =  // Describe each digit in terms of display segments  0, 1, 2, 3, 4, 5, 6, 7, 8, 9
{
  B11111100,
  B01100000,
  B11011010,
  B11110010,
  B01100110,
  B10110110,
  B10111110,
  B11100000,
  B11111110,
  B11100110,
};

void setup()
{
  pinMode(latchPin, OUTPUT);   // set SR pins to output
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(upPin, INPUT);   // sets pin 12 as pushbutton INPUT
  pinMode(downPin, INPUT);   // sets pin 13 as pushbutton INPUT
}

void loop()
{
  //counter part
  //currUpState = digitalRead(upPin); <== NO need for stupid global
  if (prevUpState != digitalRead(upPin))             // has the state changed from
  { // HIGH to LOW or vice versa
    prevUpState = !prevUpState; //invert
    if (currUpState == HIGH && counter < maxCount)                  // If the button was pressed AND not reached limit
      counter++;
   
    //if (counter > 8) counter = 0;     // increment the counter by one <=== LIMITS IT TO 8 NOW!

  }
  /* useless bit of code
  if (counter > 8)
    counter--;
  show(numbers[counter]); // display the current digit
  */
  currDownState = digitalRead(downPin);
  if (prevDownState != currDownState)         // has the state changed from
  { // HIGH to LOW or vice versa
    prevDownState = currDownState;
    if (currDownState == HIGH && counter > minCount)                // If the button was pressed AND not reached limit
      counter--;                             // decrement the counter by one

  }
  /* weird bit
  if (counter < 0)
    counter++;
  */
 
 
 
  showNumber(counter); // display the current digit
}

void showNumber(int number)
{
  /*just a bit weird
  // Use a loop and a bitwise AND to move over each bit that makes up
  // the seven segment display (from left to right, A => G), and check
  // to see if it should be on or not
  for (int j = 0; j <= 7; j++)
  {
    byte toWrite = number & (B10000000 >> j);
    if (!toWrite) {
      continue;
    }                 // If all bits are 0 then no point writing it to the shift register,so break out and
    //move on to next segment.
    shiftIt(toWrite); // Otherwise shift it into the register
  }
  */
  digitalWrite(latchPin, LOW);                  // Set latchPin LOW while clocking bits in to the register
 
  for(byte i = 0; i < numberOfDigits; i++){
    shiftOut(dataPin, clockPin, LSBFIRST, numbers[number % 10]);
    number /= 10;
  }
 
  digitalWrite(latchPin, HIGH);                //set latchPin to high to lock and send data
}
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

gregchill

Thanks for explaining that and showing the sample it works great now

Cheers

Greg

DimmyB

Greg, would you mind posting the final code bro?!

Greetings from Brazil!!

Go Up