SOLVED Problem changing number displayed in seven segment display

My problem was solved by johnwasser:

if (HrUpState != lastHrUpState)
{
lastHrUpState = HrUpState; //// YOU FORGOT THIS LINE
if (HrUpState == HIGH)
{

First time post so I hope I do it correctly.

I've written code that changes the number displayed in a 2 digit seven segment display every second. The number increases by 1 from 1-12 and then resets to 1. Eventually I will have it change every hour.

What I'd like to add to this code is a way to increase the number displayed each time a button is pressed. This will allow me to set the display to the current hour of the day.

What happens is that when the button is pressed, the seven segment display crashes and has only the middle horizontal segment of each digit lit up (- -).

The crash is occurring within the HrAdj() function. I've made a couple of additional notes there.

Thank you for whatever assistance you can offer me.

Michael

Here is my code:

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

static int Hr = 1; // Current Hour

int HrUpPin = 41;
int HrDnPin = 43;
int HrUpState = 0;
int HrDnState = 0;
int lastHrUpState = 0;
int lastHrDnState = 0;

void setup() {
byte numDigits = 2;
byte digitPins[] = {10, 11};
byte segmentPins[] = {3, 9, 8, 6, 7, 4, 1, 2};
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 = false; // 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

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

}

void loop() {
HrAdj();
HrDisp();
}

void HrDisp(){ //Displays the hour

static unsigned long timer = millis(); // timer is how long between number change

sevseg.setNumber(Hr);
if (millis() - timer >= 1000) // number of seconds between number change
{
timer += 1000; //sets timer to equal millis()

Hr++;

if (Hr == 13) { // Reset to 0 after counting for 10 seconds.
Hr=1;
}
}

sevseg.refreshDisplay(); // Must run repeatedly

}

void HrAdj(){ // Adjusts the Hour

//Getting the reads from the buttons
HrUpState = digitalRead(HrUpPin);
HrDnState = digitalRead(HrDnPin);

//Detecting button press and getting the button status
//Do this for the button up

if (HrUpState != lastHrUpState)
{
if (HrUpState == HIGH)
{

++Hr; // THIS IS WHERE I'M CRASHING. IF INSTEAD I SIMPLY SAY SOMETHING LIKE "Hr = 33", THE DISPLAY SHOWS "33" AND CONTINUES INCREASING BY ONE EVERY SECOND.

}
}

//Do this for the button down

if (HrDnState == HIGH)
{

}
}
/// END ///

Welcome to the Forum. Please read these two posts:

General Guidance and How to use the Forum
and
Read this before posting a programming question ...
You may also find useful information that would answer your question here:
Useful links - check here for reference posts / tutorials

It is important to provide as much of the information that is needed to solve your problem as you can, in your first posts. The forum link above has guidelines for posting in a standard way that makes it easiest for people to provide you with useful answers. Making an effort to do this will greatly increase the number and quality of helpful responses that you get.

In this case, the problem is that you have posted code without using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The "Code: [Select]" feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing.

If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower right corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

Please remove any blank lines and auto format using ctrl-T in the IDE to space the code elements in a standard, more readable way.

The press of a switch is detected by the transition from off to on.

If you are simply testing if the switch is on, chances are that switch will be tested hundreds or thousands of times during the normal 'human time' press as the CPU run much faster.

  if (HrUpState != lastHrUpState)
  {
    lastHrUpState = HrUpState;  //// YOU FORGOT THIS LINE
    if (HrUpState == HIGH)
    {

Thank you so much, John Wasser!

Thank you, aarg. I will try to post more correctly the next time.