4 Digit 7 Segment Display as a 'monitor'

Hi there, long time reader, first time poster.

Arduino Mega 2560

I have a two loop switcher. Pressing pushbutton number one turns on/off a selection of 4 pins. Pressing pushbutton number two, turns on/off a different selection of the same 4 pins. This works well.

However, I am trying to incorporate a 4 digit, 7 segment display to indicate what loop is selected. The two loops are named '6666' and '1234'. THe function works well, and the 4 digit 7 segment display shows the loop selected, however disappears as soon as the momentary pushbutton is released.

Simply, I would like to the 4 digit display to show the loop that is selected (6666 or 1234) until the next selection is made.

Code below.

The easiest way to look at it is when 'indicator1' goes 'high', the 7 segment display should show and keep showing '6666' until it is goes 'low'.

I have tried playing with 'while' and 'while do' to no avail.

Any help would be greatly appreciated.....please be gentle!

#include "SevSeg.h"
SevSeg sevseg; //Initiate a seven segment controller object

// constants won't change. They're used here to
// set pin numbers:
const int button1 = 22;      // button 1 input pin
const int button2 = 23;      // button 2 input pin
const int loop1 = 30;       // loop 1 output pin
const int loop2 = 31;       // loop 2 output pin
const int loop3 = 32;       // loop 3 output pin
const int loop4 = 33;       // loop 4 output pin
const int indicator1 = 38;      // loop 1 indicator
const int indicator2 = 39;      // loop 2 indicator

// variables will change:
int buttonState1 = 0;         // variable for reading button1 status
int buttonState2 = 0;         // variable for reading button2 status 

void setup() {                
    byte numDigits = 4;  
    byte digitPins[] = {2, 3, 4, 5};
    byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13};
    bool resistorsOnSegments = true; 
    // variable above indicates that 4 resistors were placed on the digit pins.
    // set variable to 1 if you want to use 8 resistors on the segment pins.
    sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins, resistorsOnSegments);
    sevseg.setBrightness(90);

  // initialize the Loop pins as an outputs:
  pinMode(loop1, OUTPUT);
  pinMode(loop2, OUTPUT);
  pinMode(loop3, OUTPUT);
  pinMode(loop4, OUTPUT);
  pinMode(indicator1, OUTPUT);
  pinMode(indicator2, OUTPUT);
  
  // initialize the button pins as an inputs:
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);


}
// the loop routine runs over and over again forever:

void loop() {

  buttonState1 = digitalRead(button1);

  if (buttonState1 == HIGH) {
    // turn LED on:
    digitalWrite(loop1, HIGH);
    digitalWrite(loop2, HIGH);
    digitalWrite(loop3, HIGH);
    digitalWrite(loop4, HIGH);
    digitalWrite(indicator1, HIGH);
    digitalWrite(indicator2, LOW);
    sevseg.setNumber(6666);
    sevseg.refreshDisplay(); // Must run repeatedly
  }

  
  buttonState2 = digitalRead(button2);
  if (buttonState2 == HIGH) {
    digitalWrite(loop1, LOW);
    digitalWrite(loop2, HIGH);
    digitalWrite(loop3, LOW);
    digitalWrite(loop4, HIGH);
    digitalWrite(indicator1, LOW);
    digitalWrite(indicator2, HIGH);

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

   }

Welcome to the forum

Instead of detecting when a button is pressed detect when it becomes pressed and execute the appropriate code to output to the display

See the StateChangeDetection example in the IDE

says it all.
you must call this function in loop - not just if the button is pressed

@milefire666

As suggested by @noiasca, your loop() function needs to be re-arranged as follows:

void loop()
{
  buttonState1 = digitalRead(button1);

  if (buttonState1 == HIGH)
  {
    // turn LED on:
    digitalWrite(loop1, HIGH);
    digitalWrite(loop2, HIGH);
    digitalWrite(loop3, HIGH);
    digitalWrite(loop4, HIGH);
    digitalWrite(indicator1, HIGH);
    digitalWrite(indicator2, LOW);
    sevseg.setNumber(6666);
    // sevseg.refreshDisplay(); // Must run repeatedly
  }
  buttonState2 = digitalRead(button2);
  if (buttonState2 == HIGH)
  {
    digitalWrite(loop1, LOW);
    digitalWrite(loop2, HIGH);
    digitalWrite(loop3, LOW);
    digitalWrite(loop4, HIGH);
    digitalWrite(indicator1, LOW);
    digitalWrite(indicator2, HIGH);
    sevseg.setNumber(1234);
    // sevseg.refreshDisplay(); // Must run repeatedly
  }
  sevseg.refreshDisplay(); // Must run repeatedly
}

Now that I see this it makes so much sense!!! Face palm moment.

Thank you! Your support is greatly appreciated.

Nailed it!! So much clearer now. Thank you.

Doing it the hard way then, using Arduino pins directly connected to the display. :roll_eyes:

Note that unless using transistors to drive the digits, the segment resistors must be no less than 1k.

The hard way seemed simple enough, but yes, a whole lot of wires (= potential for things to go wrong!).

I’d love to see the easy way!!! Please feel free to share suggestions….but please keep It simple. I’m very new to this!!!


Requires the TM1637 library in the IDE.

Mega 2560 is unnecessary!

I prefer the displays with HT16K33 for I2C.
They are also available in 14 segment variants.

In case if the sketch uses already SPI, I use MAX7219 . The displays on the variants with green PCB are using sockets and can be changed with other colors.

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