I am making a temperature sensor. I want to make it so that I can save the temperature using EEPROM. I want to be able to save it and then scroll through the saved temps. I thought I might be able to assign a temperature to a number than scroll through it using a potentiometer. I want to know if this is possible and how I might be able to do this Please ignore spelling mistakes
#include <ezButton.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <LCD_I2C.h>
#include <EEPROM.h>
bool useCelcius = true;
DHT dht(11, DHT11);
LCD_I2C lcd(0x27, 16, 2); // Default address of most PCF8574 modules, change according
ezButton button(4); // create ezButton object that attach to pin 4;
int button2 = 7;
int button3 = 6;
void setup() {
EEPROM.write(0, 0);
pinMode(button2, INPUT_PULLUP);
Serial.begin(9600);
button.setDebounceTime(50); // set debounce time to 50 milliseconds
lcd.begin(); // If you are using more I2C devices using the Wire library use lcd.begin(false)
// this stop the library(LCD_I2C) from calling Wire.begin()
lcd.backlight();
dht.begin();
}
void tempature() {
int celcius = dht.readTemperature();
int farenheit = dht.readTemperature(true);
int humidity = dht.readHumidity();
int heat = dht.computeHeatIndex(celcius, humidity, false);
button.loop(); // MUST call the loop() function first
if (button.isPressed()) {
// change state of switch
useCelcius = !useCelcius;
}
if (!useCelcius) {
lcd.setCursor(0, 0);
lcd.print(" Heat Index ");
lcd.setCursor(7, 1); //
lcd.print(heat); //
lcd.print("C");
lcd.print(" ");
}
else {
lcd.setCursor(0, 0);
lcd.print(" Celsius ");
lcd.setCursor(7, 1); //
lcd.print(celcius); //
lcd.print("C");
lcd.print(" ");
}
}
void saving() {
int celcius = dht.readTemperature();
int humidity = dht.readHumidity();
int heat = dht.computeHeatIndex(celcius, humidity, false);
int one
int two
int three
int four
int five
celcius = EEPROM.read(0);
Serial.println(celcius);
lcd.setCursor(0, 0);
lcd.print(" SAVING ");
lcd.setCursor(0, 1);
lcd.print(" ");
}
void loop() {
if (digitalRead(button2) == HIGH){
tempature();
}
else {
saving();
delay(2000);}
}
I don't know what good assigning a number would do, just map the pot (use a linear pot) values to some arbitrary mapping. It will likely need to be a fairly small number as a regular pot only moves a small amount. BUT get one of those multi turn pots and you will have many more choices. I just did an Amazon search on 'multi turn precision pot' and quite a few turned up.
You already have an "index" for your data, the EEPROM address. I would suggest you use an encoder to scroll, they don't "bounce" and you can go around as many times as you want.
Consider if you are going to save a "time" with your data. You could save the millis() or perhaps seconds (or minutes) since rebooting (or resetting a variable by another button).
Be aware that EEPROM.write() only writes a single byte and EEPROM.read() only reads a single byte. You're using integers (2 or 4 bytes depending on the board that you're using) for the temperatures so your attempt will fail. Use EEPROM.put() to write and EEPROM.get() to read; see the reference.
You will need to keep track of where you are writing; use an integer as an index variable so you know where to write the next time (increment it after writing); call it e.g. writeIndex.
You will also need a similar variabke for the reading; call it readIndex.
The locations in the EEPROM need to be incremented by the size of the temperature variable. So you can not write to location 0, location 1 etc but you write to writeIndex * sizeof(celsius) and you read from readIndex * sizeof(celsius).
If you also want to store the humidity, the locations will be writeIndex * (sizeof(celsius) + sizeof(humidity)); similar for the read.
@jacob4838034
see if this code meets your needs.
But note that a common potentiometer will be very difficult to hit the exact point you want to list.
The ideal would be to use a multi-turn potentiometer as recommended by @sonofcy.
This code is simulated at:
/***
eeprom_get example.
This shows how to use the EEPROM.get() method.
To pre-set the EEPROM data, run the example sketch eeprom_put.
This sketch will run without it, however, the values shown
will be shown from what ever is already on the EEPROM.
This may cause the serial object to print out a large string
of garbage if there is no null character inside one of the strings
loaded.
Written by Christopher Andrews 2015
Released under MIT licence.
***/
#include <EEPROM.h>
void setup() {
float f = 0.00f; //Variable to store data read from EEPROM.
int eeAddress = 0; //EEPROM address to start reading from
byte value;
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
delay(10000);
// read first few bytes to see if the "default" value is 255
Serial.print("Read first 3 bytes: ");
value = EEPROM.read(0);
Serial.print(value); Serial.print(" ");
value = EEPROM.read(1);
Serial.print(value); Serial.print(" ");
value = EEPROM.read(2);
Serial.println(value);
Serial.print("Read float from EEPROM: ");
//Get the float data from the EEPROM at position 'eeAddress'
EEPROM.get(eeAddress, f);
Serial.println(f, 3); //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float.
/**
As get also returns a reference to 'f', you can use it inline.
E.g: Serial.print( EEPROM.get( eeAddress, f ) );
Get can be used with custom structures too.
I have separated this into an extra function.
**/
secondTest(); //Run the next test.
}
struct MyObject {
float field1;
byte field2;
char name[10];
};
void secondTest() {
int eeAddress = sizeof(float); //Move address to the next byte after float 'f'.
MyObject customVar; //Variable to store custom object read from EEPROM.
EEPROM.get(eeAddress, customVar);
Serial.println("Read custom object from EEPROM: ");
Serial.println(customVar.field1);
Serial.println(customVar.field2);
Serial.println(customVar.name);
}
void loop() {
/* Empty loop */
}