#include <EEPROM.h>
// Define EEPROM addresses
const int value1Address = 0; // Address for the first value
const int value2Address = sizeof(int); // Address for the second value
const int value3Address = 2 * sizeof(int); // Address for the third value
const int value4Address = 3 * sizeof(int); // Address for the fourth value
// Button pins
const int button1Pin = 2; // Pin for the first button
const int button2Pin = 3; // Pin for the second button
const int button3Pin = 4; // Pin for the third button
const int button4Pin = 5; // Pin for the fourth button
// Variables to hold the values
int value1;
int value2;
int value3;
int value4;
// Function to read values from EEPROM
void readValuesFromEEPROM() {
EEPROM.get(value1Address, value1);
EEPROM.get(value2Address, value2);
EEPROM.get(value3Address, value3);
EEPROM.get(value4Address, value4);
}
// Function to write values to EEPROM
void writeValuesToEEPROM() {
EEPROM.put(value1Address, value1);
EEPROM.put(value2Address, value2);
EEPROM.put(value3Address, value3);
EEPROM.put(value4Address, value4);
}
void setup() {
Serial.begin(9600); // Start serial communication
// Initialize button pins
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
pinMode(button3Pin, INPUT_PULLUP);
pinMode(button4Pin, INPUT_PULLUP);
// Read values from EEPROM
readValuesFromEEPROM();
// Check if the values are uninitialized (assuming default values are not negative)
if (value1 == 0 && value2 == 0 && value3 == 0 && value4 == 0) {
// If all values are zero, initialize them
value1 = 0;
value2 = 0;
value3 = 0;
value4 = 0;
writeValuesToEEPROM(); // Write initialized values to EEPROM
Serial.println("Default values written to EEPROM.");
} else {
Serial.println("Values read from EEPROM:");
}
// Print the values
Serial.print("Value 1: ");
Serial.println(value1);
Serial.print("Value 2: ");
Serial.println(value2);
Serial.print("Value 3: ");
Serial.println(value3);
Serial.print("Value 4: ");
Serial.println(value4);
}
void loop() {
// Check button 1
if (digitalRead(button1Pin) == LOW) { // Button 1 pressed
value1++;
writeValuesToEEPROM(); // Save new value to EEPROM
Serial.print("Button 1 pressed. New Value 1: ");
Serial.println(value1);
delay(300); // Simple debounce
}
// Check button 2
if (digitalRead(button2Pin) == LOW) { // Button 2 pressed
value2++;
writeValuesToEEPROM(); // Save new value to EEPROM
Serial.print("Button 2 pressed. New Value 2: ");
Serial.println(value2);
delay(300); // Simple debounce
}
// Check button 3
if (digitalRead(button3Pin) == LOW) { // Button 3 pressed
value3++;
writeValuesToEEPROM(); // Save new value to EEPROM
Serial.print("Button 3 pressed. New Value 3: ");
Serial.println(value3);
delay(300); // Simple debounce
}
// Check button 4
if (digitalRead(button4Pin) == LOW) { // Button 4 pressed
value4++;
writeValuesToEEPROM(); // Save new value to EEPROM
Serial.print("Button 4 pressed. New Value 4: ");
Serial.println(value4);
delay(300); // Simple debounce
}
// Add a small delay to avoid rapid toggling
delay(10);
}