Please help with Seven-Segment Display

Good afternoon,

Thanks in advance for the help.

For my final project, I have to use a seven-segment display to create a ‘slot machine’. A four-digit seven-segment display has to show a pattern while a button is being pressed (the numbers are ‘spinning’) and land on a random number when the button is released.

I’m using a library that I found online, which is allowed for this project, to write the values to the four digit seven-segment display.

Initially, I had the display working just with the ‘spinning’ animation, but no button or anything.

Implementing the button and the random numbers has caused the display to not show anything

I have a few specific questions:

  • Am I doing something wrong when I pass the variables between functions?

  • Why does adding a delay(); after the refreshDisplay(); make it stop working?

  • Am I doing something wrong when I read the button?

  • Am I writing the four-digit random number to the display correctly?

#include <SevSeg.h>

SevSeg sevseg;                                                                //initiating a seven-segment controller object

int button = 1;
int prev_button = 1;
long digit = 0000;

void setup()

{
  byte numDigits = 4;                                                          //telling the library that there are four seven-segment characters

  byte digitPins[] = {2, 3, 4, 5};                                             //assigning the decimal points as such

  byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13};                           //assigning the character pins as such

  sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins);             //telling the library that we're using common cathode, and to hang onto the decimal points, and the four characters

  sevseg.setBrightness(90);                                                    //if the brightness is too high or resistance is too low, it will flicker (I used 200-ohm resistors)
}

int buttonread(int button)
{
  int button_input = digitalRead(0);
  button = button_input;
  return button;
}

int rolling(int button)

{
  sevseg.blank();

  while (button == 0)
  {
  sevseg.blank();
  sevseg.setChars("-   ");
  sevseg.refreshDisplay();
  sevseg.setChars(" -  ");
  sevseg.refreshDisplay();
  sevseg.setChars("  - ");
  sevseg.refreshDisplay();
  sevseg.setChars("   -");
  sevseg.refreshDisplay();
  sevseg.setChars("  - ");
  sevseg.refreshDisplay();
  sevseg.setChars(" -  ");
  sevseg.refreshDisplay();
  }
}

long result(long digit)
{
  digit = random(0000,9999);
  return digit;

}

void loop()

{
int buttonread(int button);
int rolling(int button);
long result(long digit);
sevseg.setNumber(digit);

}

Why have you got function prototypes in loop()?

AWOL:
Why have you got function prototypes in loop()?

I thought I needed to call each function in the loop for them to keep running forever. How is it supposed to be done?

But you're not calling the functions, just providing prototypes.

Try working through some of the supplied examples.

AWOL:
Try working through some of the supplied examples.

Do you mean the native libraries? Do you know which one?

So, would I be calling the function if I said something like

int buttonreadfun = int buttonread(int button);[\code]

No, that still looks a lot like a function prototype, behind an assignment.
What did the compiler say about it?

AWOL:
No, that still looks a lot like a function prototype, behind an assignment.
What did the compiler say about it?

The compiler said "expected primary-expression before 'int'".

Do you know what the reference page about functions means by "To call our function we just assign it to a variable."?

audiojulie:
Do you know what the reference page about functions means by "To call our function we just assign it to a variable."?

No, I'm afraid I don't know what that means. It looks a bit trite to me.
Where is that written?

AWOL:
No, I'm afraid I don't know what that means. It looks a bit trite to me.
Where is that written?

Have added 'trite' to my lexicon.

On the Function Declaration reference page of this site. I'm sure I'm misunderstanding what they're trying to say, but I'm not sure how.

There's an example of a function being called on that page. Here it isk = myMultiplyFunction(i, j);

Can you see the difference between it and your attempt?

audiojulie:
The compiler said “expected primary-expression before ‘int’”.

Do you know what the reference page about functions means by “To call our function we just assign it to a variable.”?

They are trying to say, “To use the result returned by the function we just assign it to a variable.”

AWOL:
There's an example of a function being called on that page. Here it isk = myMultiplyFunction(i, j);

Can you see the difference between it and your attempt?

I think so, does that mean I should do it like this?

buttonreadfn = buttonread(button);

Yes.

But....

int buttonread(int button)
{
  int button_input = digitalRead(0);
  button = button_input;
  return button;
}

could be simplified

int buttonread()
{
  return digitalRead(0);
}

