Translate

Saturday, 14 June 2014

Room Management system – The Menu part 14 – with EEPROM


Working myself through the memory of the Atmega 328 pu chip was quite a challenge and get ready for a bit of work to be done.

To start of with, a small explanation what we need to do to make use of the EEPROM and it's limitations. First of all on the Atmega 328 we have 1024 bytes of usable EEPROM and now we see how we can make use of it. We all now that computer chips store things as binary code. Let's have a quick look again into bits, bytes and binary.
A byte consists of 8 bits and looking in to binary, a 8 bit binary consisting of 8 digits eider 0 or 1 can give us any number between 0 and 255.
00000000 in decimal is still 0,
00000001 in decimal is still 1,
00000010 in decimal is 2
00000011 in decimal is 3
00000100 in decimal is 4
00000101 in decimal is 5
…...
11111111 in decimal is 255

That means we can store any number between 0 and 255 in each one of the 1024 bytes of the EEPROM

If we need to store a larger number than 255 we need a second byte to do this, which would be a 16 bit binary.
0000000100000000 in decimal is 256
0000001000000000 in decimal is 512
0000001100000000 in decimal is 768
…....
11111111111111111 in decimal is 65535

65535 is also the max. value a variable declared as (unsigned int) can hold and that are the max values we are working with in our sketch. However, to store the values in the EEPROM we need to check first if we have a 1 byte value (0 – 255) or a unsigned integer value (256 – 65535). We need to know that because the memory space is also addressed from 0 to 1024, meaning, as long as we have only 1 byte values the memory address increases by 1 with every storage space. Soon as we have a value larger than 255, in our case it will be a 2 byte value. That again means we need to reserve 2 bytes of memory space for it.

To deal with assigning a address to a value, so we find it again to use in the program there are kind of two possibilities.
The first one is writing arrays with addresses corresponding to the array holding the values so for example value[x][y][z] corresponds with value_ADDRESS[x][y][z] or supposed, there is a way to calculate the addresses within a data structure, which I haven't got to work yet. For now we stick to option 1.

Since we the write and erase cycles of the EEPROM memory are limited to about 100.000, we also want to be careful in how often we write to the EEPROM. Not to re upload the data any time there was a power cut or the program restarts for what ever reason, I wrote a separate sketch to upload the data.

Lets start with including the standard EEPROM library:

///////////////// INCLUDES //////////////////

#include <EEPROM.h>

/////////////////Declaring the variables///////////////

void setup() {

// put your setup code here, to run once:

}

void loop() {

// put your main code here, to run repeatedly:

}

Now we move the variable declarations and there values from the main sketch:

/*int delayTime[16] = {dBed1, dBed2, dBed3, dLiving, dBath1, dBath2, dBath3,

dBath4, dKitchen, dCorridor, dAC1, dAC2, dAC3, dAC4,

dMaster, 0};*/

int delayTime[15] = {300, 300, 300, 600, 600, 600, //delay times in seconds to the same order as above

600, 300, 120, 120, 120, 120,

120, 120, 240};

byte startDelay = 0; //var to optional ac compressor start delay

byte acSwitchDelay = 2; //ac pulse delay for mode 3

byte ac_op_mode = 3; //ac mode 1) read switches only

//ac mode 2) limited control using ac switch or auto restart of AC

//ac mode 3) limited control using ac momentary switch (push button)

byte ac_set_temp = 28; //temperature at which the AC switches on

byte ac_periode[4][2] = {

{1, 5}, //time periode between January and May

{6, 9}, //time periode between June and September

{10, 10}, //October

{11, 12} //time periode between November and December

};

byte ac_master_bypass[4] = {0}; //array holding values to bypass master relay

//////////////////////holiday and AC timer settings//////////////////////

byte timer_active[16][4] = {

{1, 1, 1, 0}, //room 0 timers 0 to 3

{1, 1, 0, 0}, //room 1 timers 0 to 3

{1, 1, 1, 1}, //room 2 timers 0 to 3

{0, 1, 0, 0}, //room 3 timers 0 to 3

{1, 1, 2, 2}, //room 4 timers 0 to 3

{1, 1, 2, 2}, //room 5 timers 0 to 3

{1, 1, 2, 2}, //room 6 timers 0 to 3

{1, 0, 2, 2}, //room 7 timers 0 to 3

{1, 1, 2, 2}, //room 8 timers 0 to 3

{0, 0, 2, 2}, //room 9 timers 0 to 3

{1, 1, 1, 0}, //room 0 AC timers 0 to 3

{1, 1, 1, 0}, //room 1 AC timers 0 to 3

{1, 1, 1, 0}, //room 2 AC timers 0 to 3

{1, 1, 2, 0}, //room 3 AC timers 0 to 3

{2, 2, 2, 2}, //Dummy room

{0, 1, 2, 2} //outside lighting

};

//Timer Settings room, timer, hour on, minute on, hour off, minute off

byte room_timers[16][4][4] = {

{

{5, 35, 6, 5}, //room 0 timer 0

{19, 35, 20, 15}, //room 0 timer 1

{21, 5, 21, 15}, //room 0 timer 2

{0, 0, 0, 0} //room 0 timer 3

},

{

{6, 30, 6, 50}, //room 1 timer 1

{19, 30, 20, 10}, //room 1 timer 2

{0, 0, 0, 0}, //room 1 timer 3

{0, 0, 0, 0} //room 1 timer 4

},

{

{5, 50, 6, 20}, //room 2 timer 1

{18, 10, 18, 25}, //room 2 timer 2

{19, 15, 19, 40}, //room 2 timer 3

{23, 20, 23, 35} //room 2 timer 4

},

{

{0, 0, 0, 0}, //room 3 timer 1

{17, 30, 23, 30}, //room 3 timer 2

{0, 0, 0, 0}, //room 3 timer 3

{0, 0, 0, 0} //room 3 timer 4

},

{

{5, 40, 5, 45}, //room 4 timer 1

{19, 55, 20, 10}, //room 4 timer 2

{0, 0, 0, 0}, //not used

{0, 0, 0, 0} //not used

},

{

{6, 35, 6, 45}, //room 5 timer 1

{19, 50, 20, 5}, //room 5 timer 2

{0, 0, 0, 0}, //not used

{0, 0, 0, 0} //not used

},

{

{6, 5, 6, 25}, //room 6 timer 1

{22, 50, 23, 15}, //room 6 timer 2

{0, 0, 0, 0}, //not used

{0, 0, 0, 0} //not used

},

{

{0, 0, 0, 0}, //room 7 timer 1

{22, 5, 22, 20}, //room 7 timer 2

{0, 0, 0, 0}, //not used

{0, 0, 0, 0} //not used

},

{

{5, 50, 6, 45}, //room 8 timer 1

{17, 45, 18, 30}, //room 8 timer 2

{0, 0, 0, 0}, //room 8 timer 3

{0, 0, 0, 0} //not used

},

{

{0, 0, 0, 0}, //room 9 timer 1

{0, 0, 0, 0}, //room 9 timer 2

{0, 0, 0, 0}, //not used

{0, 0, 0, 0} //not used

},

{

{19, 30, 22, 0}, //room 0 AC timer 1

{19, 30, 5, 30}, //room 0 AC timer 2

{19, 30, 22, 0}, //room 0 AC timer 3

{0, 0, 0, 0} //room 0 AC timer 4

},

{

{19, 30, 22, 0}, //room 1 AC timer 1

{19, 30, 5, 30}, //room 1 AC timer 2

{19, 30, 22, 0}, //room 1 AC timer 3

{0, 0, 0, 0} //room 1 AC timer 4

},

{

{21, 30, 1, 0}, //room 2 AC timer 1

{21, 30, 6, 0}, //room 2 AC timer 2

{21, 30, 1, 0}, //room 2 AC timer 3

{0, 0, 0, 0} //room 2 AC timer 4

},

{

{13, 0, 20, 0}, //room 3 AC timer 1

{5, 0, 23, 59}, //room 3 AC timer 2

{6, 0, 20, 0}, //room 3 AC timer 3

{0, 0, 0, 0} //room 3 AC timer 4

},

{

{0, 0, 0, 0}, //Dummy timer not used

{0, 0, 0, 0},

{0, 0, 0, 0},

{0, 0, 0, 0}

},

{

{0, 0, 0, 0}, //outside lights timer 1

{17, 3, 23, 59}, //outside lights timer 2

{0, 0, 0, 0}, //outside lights timer 3

{0, 0, 0, 0} //outside lights timer 4

}

};

byte ac_forced_on[4][2] = {

{19, 30}, //Switch on AC room 1

{19, 30}, //Switch on AC room 2

{22, 0}, //Switch on AC room 3

{16, 0} //Switch on AC room 4

};

The whole lot above, we copy and paste it into the new sketch under the section “Declaring the variables”.

The next thing I did was copying the arrays holding the values and replaced them with addresses. Careful, there are some values needing 2 bytes storage and not to mix up the variables and matching addresses, I just added a _ADDR to the variable or array name like below:

int eepromValue = 0;

const byte EEPROM_ID = 0x99;

const int ID_ADDR = 0;

const byte Sensitivity_ADDR = 1; //2 byte value

const byte photoCellCutOff_ADDR = 3; //2 byte value

const byte photoOutsideOff_ADDR = 5; //2 byte value

const int startDelay_ADDR = 374; //1 byte value

const int acSwitchDelay_ADDR = 375; //1 byte value

const int ac_op_mode_ADDR = 376; //1 byte value

const int ac_set_temp_ADDR = 377; //1 byte value

const byte delayTime_ADDR[15] = {7, 9, 11, 13, 15, 17,

19, 21, 23, 25, 27, 29,

31, 33, 35};

const byte timer_active_ADDR[16][4] = {

{37, 38, 39, 40},

{41, 42, 43, 44},

{45, 46, 47, 48},

{49, 50, 51, 52},

{53, 54, 55, 56},

{57, 58, 59, 60},

{61, 62, 63, 64},

{65, 66, 67, 68},

{69, 70, 71, 72},

{73, 74, 75, 76},

{77, 78, 79, 80},

{81, 82, 83, 84},

{85, 86, 87, 88},

{89, 90, 91, 92},

{93, 94, 95, 96},

{97, 98, 99, 100}

};

const int room_timers_ADDR[16][4][4] = {

{

{102, 103, 104, 105},

{106, 107, 108, 109},

{110, 111, 112, 113},

{114, 115, 116, 117}

},

{

{118, 119, 120, 121},

{122, 123, 124, 125},

{126, 127, 128, 129},

{130, 131, 132, 133}

},

{

{134, 135, 136, 137},

{138, 139, 140, 141},

{142, 143, 144, 145},

{146, 147, 148, 149}

},

{

{150, 151, 152, 153},

{154, 155, 156, 157},

{158, 159, 160, 161},

{162, 163, 164, 165}

},

{

{166, 167, 168, 169},

{170, 171, 172, 173},

{174, 175, 176, 177},

{178, 179, 180, 181}

},

{

{182, 183, 184, 185},

{186, 187, 188, 189},

{190, 191, 192, 193},

{194, 195, 196, 197}

},

{

{198, 199, 200, 201},

{202, 203, 204, 205},

{206, 207, 208, 209},

{210, 211, 212, 213}

},

{

{214, 215, 216, 217},

{218, 219, 220, 221},

{222, 223, 224, 225},

{226, 227, 228, 229}

},

{

{230, 231, 232, 233},

{234, 235, 236, 237},

{238, 239, 240, 241},

{242, 243, 244, 245}

},

{

{246, 247, 248, 249},

{250, 251, 252, 253},

{254, 255, 256, 257},

{258, 259, 260, 261}

},

{

{262, 263, 264, 265},

{266, 267, 268, 269},

{270, 271, 272, 273},

{274, 275, 276, 277}

},

{

{278, 279, 280, 281},

{282, 283, 284, 285},

{286, 287, 288, 289},

{290, 291, 292, 293}

},

{

{294, 295, 296, 297},

{298, 299, 300, 301},

{302, 303, 304, 305},

{306, 307, 308, 309}

},

{

{310, 311, 312, 313},

{314, 315, 316, 317},

{318, 319, 320, 321},

{322, 323, 324, 325}

},

{

{326, 327, 328, 329},

{330, 331, 332, 333},

{334, 335, 336, 337},

{338, 339, 340, 341}

},

{

{342, 343, 344, 345},

{346, 347, 348, 349},

{350, 351, 352, 353},

{354, 355, 356, 357}

}

};

const int ac_forced_on_ADDR[4][2] = {

{358, 359},

{360, 361},

{363, 363},

{364, 365}

};

const int ac_periode_ADDR[4][2] = {

{366, 367},

{368, 369},

{370, 371},

{372, 373}

};

const int ac_master_bypass_ADDR[4] = {378, 379, 380, 381}; 

I know, if I can get the method of calculating the storage address to work will save a lot of typing. But for now that's what I can come up with and again after putting all this work in and see the unit in function after I guess it is still worth the effort.
Also for later use and to minimize the write operations to the eeprom, I wrote a few functions since the original EEPROM library gives us only the option to read from or write to the EEPROM. To minimize the write cycles I also wrote a little function to check the stored value and over write the stored value only if the stored value is different from the value we want to update with. For example we change a timer from lets say 05:35 to 05:40 which is stored in 2 different cells. The hour (5) is stored in 1 cell of the eeprom and the minutes (35) in another cell. The program gives us the whole set as update and the standard EEPROM.write(address, value) command just overwrites the given address with the given value. In this case we only need to update the cell holding the minutes since the hour did not change.

Let's have a closer look at the functions and the first one we will need in this sketch is the one to write a 1 byte value into the EEPROM.

It is pretty straight forward I think. We are entering the function with the needed variables, the address where to write to and the value to write followed by the actual write command and a 5 millisecond delay to make sure the program has finished writing before it can deal with a following write request. According to documentation it takes 3,3 milliseconds to write 1 byte to the EEPROM.

void Mem_writeByte(int address, byte value){

EEPROM.write(address, value);

delay(5);

}

The second function we need is the one writing a integer (2 byte) value. There we have to split our value in a low Byte, the 8 bits of our binary counting from 0 to 255 and a high Byte, the 8 bits of the binary counting from 256 to 65535.
We are entering the function again with the assigned address and the value to write. Now we call again the function Mem_writeByte with the given base address and here we extract the high Byte of our value with highByte(value). Than we write the second byte with another function call to Mem_write byte an now important not to overwrite the address of the highByte(value), we need to add 1 to the assigned base address and than we extract the low Byte of our value and write it to the EEPROM.

void Mem_writeInt(int address, int value)

{

Mem_writeByte(address, highByte(value) );

Mem_writeByte(address+1, lowByte(value) );

}  

The next function we need is reading a byte back from the EEPROM, which again is pretty straight forward. We enter the function with the address of the value we want to extract and there we are:

byte Mem_readByte(int address){

byte value = EEPROM.read(address);

return value;

}

A little more exiting is to read back a integer (2 byte value), since we need to read the high Byte value, the low Byte value and put it back together to the stored value. First we read the high Byte value into a variable (hiByte) from the assigned base address. Second we read the low Byte value into a variable (loByte) from the base address + 1. Than we use the “word” command to set the two parts together and return the stored value.

int Mem_readInt(int address){

byte hiByte = EEPROM.read(address);

byte loByte = EEPROM.read(address+1);

int value = word(hiByte, loByte);

return value;

}

Since we have done read and write, we need the function which updates the cell only if the values are not the same. Only for a byte we read the given address in to a variable and compare it with the value we want to update with. If they are not the same we write the new value to the EEPROM other wise we do nothing.

void Mem_updateByte(int address, byte value){

byte reading = EEPROM.read(address);

if(reading != value) Mem_writeByte(address, value);

}

Doing the same with a two byte value, we need another step. Here we split the value again in high byte an d low Byte and than call the function Mem_updateByte to check the single cells if they are changed and update accordingly.

void Mem_updateInt(int address, int value){

Mem_updateByte(address, highByte(value)); //high byte

Mem_updateByte(address+1, lowByte(value)); //low byte

}

Since we have all our functions we need after to copy to the main sketch, we take care of writing the values to the EEPROM the first time. This we do in the setup loop:

Mem_writeByte(ID_ADDR, EEPROM_ID);

Mem_writeInt(Sensitivity_ADDR, 300);

Mem_writeInt(photoCellCutOff_ADDR, 320);

Mem_writeInt(photoOutsideOff_ADDR, 220);

