Anyone have advice on using an Arduino as a multiplexer for a 4 digit display?

I believe that when you use an Arduino for a device like a 4 digit display (I am using 3461bs-1) it is acting as a multiplexer. By all means correct me on that assumption. I am using sevseg library, which may not be the best library.

I have been at this for a while because it is the first time using something like this. It is awkward due to the use of delays, which are not so good with this type of thing obviously (in fact delays are probably bad in many applications). I am just experimenting so this is a very basic concept. I just want to increment display by 1 digit. The best I have worked out so far is:

#include "SevSeg.h"
SevSeg sevseg;
int inPin = 14;
int displayNum = 1;

int latchState = 0;

void setup() {

  pinMode(inPin, INPUT_PULLUP);

  byte numDigits = 4;
  byte digitPins[] = { 2, 3, 4, 5 };
  byte segmentPins[] = { 6, 7, 8, 9, 10, 11, 12, 13 };

  bool resistorsOnSegments = false;    // 'false' means resistors are on digit pins
  byte hardwareConfig = COMMON_ANODE;  // See README.md for options
  bool updateWithDelays = false;       // Default 'false' is Recommended
  bool leadingZeros = true;            // Use 'true' if you'd like to keep the leading zeros
  bool disableDecPoint = false;        // Use 'true' if your decimal point doesn't exist or isn't connected. Then, you only need to specify 7 segmentPins[]

  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
               updateWithDelays, leadingZeros, disableDecPoint);
  sevseg.setBrightness(90);
}

void setNum(int numVal) {

  sevseg.setNumber(numVal, 0);
}

void loop() {
  if (digitalRead(inPin) == LOW && !latchState) {
    delay(100);
    latchState = 1;

  } else if (digitalRead(inPin) == HIGH && latchState) {
    displayNum++;
    delay(100);
    latchState = 0;
  }

  setNum(displayNum);
  sevseg.refreshDisplay();
}

It is a bit intermittent, and it also makes the display flicker because of those dreaded delays.

Some assumptions I have come up with:

  1. Maybe use one Arduino solely for the purpose of driving the display.
  2. Use an external Arduino as the control for switches etc.
  3. Consider a different library.
  4. Consider using a dedicated device for the display.

Any advice would be appreciated.

Use a button library that handles the debouncing without delays.

1 Like

Thank you. Do you have any good suggestions for which one? I have a feeling that sevseg may not be the best, but it seems to pop up quite a lot in searches, and tutorials. Also, I found this interesting with an example provided by the sevseg docs:

void loop() {
  static unsigned long timer = millis();
  static int deciSeconds = 0;
  if(digitalRead(inPin) == LOW){
    if (millis() - timer >= 500) { // I worked out I have to keep these the same...
      timer += 500; //<------ these ones!
      deciSeconds++; // 100 (or whatever) milliSeconds is equal to 1 deciSecond (I have played about with the timing so this value is false!)
      
      if (deciSeconds == 10000) { // Reset to 0 after counting for 1000 seconds.
        deciSeconds=0;
      }
      sevseg.setNumber(deciSeconds, -1);
    }
  }

  sevseg.refreshDisplay(); // Must run repeatedly
}

Use the library manager. Pick a button library. Try it out. If it doesn't suit you, try another.

Eventually you may take a particular library and modify it to suit your custom needs and then you'll use that going forward. It's what I tend to do.

And don't worry about SevSeg. it has nothing to do with any buttons. It just displays digits. But the same advice applies there. It doesn't suit you? Try another. Maybe in the fullness of time, modify an off the rack one to be a custom fit. Or maybe you'll be happy with the one you end up using as it is.

1 Like
  • Always show us a good schematic of your proposed circuit.
    Show us good images of your ‘actual’ wiring.
    Give links to components.

Switches can be debouched by simply scanning them every 50ms while looking for a change in state.

Never use delay(. . .) unless it’s for a very very short period.

2 Likes

Have you tried the example sket ch in the library called SevSeg_Counter. It does

This example demonstrates a very simple use of the SevSeg library with a 4
digit display. It displays a counter that counts up, showing deci-seconds.

2 Likes

No-one commented on this idea, so I will, because it's an important point: Definitely a bad idea!

Using multiple Arduino to solve a problem like this would, in practice, be far more complex and difficult than fixing the problem you currently have.

Even the most basic Arduino models run at 16MHz, making them more powerful than many desk-sized computers from the 1960s-1970s. It would be so wasteful to use 2 such computers to solve such a simple problem :wink:

1 Like

Yes, I posted a little bit of that code in post #3. That is actually a snippet of that exact code you mentioned. I am just going to try to wrap my head around that and, and then reformat the code to work as a switch, instead of just counting. That was why I mentioned that code snippet.

That seems a much more sensible way to avoid delays.

I, for one, am not clear on what you are trying to accomplish, but I see some clues. Perhaps check several of the tutorials on state machines and several of the tutorials on doing many things at once.

1 Like

This video should help: https://www.youtube.com/watch?v=iZI1GjCvIiw

1 Like

OK, thank you, and I agree with you.

To everyone, thank you for the responses. I am going to go with the idea of using/ investigating established switching libraries. I have also come across millis() as a great alternative to delay() (e.g. my post #3 example). To avoid reinventing the wheel though, the switching libraries seem a very good path to take.

What this comes down to is that this was a really embarrassingly simple question. I didn’t even need to mention that I was using the 4 digit display tbh. I just did a search, picked a library, checked the git page, and then pasted the library name into the IDE library manager, and followed the instructions. Piece of cake!

I used to do web programming, using frameworks, and databases etc, and I was using libraries all the time. I simply need to use that same frame of mind with Arduino programming from now on.

Thank you all again, and we can consider this closed :grinning_face: