How to store a starting number that my current number can reset to

// Libraries for OLED
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "modified_font.h"

// Define parameters
#define SCREEN_WIDTH 128 
#define SCREEN_HEIGHT 64 
#define OLED_RESET     -1 
#define SCREEN_ADDRESS 0x3C 
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);


// Define Variables

int upPin = 8;
int dnPin = 7;
int shotPin = 4;
const int selPin = 5;

bool dissable = false;

int strtValue = 0;


char msg1[] = "Set your ammo count:";
char msg2[] = "READY";

int buttonState;
int lastButtonState = LOW;//when using INPUT_PULLUP the pin values are inversed, in this case LOW will mean HIGH etc.

unsigned long lastDebounceTime = 10;
unsigned long debounceDelay2 = 2000;
unsigned long debounceDelay = 1;
 



void setup()// intialize screen
  { 
    display.begin (SSD1306_SWITCHCAPVCC, 0x3C);
    pinMode(upPin, INPUT_PULLUP); 
    pinMode(dnPin, INPUT_PULLUP);
    pinMode(selPin, INPUT_PULLUP);
    pinMode(shotPin, INPUT_PULLUP);
    
    display.clearDisplay();
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0,0);
    display.println(msg1);
  }


void loop() // call variables and display to screen
  {
    
    int i = strtValue; 
      
    if(strtValue <= 0)
      {
        strtValue = 0; 
      }
    
    
    display.drawRoundRect(7, 53, 114, 7, 3, WHITE);
    display.fillRoundRect(7, 53, i, 7, 3, WHITE);
    display.drawRoundRect(1, 16, 127, 48, 4, WHITE); 
    
    
    increaseParameter();
    decreaseParameter();
    selectParameter();
    shotParameter(); 
    //display.setFont(&Orbitron_Medium_12); display.setTextSize(4);

    display.setTextSize(4);
    display.setTextColor(WHITE);
    display.setCursor(42,16);
    display.println(strtValue);
    display.display();
       
  }

void increaseParameter()
  {
    int reading = digitalRead(upPin);
      if (reading != lastButtonState)
      delay(30);
        {
          lastDebounceTime = millis();
        }
      if((millis() - lastDebounceTime) < debounceDelay)  
        {
            if(reading != buttonState)
              {
                buttonState = reading;
                if(buttonState == LOW)
                  {
                  display.clearDisplay();                         
                  strtValue++; 
                  }
              }  
          }
      lastButtonState = reading;
  }

void decreaseParameter()
  {
    int dnReading = digitalRead(dnPin);
      if (dnReading != lastButtonState)
      delay(30);
        {
          lastDebounceTime = millis();
        }
      if((millis() - lastDebounceTime) < debounceDelay)
        {
          if(dnReading != buttonState)
            {
              buttonState = dnReading;
              if(buttonState == LOW)
                {
                display.clearDisplay();
                strtValue--;
                }
            }
        }
      lastButtonState = dnReading;
  }

void selectParameter()
  {
    int selReading = digitalRead(selPin); 
      if (selReading != lastButtonState)
      delay(30);
        {
          lastDebounceTime = millis();
        }
      if((millis() - lastDebounceTime) < debounceDelay)
        {
          if(selReading != buttonState)
            {
              buttonState = selReading;
              if(buttonState == LOW)//button pressed
                {
                upPin = dissable;
                dnPin = dissable;
                
                display.clearDisplay();

                display.drawRoundRect(1, 16, 127, 48, 4, WHITE);  
                               
                display.setTextSize(2);
                display.setTextColor(WHITE);
                display.setCursor(42,0);
                display.println(msg2);
                
                display.setCursor(40,16);
                display.setTextSize(4);
                display.setTextColor(WHITE);
                
                display.println(strtValue);
              }
          }
        lastButtonState = selReading;
     }
   }
  

void shotParameter()
  {
    int shotReading = digitalRead(shotPin);
      if (shotReading != lastButtonState)    
      delay(100);
        {
          lastDebounceTime = millis();
        }
      if((millis() - lastDebounceTime) < debounceDelay)
        {
          if(shotReading != buttonState)
            {
              buttonState = shotReading;
              if(buttonState == LOW)
                {
                display.clearDisplay();
                strtValue--;
                delay(1);
                }
            }
        }
      lastButtonState = shotReading;
  }

Hello everyone this is my first arduino uno project and overall introduction to coding as a whole so my experience is limited. Im currently designing a digital ammo counter using a 128 x 64 OLED display with the Adafruit_ssd1306 library. Right now im trying to figure out how to detect when a button is pressed for longer than a second and upon release of said button for that to reset my starting number to a specified max number which is selected by the user on startup. I have my increase, decrease, select, reset and bullet shot functions all working but now i'm trying to get the selected number to reset back to max ammo. Any info/ suggestions are much appreciated

Looking at the code I will take a SWAG: When you start save the max ammo in a variable, then when reseting copy that variable to the counting variable. UsedAmmo=StartAmmo would work.

Which button needs to be held for more than 1 second? To accomplish this, you will have to record the time [millis()] when said button is first pressed and then calculate elapsed time when said button is released.

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