Write a float variable to EEPROM

Hello all,

I am trying to write a float variable to the EEPROM, but, I get a lot of errors from the compiler. The code I use is below. Can any one help please?

I want also to save an float array. I have looked to the following topic http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1234477290/3 , but it is very difficult for me to understand the structure as well as where put array name, length , etc. Is there any clearest examples in order to understand it and use?

thanks,
Manuel

   uint8_t i=0;
   uint8_t a=0;
   uint8_t ad=0X01
   
   float *v_limit_ptr;
   v_limit_ptr = &volt_element;

     for (1=24; i<=0; i-8)
          {
        a= (uint8_t) (*v_limit_ptr >> i); 
           EEPROM.write(ad, a);
           ad++
          }
unsigned long* v_limit_ptr = (unsigned long*)&volt_element;

would be a quick-and-dirty way.

You could avoid shifts altogether:

unsigned char* v_limit_ptr = (unsigned char*)&volt_element;

and just write four bytes, no shifts required.

This has some major problems, too:

     for (1=24; i<=0; i-8)

i-8 is not doing anything. i-=8 would. The first character after the ( should probably be an i, too.

with some pointer math it is not too difficult.

the name of an array is the start adres in memory of the array,
by casting this address to a byte pointer
one can iterate through the array
by adding an offset to its base address.

one must keep in mind the size of the array and the size of the element to get it all.

//
//    FILE: floatEEPROM
//  AUTHOR: Rob van den Tillaart
//    DATE: 2011-11-23 
//

#include <EEPROM.h>

float arf[10] = { 
  1,2,3,4,5,6,7,8,9,0 };

///////////////////////////////////////////////////////////////////

void setup()
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);  
}

void loop()
{
  digitalWrite(13, HIGH);
  Serial.println("Start writing EEPROM");        
  for (int i=0; i< 10*sizeof(float); i++)
  {
    byte b = *(((byte*)arf)+i);
    EEPROM.write(0+i, b);
  }
  Serial.println("Clearing array");        

  for (int i=0; i< 10; i++)
  {
    arf[i] = 0;
  }  
  Serial.println("Start reading EEPROM");        
  for (int i=0; i< 10*sizeof(float); i++)
  {
    byte b = EEPROM.read(0+i);
    *(((byte*)arf)+i) = b;
  }
  Serial.println("printing array");        
  for (int i=0; i< 10; i++)
  {
    Serial.println(arf[i], 2);
  } 

  Serial.println("Ready");

  while(true)                // blink to indicate ready
  {
    digitalWrite(13, HIGH);
    delay(50);                  
    digitalWrite(13, LOW);
    delay(50);               
  }
}

Robtillaart, PaulS and AWOL,

Thanks for your inputs. You really helped. I am going to try/use your sugestions.

Regards,
Manuel

Hello all,

I am trying to save a float to eeprom. I used several ways like using a float pointer to the float "volt_element" to get the value (4 bytes) and shift it out and copy to EEPROM, but it didn't work. I tried several others ways but I always get compiler errors. I do not know enough C language to do it. I tried using this code that I took from the net, but it doesn't work also. Can you please let me know what is wrong?
Your help is appreciated.

#include "EEPROM.h"


int address=0;
void eepromWriteFloat(int address, float volt_element);

volt_element = eepromReadFloat(address);

 void eepromWriteFloat(int address, float volt_element)
{
   union u_tag
   {
     byte b[4];
     float fval;
   } u;
   u.fval=volt_element;
 
   EEPROM.write(address  , u.b[0]);
   EEPROM.write(address+1, u.b[1]);
   EEPROM.write(address+2, u.b[2]);
   EEPROM.write(address+3, u.b[3]);
}

float eepromReadFloat(int address)
  {
   union u_tag 
   {
     byte b[4];
     float volt_element;
   } u;   
   u.b[0] = EEPROM.read(address);
   u.b[1] = EEPROM.read(address+1);
   u.b[2] = EEPROM.read(address+2);
   u.b[3] = EEPROM.read(address+3);
   return u.volt_element;
}

That can't compile, so I don't know how you can say it doesn't work.
Where are "setup" and "loop"?

Please post your complete code…

Robtillaart, AWOL,

Thanks for your interest.

The code is a big listing. Please do not criticize, because it is my first C program. I normally write in assembly code. Here is the code:

#include <DogLcd.h>
#include "EEPROM.h" 

/*
 * PCD8544 - Interface with Philips PCD8544 (or compatible) LCDs.
 *
 * Copyright (c) 2010 Carlos Rodrigues <cefrodrigues@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/*
 * To use this sketch, connect the eight pins from your LCD like thus:
 *
 * Pin 1 VCC  -> +3.3V (rightmost, when facing the display head-on)
 * Pin 2 GND  -> Ground
 * Pin 3 SCE  -> Arduino digital pin 7
 * Pin 4 RST  -> Arduino digital pin 6
 * Pin 5 D/C  -> Arduino digital pin 5
 * Pin 6 MOSI -> Arduino digital pin 4
 * Pin 7 SCLK -> Arduino digital pin 3
 * Pin 8 LED  -> 10uF capacitor -> Ground LED

 *
 * Since these LCDs are +3.3V devices, you have to add extra components to
 * connect it to the digital pins of the Arduino (not necessary if you are
 * using a 3.3V variant of the Arduino, such as Sparkfun's Arduino Pro).
 */