for(int i=0; i<15; i++){

Mem_writeInt(delayTime_ADDR[i], delayTime[i]);

}

for(int x=0; x<16; x++){

for(int y=0; y<4; y++){

Mem_writeByte(timer_active_ADDR[x][y], timer_active[x][y]);

}

}

for(int x=0; x<16; x++){

for(int y=0; y<4; y++){

for(int z=0; z<4; z++){

Mem_writeByte(room_timers_ADDR[x][y][z], room_timers[x][y][z]);

}

}

}

for(int x=0; x<4; x++){

for(int y=0; y<2; y++){

Mem_writeByte(ac_forced_on_ADDR[x][y], ac_forced_on[x][y]);

}

}

for(int x=0; x<4; x++){

for(int y=0; y<2; y++){

Mem_writeByte(ac_periode_ADDR[x][y], ac_periode[x][y]);

}

}

Mem_writeByte(startDelay_ADDR, 0);

Mem_writeByte(acSwitchDelay_ADDR, 2);

Mem_writeByte(ac_op_mode_ADDR, 3);

Mem_writeByte(ac_set_temp_ADDR, 28);

for(int i=0; i<4; i++){

Mem_writeByte(ac_master_bypass_ADDR[i], ac_master_bypass[i]);

}

I guess, this part again is pretty straight forward. Apart from some single declared variables, we loop through our arrays holding the values and since the arrays holding the addresses are having the same structure, we use the same counter to retrieve the address like in

for(int i=0; i<15; i++){
Mem_writeInt(delayTime_ADDR[i], delayTime[i]);
}

The counter “i” is the same for the value in the delayTime[] array as for the corresponding address in the delayTime_ADDR[] array.

As final for today the complete sketch we need to store all the needed values into the EEPROM. Please, this sketch is intended to be used only once to write the needed variables for the Room Management System (Version 1.4.3 and above) in to the EEPROM.


///////////////////////////////////////////////////////////////////////////////////
////Variable upload program Version 1.0.0//////////////////////////////////////////
////intendet to use for first time variable upload to EEPROM of////////////////////
////a Atmega 328 chip or a Arduino UNO board to be used with the //////////////////
////Room Management System Version 1.4.3///////////////////////////////////////////
////by Dieter Achtelstetter////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
////This program is free software: you can redistribute it and/or modify///////////
////it under the terms of the GNU General Public License as published by///////////
////the Free Software Foundation, either version 3 of the License, or   ///////////
////(at your option) any later version.                                 ///////////
///////////////////////////////////////////////////////////////////////////////////
////This program is distributed in the hope that it will be useful,     ///////////
////but WITHOUT ANY WARRANTY; without even the implied warranty of      ///////////
////MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       ///////////
////GNU General Public License for more details.                        ///////////
///////////////////////////////////////////////////////////////////////////////////
////You should have received a copy of the GNU General Public License   ///////////
////along with this program.  If not, see <http://www.gnu.org/licenses/>.//////////
///////////////////////////////////////////////////////////////////////////////////



#include <EEPROM.h>

byte ac_master_bypass[4] = {0};       //array holding values to bypass master relay


byte ac_periode[4][2] = {
                          {1, 5},     //time periode between January and May
                          {6, 9},     //time periode between June and September
                          {10, 10},   //October
                          {11, 12}    //time periode between November and December
};

int delayTime[15] = {300, 300, 300, 600, 600, 600,
                     600, 300, 120, 120, 120, 120, 
                     120, 120, 240};

byte ac_forced_on[4][2] = {
                            {19, 30},  //Switch on AC room 1
                            {19, 30},  //Switch on AC room 2
                            {22, 0},   //Switch on AC room 3
                            {16, 0}    //Switch on AC room 4
};

byte timer_active[16][4] = {
                    {1, 1, 1, 0},    //room 0 timers 0 to 3
                    {1, 1, 0, 0},    //room 1 timers 0 to 3
                    {1, 1, 1, 1},    //room 2 timers 0 to 3
                    {0, 1, 0, 0},    //room 3 timers 0 to 3
                    {1, 1, 2, 2},    //room 4 timers 0 to 3
                    {1, 1, 2, 2},    //room 5 timers 0 to 3
                    {1, 1, 2, 2},    //room 6 timers 0 to 3
                    {1, 0, 2, 2},    //room 7 timers 0 to 3
                    {1, 1, 2, 2},    //room 8 timers 0 to 3
                    {0, 0, 2, 2},    //room 9 timers 0 to 3
                    {1, 1, 1, 0},    //room 0 AC timers 0 to 3
                    {1, 1, 1, 0},    //room 1 AC timers 0 to 3
                    {1, 1, 1, 0},    //room 2 AC timers 0 to 3
                    {1, 1, 2, 0},    //room 3 AC timers 0 to 3
                    {2, 2, 2, 2},    //Dummy room 
                    {0, 1, 2, 2}     //outside lighting
};

byte room_timers[16][4][4] = {
                               {
                                 {5, 35, 6, 5},        //room 0 timer 0
                                 {19, 35, 20, 15},     //room 0 timer 1
                                 {21, 5, 21, 15},      //room 0 timer 2
                                 {0, 0, 0, 0}          //room 0 timer 3
                               },
                               {
                                 {6, 30, 6, 50},       //room 1 timer 1
                                 {19, 30, 20, 10},     //room 1 timer 2
                                 {0, 0, 0, 0},         //room 1 timer 3
                                 {0, 0, 0, 0}          //room 1 timer 4
                               },
                               {
                                 {5, 50, 6, 20},       //room 2 timer 1
                                 {18, 10, 18, 25},     //room 2 timer 2
                                 {19, 15, 19, 40},     //room 2 timer 3
                                 {23, 20, 23, 35}      //room 2 timer 4
                               },
                               {
                                 {0, 0, 0, 0},         //room 3 timer 1
                                 {17, 30, 23, 30},     //room 3 timer 2
                                 {0, 0, 0, 0},         //room 3 timer 3
                                 {0, 0, 0, 0}          //room 3 timer 4
                               },
                               {
                                 {5, 40, 5, 45},       //room 4 timer 1
                                 {19, 55, 20, 10},     //room 4 timer 2
                                 {0, 0, 0, 0},         //not used
                                 {0, 0, 0, 0}          //not used
                               },
                               {
                                 {6, 35, 6, 45},       //room 5 timer 1
                                 {19, 50, 20, 5},      //room 5 timer 2
                                 {0, 0, 0, 0},         //not used
                                 {0, 0, 0, 0}          //not used
                               },
                               {
                                 {6, 5, 6, 25},        //room 6 timer 1
                                 {22, 50, 23, 15},     //room 6 timer 2
                                 {0, 0, 0, 0},         //not used
                                 {0, 0, 0, 0}          //not used
                               },
                               {
                                 {0, 0, 0, 0},         //room 7 timer 1
                                 {22, 5, 22, 20},      //room 7 timer 2
                                 {0, 0, 0, 0},         //not used
                                 {0, 0, 0, 0}          //not used
                               },
                               {
                                 {5, 50, 6, 45},       //room 8 timer 1
                                 {17, 45, 18, 30},     //room 8 timer 2
                                 {0, 0, 0, 0},         //room 8 timer 3
                                 {0, 0, 0, 0}          //not used
                               },
                               {
                                 {0, 0, 0, 0},         //room 9 timer 1
                                 {0, 0, 0, 0},         //room 9 timer 2
                                 {0, 0, 0, 0},         //not used
                                 {0, 0, 0, 0}          //not used
                               },
                               {
                                 {19, 30, 22, 0},         //room 0 AC timer 1
                                 {19, 30, 5, 30},         //room 0 AC timer 2
                                 {19, 30, 22, 0},         //room 0 AC timer 3
                                 {0, 0, 0, 0}             //room 0 AC timer 4
                               },
                               {
                                 {19, 30, 22, 0},         //room 1 AC timer 1
                                 {19, 30, 5, 30},         //room 1 AC timer 2
                                 {19, 30, 22, 0},         //room 1 AC timer 3
                                 {0, 0, 0, 0}             //room 1 AC timer 4
                               },
                               {
                                 {21, 30, 1, 0},         //room 2 AC timer 1
                                 {21, 30, 6, 0},         //room 2 AC timer 2
                                 {21, 30, 1, 0},         //room 2 AC timer 3
                                 {0, 0, 0, 0}            //room 2 AC timer 4
                               },
                               {
                                 {13, 0, 20, 0},         //room 3 AC timer 1
                                 {5, 0, 23, 59},         //room 3 AC timer 2
                                 {6, 0, 20, 0},          //room 3 AC timer 3
                                 {0, 0, 0, 0}            //room 3 AC timer 4
                               },
                               {
                                 {0, 0, 0, 0},           //Dummy timer not used
                                 {0, 0, 0, 0},       
                                 {0, 0, 0, 0},         
                                 {0, 0, 0, 0}           
                               },
                               {
                                 {0, 0, 0, 0},           //outside lights timer 1
                                 {17, 3, 23, 59},        //outside lights timer 2
                                 {0, 0, 0, 0},           //outside lights timer 3
                                 {0, 0, 0, 0}            //outside lights timer 4
                               }
};

const int maxAllowedWrites = 350;
const byte EEPROM_ID = 0x99;

const int ID_ADDR = 0;
const byte Sensitivity_ADDR = 1; //2 byte value
const byte photoCellCutOff_ADDR = 3; //2 byte value
const byte photoOutsideOff_ADDR = 5; //2 byte value
const int startDelay_ADDR = 374; //1 byte value
const int acSwitchDelay_ADDR = 375; //1 byte value
const int ac_op_mode_ADDR = 376; //1 byte value
const int ac_set_temp_ADDR = 377; //1 byte value

const byte delayTime_ADDR[15] = {7, 9, 11, 13, 15, 17,
                                 19, 21, 23, 25, 27, 29, 
                                 31, 33, 35};


const byte timer_active_ADDR[16][4] = {
                                       {37, 38, 39, 40},
                                       {41, 42, 43, 44},
                                       {45, 46, 47, 48},
                                       {49, 50, 51, 52},
                                       
                                       {53, 54, 55, 56},
                                       {57, 58, 59, 60},
                                       {61, 62, 63, 64},
                                       {65, 66, 67, 68},
                                       
                                       {69, 70, 71, 72},
                                       {73, 74, 75, 76},
                                       {77, 78, 79, 80},
                                       {81, 82, 83, 84},
                                       
                                       {85, 86, 87, 88},
                                       {89, 90, 91, 92},
                                       {93, 94, 95, 96},
                                       {97, 98, 99, 100}
                                       };


const int room_timers_ADDR[16][4][4] = {
                                        {
                                          {102, 103, 104, 105},
                                          {106, 107, 108, 109},
                                          {110, 111, 112, 113},
                                          {114, 115, 116, 117}
                                        },
                                        {
                                          {118, 119, 120, 121},
                                          {122, 123, 124, 125},
                                          {126, 127, 128, 129},
                                          {130, 131, 132, 133}
                                        },
                                        {
                                          {134, 135, 136, 137},
                                          {138, 139, 140, 141},
                                          {142, 143, 144, 145},
                                          {146, 147, 148, 149}
                                        },
                                        {
                                          {150, 151, 152, 153},
                                          {154, 155, 156, 157},
                                          {158, 159, 160, 161},
                                          {162, 163, 164, 165}
                                        },
                                        {
                                          {166, 167, 168, 169},
                                          {170, 171, 172, 173},
                                          {174, 175, 176, 177},
                                          {178, 179, 180, 181}
                                        },
                                        {
                                          {182, 183, 184, 185},
                                          {186, 187, 188, 189},
                                          {190, 191, 192, 193},
                                          {194, 195, 196, 197}
                                        },
                                        {
                                          {198, 199, 200, 201},
                                          {202, 203, 204, 205},
                                          {206, 207, 208, 209},
                                          {210, 211, 212, 213}
                                        },
                                        {
                                          {214, 215, 216, 217},
                                          {218, 219, 220, 221},
                                          {222, 223, 224, 225},
                                          {226, 227, 228, 229}
                                        },
                                        {
                                          {230, 231, 232, 233},
                                          {234, 235, 236, 237},
                                          {238, 239, 240, 241},
                                          {242, 243, 244, 245}
                                        },
                                        {
                                          {246, 247, 248, 249},
                                          {250, 251, 252, 253},
                                          {254, 255, 256, 257},
                                          {258, 259, 260, 261}
                                        },
                                        {
                                          {262, 263, 264, 265},
                                          {266, 267, 268, 269},
                                          {270, 271, 272, 273},
                                          {274, 275, 276, 277}
                                        },
                                        {
                                          {278, 279, 280, 281},
                                          {282, 283, 284, 285},
                                          {286, 287, 288, 289},
                                          {290, 291, 292, 293}
                                        },
                                        {
                                          {294, 295, 296, 297},
                                          {298, 299, 300, 301},
                                          {302, 303, 304, 305},
                                          {306, 307, 308, 309}
                                        },
                                        {
                                          {310, 311, 312, 313},
                                          {314, 315, 316, 317},
                                          {318, 319, 320, 321},
                                          {322, 323, 324, 325}
                                        },
                                        {
                                          {326, 327, 328, 329},
                                          {330, 331, 332, 333},
                                          {334, 335, 336, 337},
                                          {338, 339, 340, 341}
                                        },
                                        {
                                          {342, 343, 344, 345},
                                          {346, 347, 348, 349},
                                          {350, 351, 352, 353},
                                          {354, 355, 356, 357}
                                        }
};

const int ac_forced_on_ADDR[4][2] = {
                                      {358, 359},
                                      {360, 361},
                                      {363, 363},
                                      {364, 365}
};
     

const int ac_periode_ADDR[4][2] = {
                                    {366, 367},
                                    {368, 369},
                                    {370, 371},
                                    {372, 373}
};  

const int ac_master_bypass_ADDR[4] = {378, 379, 380, 381}; 

void setup() {

  Mem_writeByte(ID_ADDR, EEPROM_ID);

  Mem_writeInt(Sensitivity_ADDR, 300);

  Mem_writeInt(photoCellCutOff_ADDR, 320);

  Mem_writeInt(photoOutsideOff_ADDR, 220);

for(int i=0; i<15; i++){
       Mem_writeInt(delayTime_ADDR[i], delayTime[i]);
     }

  for(int x=0; x<16; x++){
    for(int y=0; y<4; y++){
     
      Mem_writeByte(timer_active_ADDR[x][y], timer_active[x][y]);
    }
  }
  
  for(int x=0; x<16; x++){
    for(int y=0; y<4; y++){
      for(int z=0; z<4; z++){
        
        Mem_writeByte(room_timers_ADDR[x][y][z], room_timers[x][y][z]);
      }
    }
  }
  
  for(int x=0; x<4; x++){
    for(int y=0; y<2; y++){
      
      Mem_writeByte(ac_forced_on_ADDR[x][y], ac_forced_on[x][y]);
    }
  }
  
  for(int x=0; x<4; x++){
    for(int y=0; y<2; y++){
      
      Mem_writeByte(ac_periode_ADDR[x][y], ac_periode[x][y]);
    }
  }

  Mem_writeByte(startDelay_ADDR, 0);

  Mem_writeByte(acSwitchDelay_ADDR, 2);

  Mem_writeByte(ac_op_mode_ADDR, 3);
  
  Mem_writeByte(ac_set_temp_ADDR, 28);


  for(int i=0; i<4; i++){


    Mem_writeByte(ac_master_bypass_ADDR[i], ac_master_bypass[i]);
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}

void Mem_writeByte(int address, byte value){
  EEPROM.write(address, value);
  delay(5);
}

void Mem_writeInt(int address, int value)
{
  Mem_writeByte(address, highByte(value) );
  Mem_writeByte(address+1, lowByte(value) );
}



void Mem_updateByte(int address, byte value){
  byte reading = EEPROM.read(address);
  if(reading != value) Mem_writeByte(address, value);
}

void Mem_updateInt(int address, int value){
  Mem_updateByte(address, highByte(value));  //high byte
  Mem_updateByte(address+1, lowByte(value)); //low byte
}

byte Mem_readByte(int address){
  byte value = EEPROM.read(address);
  return value;
}

int Mem_readInt(int address){
  byte hiByte = EEPROM.read(address);
  byte loByte = EEPROM.read(address+1);
  int value = word(hiByte, loByte);
  return value;
}


No comments:

Post a Comment