Shift Register 74HC595N Questions

I apologize, I got carried away and I was going too fast.
I think maybe what you meant was something like this:

You can see I put a blinking decimal point after the last digit.
I also arranged the digits in the array into forwards order, to help it be less confusing.

Code here:

// define the LED digit patterns, from 0 - 15, plus a blank and decimal point
// 1 = LED on, 0 = LED off, in this order:
//74HC595 pin     Q0,Q1,Q2,Q3,Q4,Q5,Q6,Q7 
byte seven_seg_digits[18] = {
  B11111100,  // = 0
  B01100000,  // = 1
  B11011010,  // = 2
  B11110010,  // = 3
  B01100110,  // = 4
  B10110110,  // = 5
  B10111110,  // = 6
  B11100000,  // = 7
  B11111110,  // = 8
  B11100110,  // = 9
  B11101110,  // = A
  B00111110,  // = B
  B10011100,  // = C
  B01111010,  // = D
  B10011110,  // = E
  B10001110,  // = F
  B00000000,  // = blank
  B00000001   // = decimal point
};
 
// connect to the ST_CP of 74HC595 (pin 9,latch pin)
int latchPin = 9;
// connect to the SH_CP of 74HC595 (pin 10, clock pin)
int clockPin = 10;
// connect to the DS of 74HC595 (pin 8)
int dataPin = 8;

void setup() {
  // Set latchPin, clockPin, dataPin as output
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}
 
// display a number on the digital segment display
void sevenSegWrite(byte digit) {
  // set the latchPin to low potential, before sending data
  digitalWrite(latchPin, LOW);
     
  // the original data (bit pattern)
  shiftOut(dataPin, clockPin, LSBFIRST, seven_seg_digits[digit]);  
 
  // set the latchPin to high potential, after sending data
  digitalWrite(latchPin, HIGH);
}


void loop() {
    // count from 0 to 15
  for (byte digit = 0; digit <= 15; digit++) {
    sevenSegWrite(digit);
    delay(1000);
  }
   
  // show blinking decimal point for 5 seconds
  sevenSegWrite(17);
  delay(1000);
  sevenSegWrite(16);
  delay(1000);
  sevenSegWrite(17);
  delay(1000);
  sevenSegWrite(16);
  delay(1000);
  sevenSegWrite(17);
  delay(1000);
}

The reason you are having weird results with the numbers is that, the way the example code you are working from is written, the array is bass-ackwards. It involves some fiddling to get it to count forwards. In my opinion, this is a flaw which makes this code difficult for a beginner such as yourself to learn from.

I have taken the array and put it forwards so that the pattern for the digit 0 is in array position 0, and so forth for the rest of the digits. The pattern for the digit 5 is in array position 5, and so forth.

In reply #85, you can see my modification of the code. The display counts from up from 0 to F, then shows a blinking decimal point for a few seconds, then starts counting again. You can see where I put a blank and a decimal point at the end of the array for the digit shapes.

1 Like

I've been watching the discussion. If I were to be writing code I'd expect to follow the order as you've written, if for no other reason than to tick them off as I went. Perhaps the maker of the kit is Chinese and that's their logical order.

I will rearrange them as I'd expect to see them (as you've shown them). It bothers me too, but it won't change the execution of the numbering (saying to myself).

You added E and F! Leave segment G and DP to me! I have to learn this Bxxx numbering today, if I'm feeling better.

Thanks! I'll edit the writer notes too, I know it's simple, but everything is practice for me!

Later on, I'm saying, can one upgrade to a more commonly used numbering nomenclature than the B xxx code? Or do the key words of the program go hand-in-hand with that.

Yes.

byte seven_seg_digits[16] = {
  0b11111100,  // = 0
  0b01100000,  // = 1
  0b11011010,  // = 2
  0b11110010,  // = 3
  0b01100110,  // = 4
  0b10110110,  // = 5
  0b10111110,  // = 6
  0b11100000,  // = 7
  0b11111110,  // = 8
  0b11100110,  // = 9
  0b11101110,  // = A
  0b00111110,  // = B
  0b10011100,  // = C
  0b01111010,  // = D
  0b10011110,  // = E
  0b10001110,  // = F
};
1 Like

UNO R3s have an additional power supply. Is it used when using the board to power motors and other high current load devices?

There are other altenatives as well? I have to look them up but I think there are 2 more that don't have "b" in the number at all. This isn't right but something like 0x3f or similar.

No, it's all good-no need to apologize.

I have some other code I tried, can't remember who off the top of my head, but it was pretty cool and it did work on my board. It was like a rabbit run on the LEDs they ran around the perimeter. I'll be combining that code as I go. I'll go back for it to keep it properly cited.

Not today. Today is all Bxxx and 0bxxx.

EDIT: Well not all, I'm going to update the Wokwi sketch to include your changes.

And not very 'sticky'. Adjust all you want, at some point you'll select a large region, move it by a couple of grid increments, and suddenly your wires take on a life of their own. Doesn't affect functionality, but getting things to "look nice" is... ephemeral, at best. Seems to me like the code to "move all this 3 over, 2 up" shouldn't be too hard, but it trips up badly on any wire extending outside of the selected region. It seems to be related to the order in which the line segments appear in the json table (i.e. two lines that have identical shapes were drawn from opposite ends, so moving/stretching/shortening has different results), but I haven't looked deeply at it.
Anyway, quite OT. Back to the Shift Register question, shall we?

I've been having a high 'ole time last few days. I needed the breakthrough. I spent too much time just trying to get a properly working result without a lot of artifact.

I got better with the Wokwi after deleting the breadboard. As you see in my project, I got the lines all colored nicely and layed out in a fairly decent / readable way.

Now I'm at the point of 2 goals.

One is the shift register. shift register concepts of input, output, latch, clock. Once I do that I can think about daisy chaining them. Is this called MOX or multiplexing?

Another is Code, starting in particular by reexamining my bases knowledge, hex Bxxx and Obxxx and others,

I'm going through the shift register datasheet again. that digital time shift wave thingy gave me grief.

I went through the bases theory, thanks to a couple tutorials it was easy and I remembered that I had understood it decades ago. It was easy until the next day when I started writing numbers in different bases and started flubbing them again. So I gotta go back and see where I messed up. It was something about the binary. Oh, now I'm writing, I was trying to convert made up numbers that were impossible for the base.

Anyway gotta study more.

Amazing what one sees upon rereading. Missed this before. Will swapping my resistors from 200 ohm, as called for in the tutorial by the author, to kohm range stabilize the circuit? I've already added a capacitor as recommended at the Vcc.

Have no idea what this means, I'm just a kid.

So, if I want to add it as a stabilizing element as an example for others to see how would I finesse the Wokwi to do that?

200 ohms is not good as a pullup; should not, however, destabilize your project, it's just not 'good practice'. Whatever you have, between 1k and 10k, would be best. I haven't looked in detail at what's going on in your project, but this is unlikely to be the root cause of a problem.
200 ohms, however, IS in a good 'zone' for use to limit current for an LED on an output; are you sure that's not what they were being used for?

That's okay, my reference was to the two signals used for the I2C serial communications port used for talking to other devices, like an I2C LCD, I2C port expander, and the like. Don't worry about it for now.

That's when "undo" can come in handy.

But there's no doubt it makes sense to think first and wire things up once the components are where you want them.

And shift-selecting a group of components and wires afore moving them can be useful. The grid helps.

TBH I'd rather have them keep functioning on the simulation, but it would be nice to have the quality of a real schematic capture program avaiLbe.

And though I've had my problems with the wiring, they don't seem as severe as yours; perhaps being ambidextrous or more left handed really, or just doing things a bit differently has meant for me a few countries worth of pain, not a world. :wink:

When I am being careful and expecting things to be seen and learnt from, I make the sim diagram as real as I can, with Vcc and GND wires that mightn't be necessary and resistors on LEDs and separate Vcc and GND symbols for real power (no, the UNO cannot supply any significant current for servos and stuff). That does still leave little details like 0.1 uF caps out of that picture.

So maybe hope that by a certain point of wrangling with the IDE and attaining some proficiency with the wokwi you have departed Noob Island and are at sea (bad analogy) headed for, um, wherever and little details like that are well known.

The wokwi does simulate both button bouncing and floating inputs, BTW. Button bouncing can be turned off which is often convenient, quicker than dragging out a library or coding a denounce manually just to get on with whatever the experiment is. Similar to using a real microprocessor learning tool that might provide some denounced buttons and driven LEDs and other bits and pieces already to exploit.

I don't see that anyone has changed the loops to be written in the more often seen manner

   for (int index = 0; index < numberOfItems; index++) { // blah blah blah

which is instant recognized as looping over a number of items, arrays starting at zero and all.

Calling stringing shift registers together "daisy chaining' works and eveyone would understand what you mean. Multiplexers are an entirely different solution to another kind of I/O problem, perhaps in the future of your lessons plan, and maybe maybe not something you'll get to.

a7

@crabcrawler
I believe you are still missing the basics of the seven segment display and shift register. I attempted to explain it here

Maybe someone in this thread can explain it better.

odometer added two more elements to your array that will display the letters E and F on your display. E and F complete the 16 characters of the hexadecimal values (0-9 and A-F).

This array data is clocked into the shift register. The data from the array that is clocked into the shift register defines which shift register outputs (Qa - Qh) are HIGH or LOW. Each output (Qa - Qh) is connected to a segment on your display. A 1 (HIGH) on that segment lights the LED. A 0 (LOW) turns it off.

The segments G and DP, on the other hand, are physical LED segments on your display. Go back and look at the two images on the other thread.

I believe more sooner that later you will have that hallelujah moment when it all falls into place and starts making sense. There are a few pieces to this puzzle. You just need to understand what each piece is doing. Stay with it.

The segments A through G and DP are physical LED segments.

I was trying to see why G would be different, totally willing to find that DP was somehow not an equal citizen but the diagram is clear.

a7

Maybe simply blinking 'segment A' would be a good place to re-start.

Now I know. First look I thought i2c was 12c. Thought better of it and searched it. It' a master controller bus port for short distance serial communications with other devices.

1 Like

Good idea!

Do you include the breadboard in your Wokwi designs? The grid works better for me, but my issues could'

I'm also right-brained, left-handed. Use right-handed golf clubs and wield a bat from right or left hand positions, so at least partially ambidextrous.

What's a real schematic capture program? A draw program? Less BETA and more robust?

These issues may have been my internet connection or my laptop isn't as robust at processing as my desktop. I attributed these problems as stemming from using the breadboard in the simulation... it was real finnicky, had freezes, wouldn't release or delete, etc. until I wired direct. Maybe too much memoryt demand on the laptop. Funny, until Wokwi I'd contemplated actually soldering to nails on a board oldstyle.