Need help with my code for a 8 digit 7 segment display that increases when a button is pressed

Hi, I am a student building a pinball machine powered by arduino. I have been having trouble with the code that I have put in below with getting my pushbuttons to add a specific amount to the 8 digit display. If anyone knows how to get the buttons to work and increase the numbers on the display or if you see something wrong in the code that is causing me my problems please let me know.

'''
#include "LedControl.h"
#include <ezButton.h>

// Initialise the LedControl library
// pin 7 on DIN,
// pin 6 on Clk,
// pin 5 on LOAD
// number of displays: 1
LedControl lc = LedControl(4, 2, 3, 1);

//int i = 0;
//int button1 8;
//int button2 9;
void setup() {
Serial.begin(9600);
//pinMode (button1, INPUT);
//pinMode (button2, INPUT);
// On startup, the MAX72XX chip is in power-safe mode
// we are waking this chip for device 0 with the shutdown command
lc.shutdown(0, false);

// Set the intensity of the screen on device 0 with a value between 0 and 15
lc.setIntensity(0, 5);

// Clear the display of device 0
lc.clearDisplay(0);
}
int i=0;
// Show a number of max 4 positions, so 0...9999
void printNumber(int number) {
int ones;
int tens;
int hundreds;
int thousands;
int tenThousands;
int hundredThousands;
int millions;
int tenMillions;
int button1 = 8;
int button2 = 9;

// Calculate the number of ones
// we take the remainder when the number is divided by 10

// ones = number % 10;
// Save the number of divides without remainder
// number = number / 10;

// Calculate the number of tens
// we take the remainder when the number is divided by 10

// tens = number % 10;
// number = number / 10;

// Calculate the number of hundreds
// we take the remainder when the number is divided by 10

// hundreds = number % 10;
// number = number / 10;

// thousands = number % 10;
// number = number / 10;

// tenThousands = number % 10;
// number = number / 10;

// hundredThousands = number % 10;
// number = number / 10;

// millions = number % 10;
// number = number / 10;

// tenMillions = number;
if (button1==HIGH){
lc.setDigit(0, 7, tenMillions, false);
lc.setDigit(0, 6, millions, false);
lc.setDigit(0, 5, hundredThousands, false);
lc.setDigit(0, 4, tenThousands, false);
lc.setDigit(0, 3, thousands, false);
lc.setDigit(0, 2, hundreds, false);
lc.setDigit(0, 1, tens, false);
lc.setDigit(0, 0, ones, false);
i=i+10;

}
else{
(button1==LOW);
}
// Show the digits on the display
lc.setDigit(0, 7, tenMillions, false);
lc.setDigit(0, 6, millions, false);
lc.setDigit(0, 5, hundredThousands, false);
lc.setDigit(0, 4, tenThousands, false);
lc.setDigit(0, 3, thousands, false);
lc.setDigit(0, 2, hundreds, false);
lc.setDigit(0, 1, tens, false);
lc.setDigit(0, 0, ones, false);
}

void printNumber2(long number) {

// Array of 8 bytes for each postion on the LED display
byte digits[8] = {0, 0, 0, 0, 0, 0, 0, 0};

// Loop over each position to determine the number to show
for (int i; i < 8; i++) {
digits[i] = number % 10;
number = number / 10;
}

// Loop over all LED positions
for (int i; i < 8; i++) {
// Show digit on position i on the LED display
lc.setDigit(0, i, digits[i], false);
}

}

void printNumber3(long number) {

// Array of 8 bytes for each postion on the LED display
byte digits[8] = {0, 0, 0, 0, 0, 0, 0, 0};

// Loop over each position to determine the number to show
for (int i; i < 8; i++) {
digits[i] = number % 10;
number = number / 10;
}

// True when we've found the first non-zero digit
bool firstNonZeroFound = false;

// Loop backwards from 7 to 0
for (int i = 7; i >= 0; i--) {

  // If the number is not equal to 0
  if(digits[i] != 0) {
    firstNonZeroFound = true;
  } 

  // If the first non-zero number is found show this on the LED display
  if (firstNonZeroFound) {
    lc.setDigit(0, i, digits[i], false);
  }

}

}

void loop() {
lc.clearDisplay(0);
printNumber(i);
i++;
delay(100);
}
'''

1. What Arduino are you using?
2. Are you using CA-type or CC-type display devices?
3. At which DPin you wish connect your push button?
4. What is the initial value on the display unit? 00000000 or else?
5. How much to be added with the current value of the display unit when push is pressed once?

I have a plan to show you how the display unit can be driven using SevSeg.h Library.

you gave button1 the value 8.
That's never going to equal HIGH.

Not sure what this is meant to be/do.

Please remember to use code tags when posting code

This is not how you construct for loop

Oops

Edit: pipped at the post!

//pinMode (button1, INPUT);
//pinMode (button2, INPUT);

Luckily, pins default to input.

the display shows 00000000 when I upload the code which is what I want at the start. I am also using the mega 2560.

You are just lucky it shows all zeros

Please start by reading the [instructions for posting]( how to post code), then edit your code post (use the "pencil" icon below the post) to use "code" tags so we can read it properly.

Note you need to use "Auto-Format" (Ctrl-T) on the code in the IDE first.

Can you show a diagram of the way you have connected your 8-digit display unit and the switch with MEGA?

I had 5 questions in Post-2; you had answered only one!

An int can only handle numbers up to 32767. I think that you want your number variable to be a long, which can go up to a little over 2 billion.

Perhaps, the OP wants to go from 00000000 (0x00000000) to 99999999(0x05F5E0FF); so, as you have suggested a long type is suffice (-2147483648 to 2147483647 = 0x80000000 to 0x7FFFFFFF).

Op seems to be MIA! :worried:

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