Requesting for code of increment and decrement and storage of data to set digits

So I've been working on a little project to make my segment display start at a certain number and then increment or decrement with a simple button press.

Sir we are using Arduino Uno we have done with coding for increment of digits using 7 segment LED
we are interfacing Arduino Uno with shift register 75HC595. Please help us in programming of decrement of
digits using two buttons and storage of data even after switching on and off the power supply everytime.

The program for increment of the digits is attached below.Please help us with the program for decrement and storage.

Code for Increment.ino (3.84 KB)

Use EEPROM to store the counter each time you change it and read the counter value from EEPROM in setup().

Sir, Can you please send me a code using EEPROM Library with counter.

Code for increment and decrement given in attachment

Thanks!

switch_1_and_2_proper.ino (4.06 KB)

Can you please send me a code using EEPROM Library with counter.

What, EXACTLY, do you want to store in EEPROM? Why? When?

What, EXACTLY, do you want to read from EEPROM? When?

No one can just write code to use a library without knowing what you want the library to do.

#include <EEPROM.h>


// counter button definition
#define button1    A0
#define button2    A1


// shift register pin definitions
#define clockPin  7   // clock pin
#define dataPin   6   // data pin


// common pins of the four digits definitions
#define Dig1    5
#define Dig2    4
#define Dig3    3
#define Dig4    2


// variable declarations
byte current_digit;
int  count = 0;
const int COUNTER_EEPROM_ADDRESS = 0;
void disp(byte number, bool dec_point = 0);


void setup()
{
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(Dig1, OUTPUT);
  pinMode(Dig2, OUTPUT);
  pinMode(Dig3, OUTPUT);
  pinMode(Dig4, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);


  disp_off();  // turn off the display


  // Timer1 module overflow interrupt configuration
  TCCR1A = 0;
  TCCR1B = 1;  // enable Timer1 with prescaler = 1 ( 16 ticks each 1 µs)
  TCNT1  = 0;  // set Timer1 preload value to 0 (reset)
  TIMSK1 = 1;  // enable Timer1 overflow interrupt


  // Get the latest count from EEPROM
  EEPROM.get(COUNTER_EEPROM_ADDRESS, count);
  if (count > 9999 || count < 0)
    count = 0;
}


ISR(TIMER1_OVF_vect)   // Timer1 interrupt service routine (ISR)
{
  disp_off();  // turn off the display


  switch (current_digit)
  {
    case 1:
      disp(count / 1000);   // prepare to display digit 1 (most left)
      digitalWrite(Dig1, LOW);  // turn on digit 1
      break;


    case 2:
      disp( (count / 100) % 10 );   // prepare to display digit 2
      digitalWrite(Dig2, LOW);     // turn on digit 2
      break;


    case 3:
      disp( (count / 10) % 10 );   // prepare to display digit 3
      digitalWrite(Dig3, LOW);    // turn on digit 3
      break;


    case 4:
      disp(count % 10);   // prepare to display digit 4 (most right)
      digitalWrite(Dig4, LOW);  // turn on digit 4
  }


  current_digit = (current_digit % 4) + 1;
}


// main loop
void loop()
{
  if (digitalRead(button1) == 0)
  {
    count++;  // increment 'count' by 1
    if (count > 9999 || count < 0)
      count = 0;
    EEPROM.put(COUNTER_EEPROM_ADDRESS, count);
    delay(200);  // wait 200 milliseconds
  }
  else if (digitalRead(button2) == 0)
  {
    count--;  // increment 'count' by 1
    if (count > 9999 || count < 0)
      count = 0;
    EEPROM.put(COUNTER_EEPROM_ADDRESS, count);
    delay(200);  // wait 200 milliseconds
  }
}


void disp(byte number, bool dec_point)
{
  switch (number)
  {
    case 0:  // print 0
      shiftOut(dataPin, clockPin, MSBFIRST, 252 | !dec_point);
      digitalWrite(clockPin, HIGH);
      digitalWrite(clockPin, LOW);
      break;


    case 1:  // print 1
      shiftOut(dataPin, clockPin, MSBFIRST, 96 | !dec_point);
      digitalWrite(clockPin, HIGH);
      digitalWrite(clockPin, LOW);
      break;


    case 2:  // print 2
      shiftOut(dataPin, clockPin, MSBFIRST, 218 | !dec_point);
      digitalWrite(clockPin, HIGH);
      digitalWrite(clockPin, LOW);
      break;


    case 3:  // print 3
      shiftOut(dataPin, clockPin, MSBFIRST, 242 | !dec_point);
      digitalWrite(clockPin, HIGH);
      digitalWrite(clockPin, LOW);
      break;


    case 4:  // print 4
      shiftOut(dataPin, clockPin, MSBFIRST, 102 | !dec_point);
      digitalWrite(clockPin, HIGH);
      digitalWrite(clockPin, LOW);
      break;


    case 5:  // print 5
      shiftOut(dataPin, clockPin, MSBFIRST, 182 | !dec_point);
      digitalWrite(clockPin, HIGH);
      digitalWrite(clockPin, LOW);
      break;


    case 6:  // print 6
      shiftOut(dataPin, clockPin, MSBFIRST, 190 | !dec_point);
      digitalWrite(clockPin, HIGH);
      digitalWrite(clockPin, LOW);
      break;


    case 7:  // print 7
      shiftOut(dataPin, clockPin, MSBFIRST, 224 | !dec_point);
      digitalWrite(clockPin, HIGH);
      digitalWrite(clockPin, LOW);
      break;


    case 8:  // print 8
      shiftOut(dataPin, clockPin, MSBFIRST, 254 | !dec_point);
      digitalWrite(clockPin, HIGH);
      digitalWrite(clockPin, LOW);
      break;


    case 9:  // print 9
      shiftOut(dataPin, clockPin, MSBFIRST, 246 | !dec_point);
      digitalWrite(clockPin, HIGH);
      digitalWrite(clockPin, LOW);
  }
}


void disp_off()
{
  digitalWrite(Dig1, HIGH);
  digitalWrite(Dig2, HIGH);
  digitalWrite(Dig3, HIGH);
  digitalWrite(Dig4, HIGH);
}


// end of code.

Thank You So Much johnwasser sir for this code!

It's Working :slight_smile: