Go Down

Topic: Code won't compile (Read 4128 times) previous topic - next topic

billcramer07

The following sketch will compile in version 21 and below, but not in version 22. Any ideas?
It writes multiple bytes to EEPROM. Been running it for some time in a larger sketch. Upgraded to version 22 yesterday.

#include <EEPROM.h>
float Kwh = 141.00;
float KwhHourstart = 0.000;
float KwhDaystart = 141.000;
template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
   const byte* p = (const byte*)(const void*)&value;
   int i;
   for (i = 0; i < sizeof(value); i++)
       EEPROM.write(ee++, *p++);
   return i;
}

template <class T> int EEPROM_readAnything(int ee, T& value)
{
   byte* p = (byte*)(void*)&value;
   int i;
   for (i = 0; i < sizeof(value); i++)
       *p++ = EEPROM.read(ee++);
   return i;
}

void setup()
{
   Serial.begin(9600);
   EEPROM_writeAnything(20, Kwh);
   EEPROM_writeAnything(24, KwhHourstart);
   EEPROM_writeAnything(28, KwhDaystart);
   EEPROM_readAnything(20,Kwh);
   Serial.print(Kwh,3);
   
   
}
void loop()
{
}

PaulS

This has been discussed here, before. No conclusions drawn/posted as to why, but a workaround was defined.

You need to put the template stuff in a .h file, and include that in the sketch.

billcramer07

Thanks... was not aware of a previous post.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy