This is my first post here and I’m hoping there are some arduino hobbyists here that can help me out.
Does anyone here know how to save a float variable directly to the eeprom? If so could you please have a look at the program I wrote here and hopefully you can show me how I can modify my code here to work with the other 2 variables I’m saving to the eeprom. The program I wrote displays a voltmeter to the LCD. It has an adjustable alarm set point as to when to sound an alarm when the voltage drops below a user set alarm set point.
I want to be able to declare the alarm set point as a float variable and save it directly to the eeprom. I can’t seem to find any programs that I could use as an example to try and figure out how to do that. Even though I wrote this program here I have very little experience or understanding of what’s involved in setting up a float var to be saved. I’ve been reading a lot of posts on the official arduino forum but there aren’t too many posts there that are actually resolved and even when they are resolved the complete working code is very seldom posted. I get most of my coding experience by taking apart other peoples code in a working program to combine with my programs. That’s the nice thing about open source is that a lot of it is out there but unfortunately I can’t seem to find any working programs that are saving a float var to the eeprom at least none that I can understand what’s going on.
I did manage to get my program working to save my Volt_Alarm_Set_Point by declaring it as an int var and then comparing it to my Volt_Set_Point (declared as a float var) to 165 if statements to save the right Volt_Alarm_Set_Point value. And it works perfectly but it uses up 4956 bytes of program space. Way too much…
That’s ok if I’m using an Arduino Mega but I’d like to be able to have some of my programs run on an Arduino Nano which could cause problems with some bigger programs. If anyone here could show me how to do this or has a link to a program that I can use as a guide it would be greatly appreciated and I would be in your dept. Here is my code below.
Thanks
jessey
#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
//----------Volt Meter Code----------//
int analogInput = A6;
float vout = 0.0;
float vin = 0.0;
float R1 = 99300.0;
float R2 = 9970.0;
int value = 0;
//----------Volt Meter Code----------//
int Volt_Alarm_Set_Point; // this works
float Volt_Set_Point;
int s = 0;
int a = 0;
int b = 0;
int Button_1 = 5;
int Button_2 = 6;
int Button_3 = A1;
int Button_4 = A0;
int Alarm_Relay_1 = A3;
int Relay_2 = A2;
int Relay_3 = 8;
int Blinking_Led = 3;// this one is ready to be used anytime
#define VOLT_ALARM_SET_POINT_ADDR 0
#define A_ADDR VOLT_ALARM_SET_POINT_ADDR + sizeof(int)
#define B_ADDR A_ADDR + sizeof(int)
#define Is_Pressed 0 // used for pushbuttons
#define Is_Turned_On 1 // used for the relays
#define Is_Turned_Off 0 // used for the relays
//|---------void setup---------void setup---------|//
void setup() {
pinMode(Alarm_Relay_1, OUTPUT);
pinMode(Relay_2, OUTPUT);
pinMode(Relay_3, OUTPUT);
pinMode(Blinking_Led, OUTPUT);
pinMode(Button_1, INPUT); // on pin 5
pinMode(Button_2, INPUT); // on pin 6
pinMode(Button_3, INPUT); // on pin A1
pinMode(Button_4, INPUT); // on pin A0
//-----------Volt Meter Code--------------//
pinMode(analogInput, INPUT);
//-----------Volt Meter Code--------------//
lcd.begin();
lcd.setCursor(0, 0);
lcd.print(F(" This Program Is "));
lcd.setCursor(0, 1);
lcd.print(F("to try and save a "));
lcd.setCursor(0, 2);
lcd.print(F("float variable "));
lcd.setCursor(0, 3);
lcd.print(F("to the eeprom "));
delay(500);
lcd.clear();
EEPROM.get(VOLT_ALARM_SET_POINT_ADDR, Volt_Alarm_Set_Point);
EEPROM.get(A_ADDR, a);
EEPROM.get(B_ADDR, b);
// EEPROM.put( VOLT_ALARM_SET_POINT_ADDR, Volt_Alarm_Set_Point);
// EEPROM.put(A, a);
// EEPROM.put(B, b);
}// end of the void setup()
//|---------void setup---------void setup---------|//
//|---------void loop---------void loop---------|//
void loop() {
delay(50);
//increment & decrement Volt_Alarm_Set_Point
if (digitalRead(Button_1)==Is_Pressed
||digitalRead(Button_2)==Is_Pressed)
{Adjust_The_Volt_Alarm_Set_Point();}
Read_Volt_Meter();
Display_A_Float_Voltage_Reference();
if (Volt_Alarm_Set_Point<=9){// these next 4 if statements
lcd.setCursor(16, 1); // below gets rid of left over
lcd.print(" ");} // #'s to the display while decrementing
if (Volt_Alarm_Set_Point<=99){
lcd.setCursor(19, 1);
lcd.print(" ");}
if (Volt_Alarm_Set_Point<=9){
lcd.setCursor(17, 2);
lcd.print(" ");}
if (Volt_Alarm_Set_Point<=99){
lcd.setCursor(18, 2);
lcd.print(" ");}
lcd.setCursor(0, 0);
lcd.print(F("This is the Volt adj"));
lcd.setCursor(0, 1);
lcd.print(F("Volt Set Pnt = "));
lcd.setCursor(15, 1);
lcd.print(Volt_Set_Point);
lcd.setCursor(0,2);
lcd.print(F("Voltage = "));
lcd.setCursor(10, 2);
lcd.print(vin);
lcd.setCursor(16, 2);
lcd.print(Volt_Alarm_Set_Point);
if (vin < Volt_Set_Point){digitalWrite(Relay_3, Is_Turned_On);}
if (vin > Volt_Set_Point){digitalWrite(Relay_3, Is_Turned_Off);}
lcd.setCursor(0, 3);lcd.print("A = ");lcd.setCursor(4, 3);lcd.print(a);
lcd.setCursor(6, 3);lcd.print("B = ");lcd.setCursor(11, 3);lcd.print(b);
if (a==0) {digitalWrite(Relay_2, Is_Turned_Off);}
if (a==1) {digitalWrite(Relay_2, Is_Turned_On);}
if (b==0) {digitalWrite(Alarm_Relay_1, Is_Turned_Off);}
if (b==1) {digitalWrite(Alarm_Relay_1, Is_Turned_On);}
if (digitalRead(Button_4)==Is_Pressed){
if (a==0){a=1;}else{a=0;}
while (digitalRead(Button_4)==Is_Pressed){
if (a==0) {digitalWrite(Relay_2, Is_Turned_Off);}
if (a==1) {digitalWrite(Relay_2, Is_Turned_On);}
lcd.setCursor(0, 3);lcd.print("A = ");lcd.setCursor(4,3);lcd.print(a);
lcd.setCursor(6, 3);lcd.print("B = ");lcd.setCursor(11,3);lcd.print(b);
}
EEPROM.put(A_ADDR, a); EEPROM.put(B_ADDR, b);
}
if (digitalRead(Button_3)==Is_Pressed){
if (b==0){b=1;}else{b=0;}
while (digitalRead(Button_3)==Is_Pressed){
if (b==0) {digitalWrite(Alarm_Relay_1, Is_Turned_Off);}
if (b==1) {digitalWrite(Alarm_Relay_1, Is_Turned_On);}
lcd.setCursor(0, 3);lcd.print("A = ");lcd.setCursor(4,3);lcd.print(a);
lcd.setCursor(6, 3);lcd.print("B = ");lcd.setCursor(11,3);lcd.print(b);
}
EEPROM.put(A_ADDR, a); EEPROM.put(B_ADDR, b);
}
}// end of the void loop()
//|---------void loop---------void loop---------|//
//======= Subroutine Area ======= Subroutine Area =======//
///*
void Display_A_Float_Voltage_Reference()
{
if (Volt_Alarm_Set_Point==0){Volt_Set_Point=0.00;}// this code uses 165 if statements for 4956 bytes of program space and 4 bytes of dynamic memory.
if (Volt_Alarm_Set_Point==1){Volt_Set_Point=0.10;}
if (Volt_Alarm_Set_Point==2){Volt_Set_Point=0.20;}
if (Volt_Alarm_Set_Point==3){Volt_Set_Point=0.30;}
if (Volt_Alarm_Set_Point==4){Volt_Set_Point=0.40;}
if (Volt_Alarm_Set_Point==5){Volt_Set_Point=0.50;}
if (Volt_Alarm_Set_Point==6){Volt_Set_Point=0.60;}
if (Volt_Alarm_Set_Point==7){Volt_Set_Point=0.70;}
if (Volt_Alarm_Set_Point==8){Volt_Set_Point=0.80;}
if (Volt_Alarm_Set_Point==9){Volt_Set_Point=0.90;}
if (Volt_Alarm_Set_Point==10){Volt_Set_Point=1.00;}
if (Volt_Alarm_Set_Point==11){Volt_Set_Point=1.10;}
if (Volt_Alarm_Set_Point==12){Volt_Set_Point=1.20;}
if (Volt_Alarm_Set_Point==13){Volt_Set_Point=1.30;}
if (Volt_Alarm_Set_Point==14){Volt_Set_Point=1.40;}
if (Volt_Alarm_Set_Point==15){Volt_Set_Point=1.50;}
if (Volt_Alarm_Set_Point==16){Volt_Set_Point=1.60;}
if (Volt_Alarm_Set_Point==17){Volt_Set_Point=1.70;}
if (Volt_Alarm_Set_Point==18){Volt_Set_Point=1.80;}
if (Volt_Alarm_Set_Point==19){Volt_Set_Point=1.90;}
if (Volt_Alarm_Set_Point==20){Volt_Set_Point=2.00;}
if (Volt_Alarm_Set_Point==21){Volt_Set_Point=2.10;}
if (Volt_Alarm_Set_Point==22){Volt_Set_Point=2.20;}
if (Volt_Alarm_Set_Point==23){Volt_Set_Point=2.30;}
if (Volt_Alarm_Set_Point==24){Volt_Set_Point=2.40;}
if (Volt_Alarm_Set_Point==25){Volt_Set_Point=2.50;}
if (Volt_Alarm_Set_Point==26){Volt_Set_Point=2.60;}
if (Volt_Alarm_Set_Point==27){Volt_Set_Point=2.70;}
if (Volt_Alarm_Set_Point==28){Volt_Set_Point=2.80;}
if (Volt_Alarm_Set_Point==29){Volt_Set_Point=2.90;}
if (Volt_Alarm_Set_Point==30){Volt_Set_Point=3.00;}
if (Volt_Alarm_Set_Point==31){Volt_Set_Point=3.1;}
if (Volt_Alarm_Set_Point==32){Volt_Set_Point=3.2;}
if (Volt_Alarm_Set_Point==33){Volt_Set_Point=3.3;}
if (Volt_Alarm_Set_Point==34){Volt_Set_Point=3.4;}
if (Volt_Alarm_Set_Point==35){Volt_Set_Point=3.5;}
if (Volt_Alarm_Set_Point==36){Volt_Set_Point=3.6;}
if (Volt_Alarm_Set_Point==37){Volt_Set_Point=3.7;}
if (Volt_Alarm_Set_Point==38){Volt_Set_Point=3.8;}
if (Volt_Alarm_Set_Point==39){Volt_Set_Point=3.9;}
if (Volt_Alarm_Set_Point==40){Volt_Set_Point=4.0;}
if (Volt_Alarm_Set_Point==41){Volt_Set_Point=4.1;}
if (Volt_Alarm_Set_Point==42){Volt_Set_Point=4.2;}
if (Volt_Alarm_Set_Point==43){Volt_Set_Point=4.3;}
if (Volt_Alarm_Set_Point==44){Volt_Set_Point=4.4;}
if (Volt_Alarm_Set_Point==45){Volt_Set_Point=4.5;}
if (Volt_Alarm_Set_Point==46){Volt_Set_Point=4.6;}
if (Volt_Alarm_Set_Point==47){Volt_Set_Point=4.7;}
if (Volt_Alarm_Set_Point==48){Volt_Set_Point=4.8;}
if (Volt_Alarm_Set_Point==49){Volt_Set_Point=4.9;}
if (Volt_Alarm_Set_Point==50){Volt_Set_Point=5.0;}
if (Volt_Alarm_Set_Point==51){Volt_Set_Point=5.1;}
if (Volt_Alarm_Set_Point==52){Volt_Set_Point=5.2;}
if (Volt_Alarm_Set_Point==53){Volt_Set_Point=5.3;}
if (Volt_Alarm_Set_Point==54){Volt_Set_Point=5.4;}
if (Volt_Alarm_Set_Point==55){Volt_Set_Point=5.5;}
if (Volt_Alarm_Set_Point==56){Volt_Set_Point=5.6;}
if (Volt_Alarm_Set_Point==57){Volt_Set_Point=5.7;}
if (Volt_Alarm_Set_Point==58){Volt_Set_Point=5.8;}
if (Volt_Alarm_Set_Point==59){Volt_Set_Point=5.9;}
if (Volt_Alarm_Set_Point==60){Volt_Set_Point=6.0;}
if (Volt_Alarm_Set_Point==61){Volt_Set_Point=6.1;}
if (Volt_Alarm_Set_Point==62){Volt_Set_Point=6.2;}
if (Volt_Alarm_Set_Point==63){Volt_Set_Point=6.3;}
if (Volt_Alarm_Set_Point==64){Volt_Set_Point=6.4;}
if (Volt_Alarm_Set_Point==65){Volt_Set_Point=6.5;}
if (Volt_Alarm_Set_Point==66){Volt_Set_Point=6.6;}
if (Volt_Alarm_Set_Point==67){Volt_Set_Point=6.7;}
if (Volt_Alarm_Set_Point==68){Volt_Set_Point=6.8;}
if (Volt_Alarm_Set_Point==69){Volt_Set_Point=6.9;}
if (Volt_Alarm_Set_Point==70){Volt_Set_Point=7.0;}
if (Volt_Alarm_Set_Point==71){Volt_Set_Point=7.1;}
if (Volt_Alarm_Set_Point==72){Volt_Set_Point=7.2;}
if (Volt_Alarm_Set_Point==73){Volt_Set_Point=7.3;}
if (Volt_Alarm_Set_Point==74){Volt_Set_Point=7.4;}
if (Volt_Alarm_Set_Point==75){Volt_Set_Point=7.5;}
if (Volt_Alarm_Set_Point==76){Volt_Set_Point=7.6;}
if (Volt_Alarm_Set_Point==77){Volt_Set_Point=7.7;}
if (Volt_Alarm_Set_Point==78){Volt_Set_Point=7.8;}
if (Volt_Alarm_Set_Point==79){Volt_Set_Point=7.9;}
if (Volt_Alarm_Set_Point==80){Volt_Set_Point=8.0;}
if (Volt_Alarm_Set_Point==81){Volt_Set_Point=8.1;}
if (Volt_Alarm_Set_Point==82){Volt_Set_Point=8.2;}
if (Volt_Alarm_Set_Point==83){Volt_Set_Point=8.3;}
if (Volt_Alarm_Set_Point==84){Volt_Set_Point=8.4;}
if (Volt_Alarm_Set_Point==85){Volt_Set_Point=8.5;}
if (Volt_Alarm_Set_Point==86){Volt_Set_Point=8.6;}
if (Volt_Alarm_Set_Point==87){Volt_Set_Point=8.7;}
if (Volt_Alarm_Set_Point==88){Volt_Set_Point=8.8;}
if (Volt_Alarm_Set_Point==89){Volt_Set_Point=8.9;}
if (Volt_Alarm_Set_Point==90){Volt_Set_Point=9.0;}
if (Volt_Alarm_Set_Point==91){Volt_Set_Point=9.1;}
if (Volt_Alarm_Set_Point==92){Volt_Set_Point=9.2;}
if (Volt_Alarm_Set_Point==93){Volt_Set_Point=9.3;}
if (Volt_Alarm_Set_Point==94){Volt_Set_Point=9.4;}
if (Volt_Alarm_Set_Point==95){Volt_Set_Point=9.5;}
if (Volt_Alarm_Set_Point==96){Volt_Set_Point=9.6;}
if (Volt_Alarm_Set_Point==97){Volt_Set_Point=9.7;}
if (Volt_Alarm_Set_Point==98){Volt_Set_Point=9.8;}
if (Volt_Alarm_Set_Point==99){Volt_Set_Point=9.9;}
if (Volt_Alarm_Set_Point==100){Volt_Set_Point=10.0;}
if (Volt_Alarm_Set_Point==101){Volt_Set_Point=10.1;}
if (Volt_Alarm_Set_Point==102){Volt_Set_Point=10.2;}
if (Volt_Alarm_Set_Point==103){Volt_Set_Point=10.3;}
if (Volt_Alarm_Set_Point==104){Volt_Set_Point=10.4;}
if (Volt_Alarm_Set_Point==105){Volt_Set_Point=10.5;}
if (Volt_Alarm_Set_Point==106){Volt_Set_Point=10.6;}
if (Volt_Alarm_Set_Point==107){Volt_Set_Point=10.7;}
if (Volt_Alarm_Set_Point==108){Volt_Set_Point=10.8;}
if (Volt_Alarm_Set_Point==109){Volt_Set_Point=10.9;}
if (Volt_Alarm_Set_Point==110){Volt_Set_Point=11.0;}
if (Volt_Alarm_Set_Point==111){Volt_Set_Point=11.1;}
if (Volt_Alarm_Set_Point==112){Volt_Set_Point=11.2;}
if (Volt_Alarm_Set_Point==113){Volt_Set_Point=11.3;}
if (Volt_Alarm_Set_Point==114){Volt_Set_Point=11.4;}
if (Volt_Alarm_Set_Point==115){Volt_Set_Point=11.5;}
if (Volt_Alarm_Set_Point==116){Volt_Set_Point=11.6;}
if (Volt_Alarm_Set_Point==117){Volt_Set_Point=11.7;}
if (Volt_Alarm_Set_Point==118){Volt_Set_Point=11.8;}
if (Volt_Alarm_Set_Point==119){Volt_Set_Point=11.9;}
if (Volt_Alarm_Set_Point==120){Volt_Set_Point=12.0;}
if (Volt_Alarm_Set_Point==121){Volt_Set_Point=12.1;}
if (Volt_Alarm_Set_Point==122){Volt_Set_Point=12.2;}
if (Volt_Alarm_Set_Point==123){Volt_Set_Point=12.3;}
if (Volt_Alarm_Set_Point==124){Volt_Set_Point=12.4;}
if (Volt_Alarm_Set_Point==125){Volt_Set_Point=12.5;}
if (Volt_Alarm_Set_Point==126){Volt_Set_Point=12.6;}
if (Volt_Alarm_Set_Point==127){Volt_Set_Point=12.7;}
if (Volt_Alarm_Set_Point==128){Volt_Set_Point=12.8;}
if (Volt_Alarm_Set_Point==129){Volt_Set_Point=12.9;}
if (Volt_Alarm_Set_Point==130){Volt_Set_Point=13.0;}
if (Volt_Alarm_Set_Point==131){Volt_Set_Point=13.1;}
if (Volt_Alarm_Set_Point==132){Volt_Set_Point=13.2;}
if (Volt_Alarm_Set_Point==133){Volt_Set_Point=13.3;}
if (Volt_Alarm_Set_Point==134){Volt_Set_Point=13.4;}
if (Volt_Alarm_Set_Point==135){Volt_Set_Point=13.5;}
if (Volt_Alarm_Set_Point==136){Volt_Set_Point=13.6;}
if (Volt_Alarm_Set_Point==137){Volt_Set_Point=13.7;}
if (Volt_Alarm_Set_Point==138){Volt_Set_Point=13.8;}
if (Volt_Alarm_Set_Point==139){Volt_Set_Point=13.9;}
if (Volt_Alarm_Set_Point==140){Volt_Set_Point=14.0;}
if (Volt_Alarm_Set_Point==141){Volt_Set_Point=14.1;}
if (Volt_Alarm_Set_Point==142){Volt_Set_Point=14.2;}
if (Volt_Alarm_Set_Point==143){Volt_Set_Point=14.3;}
if (Volt_Alarm_Set_Point==144){Volt_Set_Point=14.4;}
if (Volt_Alarm_Set_Point==145){Volt_Set_Point=14.5;}
if (Volt_Alarm_Set_Point==146){Volt_Set_Point=14.6;}
if (Volt_Alarm_Set_Point==147){Volt_Set_Point=14.7;}
if (Volt_Alarm_Set_Point==148){Volt_Set_Point=14.8;}
if (Volt_Alarm_Set_Point==149){Volt_Set_Point=14.9;}
if (Volt_Alarm_Set_Point==150){Volt_Set_Point=15.0;}
} // end of void Display_A_Float_Voltage_Reference()
//*/
void Adjust_The_Volt_Alarm_Set_Point()
{ s=0;
// push button to increase this Alarm Set Point
if (digitalRead(Button_2) == Is_Pressed)
{ s=1;
if (Volt_Alarm_Set_Point <= 150)
{
Volt_Alarm_Set_Point = Volt_Alarm_Set_Point + 1;// this line is using a int variable
if (Volt_Alarm_Set_Point > 150)
{Volt_Alarm_Set_Point = 150;}
}
}
// push button to decrease this Alarm Set Point
if (digitalRead(Button_1) == Is_Pressed)
{ s=1;
Volt_Alarm_Set_Point = Volt_Alarm_Set_Point - 1;// this line is using a int variable
if (Volt_Alarm_Set_Point <= 0)
{Volt_Alarm_Set_Point = 0;}
}
if (s==1){EEPROM.put( VOLT_ALARM_SET_POINT_ADDR,Volt_Alarm_Set_Point);}
}
void Read_Volt_Meter()
{
//-------------------------------Volt Meter Code-------------------------------//
//-----------------------------------// This code reads the value at analog
value = analogRead(analogInput);//---// input A6 and this is where we can change
vout = (value * 4.72) / 1024.0;//----// the volts value to what the actual supply
vin = vout / (R2/(R1+R2));//---------// voltage is equal to by checking it with an
if (vin<0.09) {//--------------------// accurate Digital Votl Meter then entering
vin=0.0;//---------------------------// it here-->(value * 4.79) changing the 4.79
}//----------------------------------// to whatever your meter reads...
//-------------------------------Volt Meter Code-------------------------------//
}
//======= Subroutine Area ======= Subroutine Area =======//
// end the Subroutine Area
// end of the program...