programming

hallo specialists....

I have programmed my UNO witch the sketch as in the attach, an up-down counter.
But...I want to save the last value (figure) on the display after switching off the power.
So the last figure appears when I switch on again.
There is an EEPROM in the examples and I tried this sketch to implement in the up-down counter program, but it does not work ....
Can anybody help me to get this done.
Sorry to say, but I am not a ICTer , so please keep the answer simple :confused:
Thanks for reading
greeitings
Bouke
zhtech@kpnmail.nl
www.zhtech.nl

up_-_down_counter_1.1.ino (2.29 KB)

include the EEPROM lib : #include <EEPROM.h>
set an address for storage : int addr = 0; // any addr will do

byte count;

in setup(): count=EEPROM.read(addr); // startup by getting the last number

in loop(): (ONLY when count change) : EEPROM.write(addr,count);

!!!!! Be aware that the EEPROM wear out around 100k writes, so dont write if no change

Compiled but untested (stress your own EEPROM).

// 2 switches to make a count increment and decrement

byte* const EEADR = 0;

const byte sw1 = 8;  // Connect pin 8 to SW1
const byte sw2 = 9;  // Connect pin 9 to SW2
int count = 0;

#include <LiquidCrystal.h>
// instanciate to RS Enable D4 D5 D6 D7
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

void setup()
{
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print(F("Up-Down Counter"));
  // Declare Switches as INPUT to Arduino
  pinMode(sw1, INPUT_PULLUP);
  pinMode(sw2, INPUT_PULLUP);
  Serial.begin(9600); // communicatie met PC
  count = eeprom_read_byte(EEADR+1);
  count <<= 8;
  count |= eeprom_read_byte(EEADR);
}

void loop()
{
  bool changed = false;
  if (digitalRead(sw1) == LOW)  // if SW1 is pressed perform action described in loop
  {
    changed = true;
    count++;                    // Increment Count by 1
    lcd.setCursor(0, 1);
    lcd.print(count);
    delay(100);
  }
  if (digitalRead(sw2) == LOW)   // if SW2 is pressed perform action described in loop
  {
    changed = true;
    count--;                     // Decrement Count by 1
    if (count < 0)
      count = 0;
    lcd.setCursor(0, 1);
    lcd.print(count);
    delay(100);
  }
  if (changed) {
    if (eeprom_read_byte(EEADR) != (byte)count) {
      eeprom_write_byte(EEADR, (byte)count);
    }
    if (eeprom_read_byte(EEADR + 1) != (byte)(count >> 8)) {
      eeprom_write_byte(EEADR + 1, (byte)(count >> 8));
    }
  }
}