#include <PCD8544.h>
// A custom glyph (a smiley)...
static const byte glyph[] = { B00010100, B00100000, B00100000, B00100000, B00010100 };
static const byte airplane_widht = 84; 
static const byte airplane_height = 42;  
                                    
static const byte airplane[] = {
0x00, 0x00, 0xC0, 0x60, 0x20, 0x60, 0x40, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x60, 0x38, 0x28, 0xE4, 0xC4, 0x0C, 0x08, 0x18,
0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x1C, 0xFC, 0x84, 0x04, 0x78, 0xE0, 0x01, 0x02, 0x04,
0x04, 0x08, 0x20, 0x40, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x60, 0x38, 0x0C, 0x46, 0x01, 0xC0, 0x74, 0x1C, 0x07,
0x01, 0x00, 0x00, 0x00, 0x00, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xBE, 0xE0,
0xC9, 0xC7, 0xFC, 0xC0, 0x80, 0x80, 0x00, 0x00, 0x00, 0x01, 0x02, 0x02, 0x04, 0x04, 0x0C, 0x08,
0x08, 0x08, 0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0x08, 0x08, 0x00, 0x04,
0x04, 0x04, 0x04, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x81, 0x81, 0x81, 0x01, 0x03, 0x02, 0x02, 0x02, 0x02, 0xE2,
0x22, 0xB4, 0xF4, 0x98, 0xF0, 0x40, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0xCC,
0x82, 0x81, 0xC1, 0xC0, 0x40, 0x60, 0x20, 0x70, 0xB8, 0xA8, 0xE5, 0xC3, 0xC0, 0x40, 0xC0, 0xC0,
0xC0, 0x80, 0x80, 0x80, 0x80, 0x87, 0x89, 0x89, 0x89, 0x87, 0x80, 0x80, 0x80, 0x80, 0x8F, 0xC9,
0xC9, 0xCB, 0xC7, 0x40, 0x40, 0x40, 0x40, 0x47, 0x49, 0xC9, 0x8D, 0x87, 0x80, 0x80, 0x80, 0x80,
0x87, 0x8D, 0x89, 0x8D, 0x87, 0x80, 0x80, 0x80, 0x80, 0x83, 0x87, 0x84, 0x85, 0x83, 0x80, 0x80,
0x80, 0x80, 0x80, 0x81, 0xC1, 0xC1, 0x41, 0xE0, 0xA0, 0xB0, 0x9E, 0x7F, 0x3F, 0x1E, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xC1,
0x41, 0x41, 0x61, 0x23, 0x33, 0x1B, 0x9B, 0x8B, 0xCF, 0x46, 0x43, 0x43, 0x61, 0x21, 0x11, 0x18,
0x08, 0x0C, 0x04, 0x06, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80,
0x80, 0xC0, 0xC0, 0xC0, 0xC0, 0xE0, 0xE0, 0x11, 0x19, 0x0F, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D,
0x04, 0x04, 0x04, 0x04, 0x06, 0x06, 0x06, 0x02, 0x02, 0x03, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x1B,
0x31, 0x21, 0x23, 0x23, 0x22, 0x22, 0x26, 0x22, 0x23, 0x21, 0x21, 0x20, 0x20, 0x20, 0x20, 0x20,
0x30, 0x10, 0x10, 0x10, 0x10, 0x18, 0x08, 0x08, 0x08, 0x0C, 0x0C, 0x0C, 0x0E, 0x0E, 0x1A, 0x1A,
0x1F, 0x1F, 0x31, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x1F, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
};






static PCD8544 lcd;
char data=0;
int data_count=2;
uint8_t received_byte;
static uint8_t GPS_data[46]={0xB5,0x62,0x01,0x05};
uint8_t frame_count;
uint8_t sync_frame=0;
uint8_t frame_data;


unsigned long barometric_pressure=0;
unsigned long temp_pressure;
unsigned long pressure;
float altitude_barometric=0;
const double pressure_zero=101235;
const float v_divider_ratio=0.1960; // = 22K/(22K+91K)
int ad_voltage=0;
float bat_voltage=0;
float temp=0;
const float I_samples=4.00;
long int latitude;
long int longitude;
long int al_titude;
long int g_speed;
long int z;
float actual_longitude;
float actual_latitude;
long int z_int;
long int r;
char lat='N';
char lgt='W';
uint16_t  RPM_value=0;
uint16_t current_ad=0;
uint16_t delta_I=0;
uint16_t temperature_ad=0;
float temperature=0;
float I_ad=0;
const float adc_bit=0.0049076;
float mAH=0;
float a_volt=0.099; // milivolts/ampere
float I_instantaneo=0;
float avg_sample=0;
float volt_element=3.5; // low limit alarm voltage cab be adjusted
float volt_element_adj=0;
int val=0;
uint8_t screen=0;
volatile uint16_t count=0;
volatile uint16_t time=0;
#define buzzerpin 9
#define LED 8
uint8_t bat_test=1;
uint8_t pressure_reset=1;
uint8_t j=0;
uint8_t element_count=0;
int read_pot_pin = A0;    // select the input pin for the potentiometer
int pot_value = 0;  // variable to store the value coming from the sensor
float I_max=0;
uint16_t rpm_max=0;
float bat_temp_max=0;
float altit_baro_max=0;
uint16_t veloc_max=0;
uint16_t altit_max=0;
float altit_baro_zero=0;
float altit_relativa=0;
const uint8_t switchpin=10;
uint8_t switch_state=0;
const float bit_value=0.004852;
float first_latitude=0;
float first_longitude=0;
uint8_t coordenadas=1;
float flat_distance=0;
float distance=0;
const float umgrau_lat=111.0;// Km
const float umgrau_long=83.91077; //Km
float delta_latitude=0;
float delta_longitude=0;
unsigned int address=0;


void setup()
{
  // PCD8544-compatible displays may have a different resolution...
  lcd.begin(84, 48);
  lcd.clear();
  Serial.begin(9600);
  // Add the smiley to position "0" of the ASCII table...
  //  lcd.createChar(0, glyph);

  // Write a piece of text on the first line...
  lcd.setCursor(4, 0);
  lcd.print(" Hello, All!  ");

  // Write the counter on the second line...
  lcd.setCursor(0, 1);
  lcd.print(" I am alive  ");
 // lcd.write(' ');
 // lcd.write(0);  // write the smiley

  delay(2000);  
//  counter++;

 pinMode (buzzerpin, OUTPUT);
 pinMode (LED, OUTPUT);
 pinMode(13, OUTPUT);
  pinMode(switchpin, INPUT);
  pinMode(9, OUTPUT);
  time=3500;
  count=20;  
// initialize timer1 
  noInterrupts();           // disable all interrupts
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1  = 0;
  //TCCR1A = (1<<COM1A0);
  OCR1A = time;            // compare match register 16MHz/256/2Hz
  TCCR1B |= (1 << WGM12);   // CTC mode
 // TCCR1B |= (1 << CS12)|(1<<CS10);    // 1024 prescaler 
  TIMSK1 |= (1 << OCIE1A);  // enable timer compare interrupt
  interrupts();             // enable all interrupts
  digitalWrite(buzzerpin, LOW); //switch OFF speaker
}

code continuation:

void loop()
{
lcd.setCursor(0,0);
static const byte airplane_widht = 84; 
static const byte airplane_height = 42;   
lcd.drawBitmap(airplane, airplane_widht, airplane_height); 
delay(4000);


volt_element = eepromReadFloat(address);
  
for(;;)
{
 
digitalWrite(LED, digitalRead(LED) ^ 1);   
USART();

gps_position_calc();

if ((coordenadas==1) && (GPS_data[23]==3))// If we have good cooedenates data save the first ones
   {
    save_coordenadas();
   }

motor_data(); //Calculates all the parameters related with motor

// Verifies the battery voltage and calculates the number
// of elements.
if (bat_test==1) // At the start up calculates the battery number of elements and bips accordingly 
   {
   calculate_num_elem();
   } 
   
bat_volt_test(); // Test the battery voltage and alarms if the voltage by element is below the
                 // set value.

flight_data();   // calculates the flight data

if (pressure_reset==1)
   {
    reset_pressure();
   }

display_data();

calc_distancia();

max_values();

// print_data();
 
switch_state = digitalRead(switchpin);

if (switch_state == LOW)
    {
      set_alarm_volt();
    }
    
// uint8_t ByteOfData; 

// ByteOfData = eeprom_read_byte((uint8_t*)46); 

}
}


 void USART()
{
Serial.begin(9600);

 uint8_t sig[] = { 0x7E, 0xFD};
 
        data_count=0;
	sync_frame=0;

 	while (data_count<=44)
	{
        frame_data=1;
	uint8_t read_sig = 0;
	while  (frame_data>0)
	{ 

//	while(!Serial.available())
//        ;// Do nothing until there is something to read  
//        received_byte = Serial.read();
unsigned long lastTime = millis();
while (Serial.available() == 0 && millis() - lastTime < 2000); // Do nothing until there is something to read  or 2000 milliseconds have passed

if (Serial.available() > 0) // if there is something to read 
{
   received_byte = Serial.read();  // read it 
  // process the byte
}
else
{
  // nothing read code
}

	
	if (read_sig < 2)
            read_sig = received_byte == sig[read_sig] ? read_sig + 1:received_byte == sig[0];
	      else
		{
		switch (read_sig)
		{
		case 2:
		{
		frame_data=received_byte;
		read_sig++;
                }
		break;

		case 3: if (sync_frame==1)
		{
		read_sig = 6;
		}
		else
		{
		read_sig++;
		}
		break;

		case 4:if ((received_byte == (0xB5)) && (sync_frame == 0))
		{
		read_sig=5;
		frame_data--;
		}
		else
		{
		read_sig=0;
		}
		break;
					
		case 5: if ((received_byte == (0x62)) && (sync_frame == 0))
		{
		read_sig=6;
		frame_data--;
		sync_frame=1;
		}
		else
		{
		read_sig=0;
		}
		break;
							
		case 6: if ((received_byte == (0x7D)) || (received_byte == (0x7E)))
		{
		read_sig=7;
		}
		else
		{
		GPS_data[data_count++]=received_byte;
		frame_data--;
              	}
		break;

		case 7: if ((received_byte == (0x5E)) || (received_byte == (0x5D)))
		{
		received_byte ^= 0x20; 
		GPS_data[data_count++]=received_byte;
		frame_data--;
		read_sig=6;
		}
  
          default:
                
	        break;

		}    
	     }
              if ( millis() - lastTime >= 2000)
               {
                frame_data=0;
                data_count=45;
               }
	   }

	}
    }
          
// GPS Position Calculation 
//******************************************
 void gps_position_calc()
  {
  lcd.clear();
  lcd.setCursor(4,0);
  lcd.print("  GPS DATA  ");
  
  uint8_t i;
  latitude=0;
  longitude=0;
  al_titude=0;
  g_speed=0;
  z=0;
  r=0;


/* Latitude calculation
 Get 4 bytes for latitude shif it in a 32 bits signed.
 if the latitude is negative we must comvert it to positive
 (two's complmente) ans latitude is South if positive is North.
 */
 
 uint8_t index=2;
 
 latitude = get_four_bytes_from_vector(index);
   
  if (latitude<0)
      {
        latitude^=0xFFFFFFFF;
        latitude+=1;
        lat=('S');
      } 
      else
      {
      lat=('N');
      }
      actual_latitude=(float)latitude/1000000,0;
     
 /* longitude calculation 
 
 The longitude calculations is the same as the 
latitude.*/ 
   
 index=6;
 
 longitude = get_four_bytes_from_vector(index);
   if (longitude<0)
      {
        longitude^=0xFFFFFFFF;
        longitude+=1;
         lgt=('W');
      } 
      else
      {
       lgt=('E');
      }
   actual_longitude=(float)longitude/1000000.0;
   
      
      
 // Altitude calculation 
 index=10;
 al_titude = get_four_bytes_from_vector(index);
  
  z_int=al_titude/100;
  z=al_titude%100;
 
  
 // Ground Speed
  index=14;
 
 g_speed = get_four_bytes_from_vector(index);
 g_speed=g_speed*3600/100000;
 } 
 
//Flit data calculations 
void flight_data()
{
 uint8_t i;
 barometric_pressure=0;
 
 for (i=30; i<=32; i++)
  {
   barometric_pressure |= GPS_data[i];
     if (i<32)
      {
      barometric_pressure <<=8;
      }
      else
      {
      break;
      }
  } 
  
/*Altitude Calculation*/
  temp=(double) barometric_pressure/pressure_zero;
  temp=1-pow(temp, 0.19029);
  altitude_barometric=(44330*temp); 
}

void reset_pressure()
{
  altit_baro_zero=altitude_barometric; 
  pressure_reset=0; 
}


 
void motor_data()
{
  lcd.clear();
//Battery voltage calculation
//****************************
  ad_voltage=0;
  bat_voltage=0;        
  uint8_t i=0;
  uint8_t index=33;
 
ad_voltage = get_two_bytes_from_vector(index);
bat_voltage=((ad_voltage*adc_bit)/v_divider_ratio);
 

//RPM calculation
//******************
 RPM_value=0;
 index=41;
 
 RPM_value = get_two_bytes_from_vector(index); 

  
//Battery discharge Calculation maH
//********************************** 
 current_ad=0;
 index=39;
 current_ad = get_two_bytes_from_vector(index);
 
 I_ad=current_ad; // valor recebido do ADC 4 amostragens.
 
 delta_I=(I_ad/I_samples);
I_instantaneo=(((float)delta_I*adc_bit)/(float)a_volt);
 
 mAH+=(I_instantaneo/3.6);
 
       

// Battery temperature calculation
//*********************************

temperature_ad=0;
temperature=0;
index=37;
temperature_ad = get_two_bytes_from_vector(index);
 
  temperature=temperature_ad;
  temperature = (temperature *adc_bit/ 0.01);
} 

/* 
void print_data()
{
  int i;
  for (i=0; i<=45; i++)
      {
        Serial.print(GPS_data[i]);
      }
}
*/
long int get_four_bytes_from_vector(uint8_t index)
        {
          uint8_t i;
          long int result;
          for(i=index; i<=(index+3); i++)
              {
               result|=GPS_data[i];
                 if (i<(index+3))
                  {
                  result <<=8;
                  }
                  else
                  {
                  break;
                  }
               }
             return result;
        }
                
int get_two_bytes_from_vector(uint8_t index)
          {
          uint8_t i;
          int result;
          for(i=index; i<=(index+1); i++)
              {
               result|=GPS_data[i];
                 if (i<(index+1))
                  {
                  result <<=8;
                  }
                  else
                  {
                  break;
                  }
               }
           return result;
        }
        
//***********************************************************
/* This routine verifies the battery voltage and checks if it is
* charged if not, sets an alarme with high cadency bip, if 
* battery voltage is within limites of a number of cell, it 
* bips the number of elements of battery.*/

void calculate_num_elem()
{

  bat_test=0; // clear the battery test
 if(bat_voltage < 7.40)
    {
      alarm_sequence();
      }
     if((bat_voltage > 7.40) && (bat_voltage < 8,43))
       {
        element_count=2;
        OCR1A=12000;
        count=4;
        TCCR1B |= (1 << WGM12);   // CTC mode
        TCCR1B |= (1 << CS12)|(1<<CS10);    // 1024 prescaler
       }
       if((bat_voltage > 8.45) && (bat_voltage < 11.1))
       {
        alarm_sequence();
       }
       if((bat_voltage > 11.1) && (bat_voltage < 12.8))
       {
        element_count=3;
        OCR1A=12000;
        count=6;
        TCCR1B |= (1 << WGM12);   // CTC mode
        TCCR1B |= (1 << CS12)|(1<<CS10);    // 1024 prescaler
       }
        if((bat_voltage > 12.9) && (bat_voltage < 14.8))
       {
        alarm_sequence();
       }
       if((bat_voltage >14.8) && (bat_voltage < 17.0))
       {
        element_count=4;
        OCR1A=12000;
        count=8;
        TCCR1B |= (1 << WGM12);   // CTC mode
        TCCR1B |= (1 << CS12)|(1<<CS10);    // 1024 prescaler
       }
}

The last part.
Thanks,
Manuel

//***********************************************************
/*The following routine selects the different menus controled
by a potentiometer.*/

void display_data()
{
  pot_value =analogRead(read_pot_pin);

if ((pot_value>=0) && (pot_value <=255))
      {
     if(screen!=1)
      { 
       lcd.clear();
       gps_position_print(); 
      }
      else
      {
      gps_position_print();
      }
      }
 if ((pot_value>=256) && (pot_value <=512)) 
      {
     if(screen!=3)
       {  
     lcd.clear();  
     motor_data_print();
       }
       else
       {
       motor_data_print();
       }
      }
 if ((pot_value>=513) && (pot_value <=768))    
      {
      if(screen!=2)
       {
        lcd.clear(); 
        flight_data_print();
       }
       else
       {
       flight_data_print();  
       }
    }
 if ((pot_value>=769) && (pot_value <=1023))    
      {
      if(screen!=4)
        {
        lcd.clear();
        max_data_print();
        }
        else
        {
        max_data_print();
        }  
   
    }
}  

void save_coordenadas()
{
 coordenadas=0;
 first_latitude=actual_latitude;
 first_longitude=actual_longitude;
}

void calc_distancia()
{
float delta_lat_distance=0;
float delta_long_distance=0;
delta_latitude=0;  
delta_longitude=0;

if (actual_latitude >= first_latitude)
   {
     delta_latitude=(actual_latitude - first_latitude);
   }
   else
   {
     delta_latitude=(first_latitude - actual_latitude);
   }
   
if (actual_longitude>= first_longitude)
    {
     delta_longitude=(actual_longitude - first_longitude);
    }
     else
    {
     delta_longitude=(first_longitude - actual_longitude); 
    }
    
   delta_lat_distance=delta_latitude*umgrau_lat*1000;
   
   delta_long_distance=delta_longitude*umgrau_long*1000;


flat_distance= sqrt(sq(delta_lat_distance)+ sq(delta_long_distance)) ;
distance = sqrt(sq(flat_distance)+ sq(altit_relativa)) ;
}










//DISPLAY CONTROL
//***************************
// LCD print GPS data
//**********************
void gps_position_print()
 {
  screen=1;
  lcd.setCursor(4,0);
  lcd.print("  GPS DATA  ");

// LCD print latitude 
  lcd.setCursor(0, 1);
  lcd.print("Lat:");
  lcd.print(actual_latitude,6);
  lcd.print(lat);
  
// LCD ptint longitude  
 lcd.setCursor(0, 2);
  lcd.print("Lgt:");
  lcd.print(actual_longitude,6);
  lcd.print(lgt);  

// LCD print altitude
  lcd.setCursor(0, 3);
  lcd.print("alt:");
  lcd.print(z_int=al_titude/100);
  lcd.print(".");
  lcd.print(z=al_titude%100);
  lcd.print("m");

// LCD print ground speed
  lcd.setCursor(0, 4);
  lcd.print("vel:");
  lcd.print(g_speed);
  lcd.print("Km/h");
  lcd.setCursor(0, 5);
  lcd.print("Satelites = ");
  lcd.print(GPS_data[22],DEC);
  } 

// LCD print flight data
//***********************
void flight_data_print() 
{
  screen=2;
  lcd.setCursor(5,0);
  lcd.print("Flight Data");
  lcd.setCursor(0,1);
  lcd.print("Altit. =");
  lcd.print(altitude_barometric,1);
  lcd.print("m");
  altit_relativa=altitude_barometric-altit_baro_zero;
  lcd.setCursor(0, 2);
  lcd.print("Alt.Rlt.=");
  lcd.print(altit_relativa,1);
  lcd.print("m");
  lcd.setCursor(0, 3);
  lcd.print("Vel= ");
  lcd.print(g_speed);
  lcd.print("Km/h");
  lcd.setCursor(0, 4);
  lcd.print("G_dist.=");
  lcd.print(flat_distance,1);
  lcd.print("m");
  lcd.setCursor(0, 5);
  lcd.print("Dist= ");
  lcd.print(distance,1);
  lcd.print("m");
  
  
}
//**************************************************************

// LCD motor data print
//************************
void motor_data_print()
{
  screen=2; 
// LCD batery voltage print
  lcd.setCursor(4, 0);
  lcd.print(" Motor Data");
  lcd.setCursor(0, 1);
  lcd.print("Bat.Vlt.=");
  lcd.print(bat_voltage,2);

// LCD RPM value print
  lcd.setCursor(0, 2);
  lcd.print("RPM=");
  lcd.print(RPM_value);

  //lcd.print("    ");
  
//Battery Dischage Calculation
  lcd.setCursor(0, 3);
  lcd.print("mAH =");
  lcd.print(mAH,0);
  lcd.setCursor(0, 4);
  lcd.print("I=");
  lcd.print(I_instantaneo,2);
  lcd.print(" ");
  lcd.print("C=");
  lcd.print(volt_element);
  
  
 //Battery Temperature Calculation
  lcd.setCursor(0, 5);
  lcd.print("Bat.temp.=");
  lcd.print(temperature,1);
}

void max_values()
{

if (I_instantaneo > I_max)
    I_max=I_instantaneo;
    
if (RPM_value > rpm_max)
   rpm_max = RPM_value;
   
if (temperature > bat_temp_max)
    bat_temp_max = temperature;
    
if (altit_relativa > altit_baro_max)
    altit_baro_max = altit_relativa;
    
if (g_speed > veloc_max)
    veloc_max = g_speed;

if ((al_titude/100) > altit_max)
    altit_max = (al_titude/100);
    
}
    

void max_data_print()
 {
 // Max. Values Print
 // Current Max. print
  lcd.setCursor(0, 0);
  lcd.print(" Max Values ");
  lcd.setCursor(0, 1);
  lcd.print("I Max.=");
  lcd.print(I_max,1);

// Max. RPM value print
  lcd.setCursor(0, 2);
  lcd.print("RPM= ");
  lcd.print(rpm_max);

//Max. temperature Print
  lcd.setCursor(0, 3);
  lcd.print("Temp.= ");
  lcd.print(bat_temp_max,1);
  
//Max. Barometric Altitude  
  lcd.setCursor(0, 4);
  lcd.print("Bar.Alt.=");
  lcd.print(altit_baro_max,1);
  
 //Max. Speed Print
  lcd.setCursor(0, 5);
  lcd.print("Speed =");
  lcd.print(veloc_max);
  lcd.print(" Km/h");
 }
 
 void set_alarm_volt()
{

  uint8_t volt_element_OK=0;
  lcd.clear();
  lcd.setCursor(4,0);
  lcd.print("Cell Volts");

  lcd.setCursor(0,2);
  lcd.print("Act.Val.=");
  lcd.print(volt_element,2);
  while (switch_state == LOW)
    {
     pot_value =analogRead(read_pot_pin);
     volt_element_adj = (pot_value*bit_value);
     delay(200);
     if ((volt_element_adj >=3.0) && (volt_element_adj <=4.0))
         {
         lcd.setCursor(0,4);
         lcd.print("New Val.=");   
         lcd.print(volt_element_adj,2);
         //lcd.setCursor(0,4);
         lcd.print(" ");
         volt_element_OK=1;   
         }
     else
         {
         lcd.setCursor(0,4);
         lcd.print("N_Valid.=");
         lcd.print(volt_element_adj,2);
         lcd.print(" ");
         volt_element_OK=0;             
         }
      switch_state = digitalRead(switchpin);   
    }
   lcd.clear();
   delay(1);
   if (volt_element_OK ==1)
       {
        volt_element = volt_element_adj;
        void eepromWriteFloat(int address, float volt_element);
        lcd.clear();
        lcd.setCursor(0,2);
        lcd.print("  Val. Stored");
        lcd.setCursor(10,3);
        lcd.print(volt_element,2);
        delay(1000);
        lcd.clear();
       }
  }
 
/* This routine is to keep tracking of the battery voltage and when
it reachs the alarm level by element it sounds an alarme.*/  

void bat_volt_test()
{
  if(bat_voltage <= (volt_element*element_count))
     {
      OCR1A=2250;
      count=8;
      TCCR1B |= (1 << WGM12);   // CTC mode
      TCCR1B |= (1 << CS12)|(1<<CS10);    // 1024 prescaler
      }  
}


/***********************************************************
 This routine sets the alarm sequence, when at the switch On the receiver
 the battery is below the aceptable voltage*/
  
void alarm_sequence()
      {
      OCR1A=1000; // 0.5 seconds On and OFF
      count=100;
      TCCR1B |= (1 << WGM12);   // CTC mode
      TCCR1B |= (1 << CS12)|(1<<CS10); // CTC mode and 1024 prescaler
      }


/* This routine turns ON the speaker and counts the number of bips.
   Each interrupt it toggles the speaker and at the end make sure it is OFF*/
 
ISR(TIMER1_COMPA_vect)          // timer compare interrupt service routine
{
  digitalWrite(buzzerpin, digitalRead(buzzerpin) ^ 1);   // toggle speaker pin
  count--;
  if (count==0)
  {
   TCCR1B =0;  // Clear bits. stop the timer
   digitalWrite(buzzerpin, LOW);
  }  
} 




void eepromWriteFloat(int address, float volt_element)
{
   union u_tag
   {
     byte b[4];
     float fval;
   } u;
   u.fval=volt_element;
 
   EEPROM.write(address  , u.b[0]);
   EEPROM.write(address+1, u.b[1]);
   EEPROM.write(address+2, u.b[2]);
   EEPROM.write(address+3, u.b[3]);
}


float eepromReadFloat(int address)
  {
   union u_tag 
   {
     byte b[4];
     float volt_element;
   } u;   
   u.b[0] = EEPROM.read(address);
   u.b[1] = EEPROM.read(address+1);
   u.b[2] = EEPROM.read(address+2);
   u.b[3] = EEPROM.read(address+3);
   return u.volt_element;
}

I recommend you use the second approach recommended by AWOL.

Posting a complete sketch is a good start, but I’d suggest that for your sake as well as ours, when you’re trying to solve a problem like this and get as far as asking for help, that you start by producing the smallest simplest sketch that you can which demonstrates the problem. In this case it would be a sketch which saves a value to EEPROM and then reads it back.

PeterH thanks,

Following your advise I wrote a program only with the code involved in the float write/read to EEPROM.

Thanks,
Manuel

#include <DogLcd.h>
#include <PCD8544.h>
#include "EEPROM.h" 





float volt_element=0.0; // low limit alarm voltage cab be adjusted, default value
float volt_element_adj=0.0;
const uint8_t switchpin=10;
uint8_t switch_state=0;
int read_pot_pin = A0;
int pot_value = 0;
int address=0;
const float bit_value=0.004852;



static PCD8544 lcd;



void setup()
{
  // PCD8544-compatible displays may have a different resolution...
  lcd.begin(84, 48);
  lcd.clear();
  Serial.begin(38400);
  pinMode(switchpin, INPUT);
}

void loop()
{
volt_element = eepromReadFloat(address); //load the alarme voltage limit by battery element
Serial.println(volt_element,2);

for(;;)
{
  
    switch_state = digitalRead(switchpin);

    if (switch_state == LOW)
        {
        set_alarm_volt();
        }  
  }
}  





 void set_alarm_volt()
{
  uint8_t volt_element_OK=0;
  lcd.clear();
  lcd.setCursor(4,0);
  lcd.print("Cell Volts");

  lcd.setCursor(0,2);
  lcd.print("Act.Val.=");
  lcd.print(volt_element,2);
  while (switch_state == LOW)
    {
     pot_value =analogRead(read_pot_pin);
     volt_element_adj = (pot_value*bit_value);
     delay(200);
     if ((volt_element_adj >=3.0) && (volt_element_adj <=4.0))
         {
         lcd.setCursor(0,4);
         lcd.print("New Val.=");   
         lcd.print(volt_element_adj,2);
         Serial.print("New Val.=");
         Serial.println(volt_element_adj,2);
         //lcd.setCursor(0,4);
         lcd.print(" ");
         volt_element_OK=1;   
         }
     else
         {
         lcd.setCursor(0,4);
         lcd.print("N_Valid.=");
         lcd.print(volt_element_adj,2);
         Serial.print("Not Val.=");
         Serial.println(volt_element_adj,2);
         lcd.print(" ");
         volt_element_OK=0;             
         }
      switch_state = digitalRead(switchpin);   
    }
   lcd.clear();
   delay(1);
   if (volt_element_OK ==1)
       {
        volt_element = volt_element_adj;
       int address=0;
       void eepromWriteFloat(int address, float volt_element);
        lcd.clear();
        lcd.setCursor(0,2);
        lcd.print("  Val. Stored");
        Serial.println("Val. Stored");
        lcd.setCursor(10,3);
        lcd.print(volt_element,2);
        delay(1000);
        lcd.clear();
       }
  }  

void eepromWriteFloat(int address, float volt_element)
{
   union u_tag
   {
     byte b[4];
     float fval;
   } u;
   u.fval=volt_element;
 
   EEPROM.write(address  , u.b[0]);
   EEPROM.write(address+1, u.b[1]);
   EEPROM.write(address+2, u.b[2]);
   EEPROM.write(address+3, u.b[3]);
}


float eepromReadFloat(int address)
  {
   union u_tag 
   {
     byte b[4];
     float volt_element;
   } u;   
   u.b[0] = EEPROM.read(address);
   u.b[1] = EEPROM.read(address+1);
   u.b[2] = EEPROM.read(address+2);
   u.b[3] = EEPROM.read(address+3);
   return u.volt_element;
}
   u.b[0] = EEPROM.read(address);
   u.b[1] = EEPROM.read(address+1);
   u.b[2] = EEPROM.read(address+2);
   u.b[3] = EEPROM.read(address+3);

You should meet my friend for.

PaulS, thanks for your info.

I went look into the C book to understand the union and it looks like for me that its OK. I changed the code according your inputs, but, it continues to not write or read, I do not know which one do not work or if both.

Thanks,
Manuel

float eepromReadFloat(int address)
  {
   uint8_t i=0; 
   union u_tag 
   {
     byte b[4];
     float volt_element;
   } u; 
  
     for (i=0; i<=3; i++, address++)
        {
         u.b[i] = EEPROM.read(address);
        }
      return u.volt_element;  

}

What does your debug output look like?

The compiler doesn’t give any error. I found that the write function is not called. I tried to change the position call but didn’t happen.

I think it is the problem.

Thanks,
Manuel

 void set_alarm_volt()
{
  uint8_t volt_element_OK=0;
  lcd.clear();
  lcd.setCursor(4,0);
  lcd.print("Cell Volts");

  lcd.setCursor(0,2);
  lcd.print("Act.Val.=");
  lcd.print(volt_element,2);
  while (switch_state == LOW)
    {
     pot_value =analogRead(read_pot_pin);
     volt_element_adj = (pot_value*bit_value);
     delay(200);
     if ((volt_element_adj >=3.0) && (volt_element_adj <=4.0))
         {
         lcd.setCursor(0,4);
         lcd.print("New Val.=");   
         lcd.print(volt_element_adj,2);
         Serial.print("New Val.=");
         Serial.println(volt_element_adj,2);
         //lcd.setCursor(0,4);
         lcd.print(" ");
         volt_element_OK=1;   
         }
     else
         {
         lcd.setCursor(0,4);
         lcd.print("N_Valid.=");
         lcd.print(volt_element_adj,2);
         Serial.print("Not Val.=");
         Serial.println(volt_element_adj,2);
         lcd.print(" ");
         volt_element_OK=0;             
         }
      switch_state = digitalRead(switchpin);   
    }
   lcd.clear();
   delay(1);
   if (volt_element_OK ==1)
       {
        volt_element = volt_element_adj;
        address=0;
        lcd.clear();
        lcd.setCursor(0,2);
        lcd.print("  Val. Stored");
        Serial.println("Val. Stored");
        lcd.setCursor(10,3);
        lcd.print(volt_element,2);
        Serial.println(volt_element,2);
        [color=blue]void eepromWriteFloat(int address, float volt_element);[/color]  
       delay(1000);
        lcd.clear();
       }
    }  


void eepromWriteFloat(int address, float volt_element)
{
    Serial.println("step here");
   uint8_t i=0;
   union u_tag
   {
     byte b[4];
     float fval;
   } u;
   u.fval=volt_element;
 
    for (i=0; i<=3; i++, address++)
        {
         EEPROM.write(address,u.b[i]);
         Serial.println(u.b[i],HEX);
         Serial.println("step here");
        }
 
  // EEPROM.write(address  , u.b[0]);
  // EEPROM.write(address+1, u.b[1]);
  // EEPROM.write(address+2, u.b[2]);
  // EEPROM.write(address+3, u.b[3]);
}

I already solved the problem. The function call was wrong. it was;

void eepromWriteFloat(int address, float volt_element);

It should be:

eepromWriteFloat(address, volt_element);

This is caused by copy/past.

Thanks to you all.
Manuel

The code to write and read a float from EEPROM is below. This may help other people.
Code:

float value=0.0;
int address=0;


void setup()
( )

void loop()
(
eepromWriteFloat(address, volt_element);

value = eepromReadFloat(address);
)


void eepromWriteFloat(int address, float Value)
{
   uint8_t i=0;
   union u_tag
   {
     byte b[4];
     float fval;
   } u;
   u.fval=value;
 
    for (i=0; i<=3; i++, address++)
        {
         EEPROM.write(address,u.b[i]);
        }
}


float eepromReadFloat(int address)
  {
   uint8_t i=0; 
   union u_tag 
   {
     byte b[4];
     float volt_element;
   } u;   

     for (i=0; i<=3; i++, address++)
        {
         u.b[i] = EEPROM.read(address);
        }
      return u.volt_element;  

}