Having had a look at your program codes, I have understood that I need to offer you an overview of the structure of the internal EEPROM of ATmega328P MCU of the UNO before going to guide you to debug your program yourself.
1. This is the conceptual view of the internal EEPROM Memory of the ATmega328P MCU.
Figure-1: Conceptual view of internal EEPROM of ATmega328P MCU
(a) There are 1024 locations inside the EEPROM. Every location has an identification code called Address. The address of the very first location is 0 (0x0000), the address of the next location is 1 (0x0001), ..., and the address of the last location is 1023 (0x03FF).
(b) Each location can hold only 8-bit data called byte.
(c) This memory block is called EEPROM memory. EEPROM stands for Electrical Erasable Electrically Programmable Random Access Read Only Memory. This a non-volatile memory; it means that the memory location can retain data even at the absence of power.
(d) The memory block is associated with lock bits to prevent others from copying the contents of the memory. It is also associated with Fuse Bits which is not being discussed here.
(e) Data read/operation with the EEPROM can be done in the following ways:
(i) Using commercial expensive Parallel ROM Programmer when the memory chip is not in the holding instrument.
(ii) Using In-system Programming Interface which is a built-in circuit of the MCU. It works in assistance with some kind of user written software.
(iii) Using program instructions when the memory chip is in the holding instrument. These instructions have been supplied at higher level fashion by the EEPROM.h Library.
(f) The Low Level (Register Level) algorithm to write a data byte (say 0x23) into a EEPROM location (say, 0x0234) is:
(i) Enable Master programming Enable Bit (EEMPE) by putting LH into this bit of EECR Register. At this time, the EEPE bit must remain at LL-state.
(ii) Set address (0x0234) via EEPROM Address Registers : <EEARH, EEARL>.
(iii) Put data (0x23) into EEDR Register.
(iv) Enable Programming Enable Bit (EEPE) by putting LH into this bit. This bit remains at LH as long as writing process is going ON; at the end of writing process, the bit assumes LL-state. The EEPROM takes about 5 ms time period to get the data byte written into it.
Arduino Coding for the above algorithm:
void setup()
{
Serial.begin(9600);
EECR = 0x00; //Mode set: erase and write in one operation
EECR = EECR | 0b00000100; //EEMPE-bit = LH
EEARH = 0x02; //high byte address of EEPROM location
EEARL = 0x34; //low byte address of EEPROM location
EEDR = 0x23; //keep 1-byte data into EEDR Register
EECR = EECR | 0b00000010; //EEPE-bit enable; | (pipe sign) indicate bit-wise OR operation
while (bitRead(EECR,1) != LOW)
; //wait until writing process is done
EECR = 0x00; //data write done; EECR is reset
//----read data back into variable x------------
EEARH = 0x02;
EEARL = 0x34;
EECR = 0b00000001; //EERE -bit is enabled
byte x = EEDR; //data 23 is in variable
EECR = 0x00; //EECR is reset
//------show data on Serial Monitor--------------
Serial.println(x); //SM shows 23
}
void loop()
{
}
Equivalent Arduino High Level codes for the codes of Step-1f.
EEPROM.write (0x0234, 0x23); //Write operation 0x23 ----> 0x0234
byte x = EEPROM.read(0x0234); //read operation x <-----(0x0234)
(g) Registers involved with EEPROM Programming
Figure-2: Registers related with EEPROM Programming
2. These are the defects in your program which you have posted.
(a) You have acquired signal from analog channel - 0 (A0), and you have saved it in an EEPROM location whose symbolic name is addressHP12 and the numerical value of the address is 0 (0x000).
(b) You have acquired signal from analog channel - 1 (A1), and you have saved it in an EEPROM location whose symbolic name is addressLP13 and the numerical value of the address is also 0 (0x000).
(c) There are two values from two two different sources; they should be saved into two different EEPROM locations. So, bring the following changes in your program:
int addressHP12 = 0x0000;
int addressLP13 = 0x0001;
(d) When buttons are not closed, you are reading the contents of the two EEPROM locations of Step-2c; but, you have not initialized them with any value at all. It is better to initialize them in the setup with some known values say , 0x00.
EEPROM.write(address12, 0x00);
EEPROM.write(addressLP13, 0x00);
Now, you will see 0 and 0 on the Serial Monitor when you have not closed the buttons.
(e) The buttons are at DPin-2 and DPin-4; you must have external pull-down resistors (2.2K/4.7k) connected with these pins. Now, push switches connected with these DPins will provide AH signals when pressed down.
3. The following is your program after correction. I have tested it with 3.3V at A0 and 5V at A1.
Figure-3: Serial Monitor showing result of program execution
// These constants won't change. They're used to give names to the pins used:
const int analogInPin = A0; // Analog input pin that the sensor is attached to
const int analogInPin1 = A1; // Analog input pin that the sensor is attached to
int sensorValue1HP = 0; // value read from the pot
int sensorValue2LP = 0; // value read from the pot
String stringOne, stringTwo, stringThree;
const int numReadings = 20;
int outputValue1 = 0; // value output to the PWM (analog out)
int outputValue2 = 0; // value output to the PWM (analog out)
int inPin = 2; // choose the input pin (for a pushbutton)
int inPin2 = 4; // choose the input pin (for a pushbutton)
int val_pb = 0; // variable for reading the pin status
int val_pb2 = 0; // variable for reading the pin status
int zerovalueHP = 0; // variable for reading the pin status HP
int zerovalueLP = 0; // variable for reading the pin status LP
int addressHP = 0;
int addressLP = 0;
int addressHP12 = 0;
int addressLP13 = 1;
int valueHPZERO = 0;
int valueLPZERO = 0;
byte valueHP;
byte valueLP;
int scaledpressure;
#include <EEPROM.h> //include libary for storage
void setup()
{
pinMode(inPin, INPUT); // declare pushbutton as input#
pinMode(inPin2, INPUT); // declare pushbutton as input#
Serial.begin(9600); // initialize serial communications at 9600 bps:
EEPROM.write(addressLP13, 0x00);
EEPROM.write(addressHP12, 0x00);
}
void loop()
{
val_pb = digitalRead(inPin); // read input value
val_pb2 = digitalRead(inPin2); // read input value
valueLP = EEPROM.read(addressLP13);
valueHP = EEPROM.read(addressHP12);
if (val_pb == HIGH)
{ //check switch if it is on , if it is run code
int zerovalueHP = analogRead(analogInPin) / 4; // A0 //read analog pin divide by 4 as storage can only store 255 and equal zerovalueHP to that value
EEPROM.write(addressHP12, zerovalueHP); //address = 0x0000 //write to storage the zero value to location
valueHP = EEPROM.read(addressHP12); //read the address and equal int value to it
}
if (val_pb2 == HIGH)
{
int zerovalueLP = analogRead(analogInPin1) / 4; //A1
EEPROM.write(addressLP13, zerovalueLP); //address 0x0000
valueLP = EEPROM.read(addressLP13);
}
Serial.print("Printing value of Ch-1 (A1) : ");
Serial.print(valueLP);
Serial.print(" Printing value of Ch-0 (A0) : ");
Serial.print(valueHP);
Serial.println();
Serial.println();
delay (3000);
}