Good call. Okay, I’ve simplified my code and I also moved the button from digital pin 0 to digital pin 1. There’s still nothing showing up on the display. I feel like I’m missing a step, but I’m not sure how to figure out what step. Do you think I have a problem passing the variables between functions? Or maybe reading the button?

#include <SevSeg.h>

SevSeg sevseg;                                                                //initiating a seven-segment controller object

int button = 1;
long digit = 0000;
int rollingfn = 0;
long resultfn = 0000;

void setup()

{
  byte numDigits = 4;                                                          //telling the library that there are four seven-segment characters

  byte digitPins[] = {2, 3, 4, 5};                                             //assigning the decimal points as such

  byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13};                           //assigning the character pins as such

  sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins);             //telling the library that we're using common cathode, and to hang onto the decimal points, and the four characters

  sevseg.setBrightness(90);                                                    //if the brightness is too high or resistance is too low, it will flicker (I used 200-ohm resistors)
}

int buttonread()
{
  return digitalRead(1);
}

int rolling(int button)

{
  sevseg.blank();

  while (button == 0)
  {
  sevseg.blank();
  sevseg.setChars("-   ");
  sevseg.refreshDisplay();
  sevseg.setChars(" -  ");
  sevseg.refreshDisplay();
  sevseg.setChars("  - ");
  sevseg.refreshDisplay();
  sevseg.setChars("   -");
  sevseg.refreshDisplay();
  sevseg.setChars("  - ");
  sevseg.refreshDisplay();
  sevseg.setChars(" -  ");
  sevseg.refreshDisplay();
  }
}

long result(long digit)
{
  digit = random(0000,9999);
  return digit;
}

void loop()

{
  button = buttonread();
  rollingfn = rolling(button);
  resultfn = result(digit);
  sevseg.setNumber(digit);
}

Your first three lines in setup are declarations - that the rest of the program will never see.

Move them above ( outside setup), that will help a lot.

lastchancename:
Your first three lines in setup are declarations - that the rest of the program will never see.

Move them above ( outside setup), that will help a lot.

Okay, I moved them to outside of the setup function. I also added a display refresh at the end. Now, a zero is displayed on the least significant digit, but it still does not respond to the press of a button.

Do you think the while loop that I used for the spinning animation could be the problem? Am I missing a step in setting up the button input?

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you post a picture of your project so we can see your component layout?

Thanks.. Tom.. :slight_smile:

Hi,
Looking at your code, you are using digital pin1 as your button, not a good thing to do as this is one of the progrmming pins.

Use pin2 and see what happens.

Can you please tell us your electronics, programming, Arduino, hardware experience?

Thanks… Tom. :slight_smile:

TomGeorge:
Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you post a picture of your project so we can see your component layout?

Thanks.. Tom.. :slight_smile:

This is a diagram made by someone else. The only differences in my circuit is that the yellow and purple wires are switched (it said they were supposed to be) and I have a pushbutton connected to ground on one end and digital pin 1 on the other end.

When I tried simply writing specific characters to the display, it worked perfectly, but I think my issue is either somewhere in the button function or somewhere in the random number function?

My physical project is not very organized, but if you still need a photo after looking at the block diagram, I can take the circuit somewhere with better lighting to get a picture.

Digital pins 2 through 13 are all currently taken up by the display. :frowning:

As for my experience, this is the final project for my first ever programming class. I have much more experience in solid state electronics, analog devices, and systems integration, and a basic understanding of digital systems (2 courses worth). And a LOT of knowledge about how to fix tape decks, speakers, patch bays, etc., but I doubt that helps here.

This programming class was kind of all over the place. It was the professor's first time teaching it, so it was an experiment. We've covered digital i/o, analog i/o, data types, operators (boolean, bitwise, etc.), libraries, interrupts (although those confuse me a lot), types of functions, etc. We basically just crashed through most of the things on the Arduino Language Reference page, but not all of them.

edit: My syntax may be wrong, because my professor writes in all capital letters, and he confused this class with the C++ class he was teaching, and accidentally put things in that syntax most of the time. I've tried to check everything against the online references, but if you catch any syntax errors, it would be much appreciated.

Hi,
Then use an analog pin, they can be used as digital.

pinMode(A0, INPUT);
return digitalRead(A0);

If your button pulls the input HIGH, then you will need a 10K resistor from pinA0 to gnd.
This is because the input pins are Hi-Impedance and you cannot leave them open circuit if you are usingthem for HIGH/LOW input.
6vbduK2.jpg.png
Tom… :slight_smile: