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 ///