Go Down

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

#### gregchill

##### Nov 16, 2016, 05:17 pm
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

#1
##### Nov 16, 2016, 08:57 pm
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

#### gregchill

#2
##### Nov 16, 2016, 10:47 pm

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()
{
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
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

#3
##### Nov 16, 2016, 10:54 pm
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

#4
##### Nov 16, 2016, 10:57 pm
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

#5
##### Nov 16, 2016, 11:00 pm
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

#### PaulRB

#6
##### Nov 17, 2016, 10:00 am
Quote
dozens
Only if the OP want to display the number in duodecimal format!

#### septillion

#7
##### Nov 17, 2016, 10:44 am
Haha, you're right! I'll never trust Google again when in doubt about a word 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

#### gregchill

#8
##### Nov 17, 2016, 11:54 am
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

#9
##### Nov 17, 2016, 12:02 pm
No, you need to edit it
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

#### PaulRB

#10
##### Nov 17, 2016, 12:03 pmLast 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

#11
##### Nov 17, 2016, 12:17 pm
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
*/
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

#### gregchill

#12
##### Nov 19, 2016, 08:14 pm
Thanks for explaining that and showing the sample it works great now

Cheers

Greg

#### DimmyB

#13
##### Nov 29, 2016, 08:25 pm
Greg, would you mind posting the final code bro?!

Greetings from Brazil!!

Go Up