2

I made a part of code to change the temperature limit for alarm system. On warm days user have to change the temperature limit with buttons and OLED display.

The question is: How can the variable templimit keep its last setup value after many restarts(for many reasons)?

This is the code:

const int  buttonPin = 8;    // up
const int  buttonPin1 = 3;    // down
const int  buttonPin2 = 2;    // enter

int buttonPushCounter;   // counter for the number of button presses
int buttonState;;  // up
int buttonState1;;  // down
int buttonState2;;  // enter
int templimit; // temperature limit for alarm

void setup() {
  Serial.begin(9600);
  Serial.print("Update temperature limit if not wait 10 sec time out");
  updatevalue();
  Serial.print("Done");

}


void loop() {
// do nothing
}

int updatevalue(void) {
  int timenow;
  int timepassed;
  timenow = millis();
  // this loop is just for 10 sec time out
  while (buttonState == LOW and buttonState1 == LOW and buttonState2 == LOW) { 
    buttonState2 = digitalRead(buttonPin2); // enter
    buttonState = digitalRead(buttonPin); //up
    buttonState1 = digitalRead(buttonPin1); // down
    timepassed = (millis() - timenow);
    if (timepassed >= 10000) 
    return 0;
  }
  while (buttonState2 != HIGH) { // do this until enter is pressed

    buttonState2 = digitalRead(buttonPin2); // enter
    buttonState = digitalRead(buttonPin); //up
    buttonState1 = digitalRead(buttonPin1); // down

    if (buttonState == HIGH) { // up
      buttonPushCounter++;
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    }
    if (buttonState1 == HIGH) { // down
      buttonPushCounter-- ;
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    }
    delay(700);
  }
  templimit = buttonPushCounter; // update templimit value

}

This is the schematic

  • 2
    You can save to EEPROM on pressing the 'Enter' button. – TisteAndii Oct 3 '16 at 14:24
  • I tried to modify the program. this project I use to limit the value of ADC. can this program be used? because I've tried but it does not work. thank you `#include <EEPROM.h> float read_from_eeprom(unsigned int address) {// read float config; for (byte i = 0; i < sizeof(config); i++) { reinterpret_cast<byte*>(&config)[i] = EEPROM.read(address + i); } return config; } // this constant won't change: const int buttonPin = 8; // up const int buttonPin1 = 7; // down const int buttonPin2 = 6; // enter int tro=A0; int value=0; int out=9; int Switpin=10; int Switch; int buttonPushCounter; // counter f – Kukuh Priambodo Nov 8 '17 at 8:22
2

An Arduino Uno has 512 bytes of non-volitile memory called EEPROM or Electrically Erasable Programmable Read Only Memory. Other Arduino platforms might have more. Accessing it is complex. Fortunately there is an Arduino EEPROM library already written for you. Your value is stored in a type int so I would use the read() and update() methods in the EEPROM library. The read is self explanatory. But the update, unlike the write, only runs if what is in the EEPROM location is different from what you want to write. In this way we save the EEPROM a little bit of ware and tare. As EEPROMs usually have a finite number of write cycles until they start failing. Which usually is in the tens of thousands.

  • Atmega328p has 1024 bytes of EEPROM. – JRobert Oct 4 '16 at 12:54
0

The microcontroller on the Arduino MEGA boards have 1024 bytes of EEPROM: memory whose values are kept when the board is turned off (like a tiny hard drive). These values will stay in the EEPROM when the board is turned off and may be retrieved later by another sketch.

I wrote two read/write functions if the value you want to write on EEPROM is negative because temperature has negative values too. Arduino EERROM write word with values between 0-255, also, has a lifetime 100,000 write cycle/ cell.

You can use EEPROM.update(address, value) instead of EEPROM.write(address, value) so the value is written only if differs from the one already saved at the same address.

Save to EEPROM:

void save_to_eeprom(unsigned int address, float config) { // (address, value)

  for (byte i = 0; i < sizeof(config); i++) { // size of config is 4
    EEPROM.write(address + i, reinterpret_cast<byte*>(&config)[i]);
  }

Read from EEPROM:

float read_from_eeprom(unsigned int address) { //(address)
  float config;
  for (byte i = 0; i < sizeof(config); i++) { // size of config is 4
    reinterpret_cast<byte*>(&config)[i] = EEPROM.read(address + i);
  }
  return config;
}

This is how templimit will keep its last value after restart, the full code is:

#include <EEPROM.h>

  float read_from_eeprom(unsigned int address) {// read 
    float config;
    for (byte i = 0; i < sizeof(config); i++) {
      reinterpret_cast<byte*>(&config)[i] = EEPROM.read(address + i);
    }
    return config;
  }

// this constant won't change:
const int  buttonPin = 8;    // up
const int  buttonPin1 = 3;    // down
const int  buttonPin2 = 2;    // enter


// Variables will change:
int buttonPushCounter;   // counter for the number of button presses
int buttonState;;  // up
int buttonState1;;  // down
int buttonState2;;  // enter
int templimit = read_from_eeprom(20); // temperature limit for alarm

void setup() {
  Serial.begin(9600);
  Serial.println("Update temperature limit if not wait 10 sec for time out");
  updatevalue();
  Serial.println("Done");
  Serial.println(templimit); // temperature limit for alarm

}


void loop() {
  // do nothing
}

int updatevalue(void) {
  int timenow;
  int timepassed;
  int counter = 10;

  timenow = millis();
  while (buttonState == LOW and buttonState1 == LOW and buttonState2 == LOW) { // this loop is just for 10 sec time out
    buttonState2 = digitalRead(buttonPin2); // enter
    buttonState = digitalRead(buttonPin); //up
    buttonState1 = digitalRead(buttonPin1); // down
    timepassed = (millis() - timenow);
    delay(1000);
    Serial.println(counter--);
    if (timepassed >= 10000)
      return 0;
  }
  while (buttonState2 != HIGH) { // do this until enter is pressed

    buttonState2 = digitalRead(buttonPin2); // enter
    buttonState = digitalRead(buttonPin); //up
    buttonState1 = digitalRead(buttonPin1); // down

    if (buttonState == HIGH) { // up
      buttonPushCounter++;
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    }
    if (buttonState1 == HIGH) { // down
      buttonPushCounter-- ;
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    }
    delay(700);
  }

  save_to_eeprom(20, buttonPushCounter); // save to address 20 tha value it can be negative

}

void save_to_eeprom(unsigned int address, float config) {// write value it can be negative

  for (byte i = 0; i < sizeof(config); i++) {
    EEPROM.write(address + i, reinterpret_cast<byte*>(&config)[i]);
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.