Earthquake detection (EEPROM WriteAnything)

Hi,

I’m builting an EarthQuake detector. I found a nice code that use an accelerometer ADXL335, I built it
with an Arduino Nano and it’s working nice. I modified the code to fit my needs and I would like to add a feature.

EEPROM writeAnything

I’m not a pro with programming. If I understand the code, it look like the XYZ values are stored
in the EEPROM during initialisation and then the code is constantly comparing the actual XYZ values to the ones stored in the EEPROM and if the threshold go over the limit, it start the alarm.

There is two push buttons to adjust the threshold limit. At boot the threshold is set to 15 (thresholdSET)
and after that I can adjust it with the buttons.

Storing thresholdSET to the EEPROM

The propblem is that if I reboot the Arduino, I loose the threshold and I have to set it again with the buttons.

Questions

Is there and error in that code that prevent the settings to be written in the EEPROM
or is that feature is just not in the code ?

How can I correct it or what do I need to add to make the settings be written to the EEPROM.

/*
 * The code was found on electronicsforu.com
 * 
 * I modified the code a little to fit my needs
 * 
 * Version 1.4
 * 
 */


#include <EEPROM.h>
#include "EEPROMAnything.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3f,20,4);


//Pin 10 output for Relay and Red LED
const int relayPin = 10; 
int Xacc;
int Yacc; 
int Zacc; 
int threshold = 0;
int thresholdSET = 15;

//Pin 9 output for LCD Backlight (PWM)
int LcdBackLight = 9;  
int brightness = 0;

//Pin 6 output for Green Led (PWM)
int GreenLed = 6;
int brightnessLed = 0;

//LED-LCDBackLight Dimmer  High LCD-LED daytime and low LED-LCD during Night
int CDS = 3; //Photocell connected to Analog Pin 3
int lowDim = 200; //set this value according to the ambiant light  Min  ******Dimmer adjust
int highDim = 700;

long debouncing_time = 30; //Debouncing Time in Milliseconds
volatile unsigned long last_micros;

//For EEPROM WriteAnything
struct sensorValue
{
  int X;
  int Y;
  int Z;
};
sensorValue acceleration;


void setup() {
  
  lcd.init();
  lcd.begin(16,4);
  lcd.backlight();
  Serial.begin(9600);
  
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, HIGH);
  pinMode(LcdBackLight, OUTPUT);  
  pinMode(GreenLed, OUTPUT);

//For threshold adjust buttons on pins 2 and 3
  attachInterrupt(0, debounceInterrupt_Increment, RISING);
  attachInterrupt(1, debounceInterrupt_Decrement, RISING);
  
  EEPROM.write(500, thresholdSET);
  
  lcd.setCursor(0, 0);
  lcd.print("Initializing....");
  delay(5000);
  
  sensorValue acceleration = { analogRead(A0) , analogRead(A1) , analogRead(A2) };
  EEPROM_writeAnything(0, acceleration);
  EEPROM_readAnything(0, acceleration);
  
  lcd.clear();
}


void debounceInterrupt_Increment(){
  if ((long)(micros() - last_micros) >= debouncing_time * 1000) {
    IncrementThreshold();
    last_micros = micros();
  }
}


void debounceInterrupt_Decrement(){
  if ((long)(micros() - last_micros) >= debouncing_time * 1000) {
   DecrementThreshold();
   last_micros = micros();
  }
}


void IncrementThreshold(){
  thresholdSET = EEPROM.read(500);
  thresholdSET++;
  EEPROM.write(500, thresholdSET);
}


void DecrementThreshold() {
  thresholdSET = EEPROM.read(500);
  thresholdSET--;
  EEPROM.write(500, thresholdSET);
}


void Sismik(){

  EEPROM_readAnything(0, acceleration);
  threshold = EEPROM.read(500);
  lcd.setCursor(0,0);
  lcd.print("   Prototype V1.4");
  lcd.setCursor(0, 1);
  lcd.print(" Detection Sismique");
  lcd.setCursor(0, 2);
  lcd.print("   Sensibilite: ");
  lcd.print(threshold);
  Xacc = analogRead(A0);
  Yacc = analogRead(A1);
  Zacc = analogRead(A2);

  if ((Xacc >= (acceleration.X + threshold)) || (Xacc <= (acceleration.X - threshold))||(Yacc >= (acceleration.Y + threshold)) || (Yacc <= (acceleration.Y - threshold))||(Zacc >= (acceleration.Z + threshold)) || (Zacc <= (acceleration.Z - threshold))) {
    digitalWrite(relayPin, LOW);
    digitalWrite(LcdBackLight, HIGH);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print(" Alarme Tremblement");
    lcd.setCursor(0,1);
    lcd.print("  EARTHQUAKE ALARM");
    lcd.setCursor(0,3);
    lcd.print("     EVACUATION");
    delay(5000);
    digitalWrite(relayPin, HIGH);
    lcd.clear();
  }
}


void XYZLcdPrint(){

 lcd.setCursor(0,3);
 lcd.print("X= ");
 lcd.print(Xacc);
 lcd.setCursor(7,3);
 lcd.print("Y= ");
 lcd.print(Yacc);
 lcd.setCursor(14,3);
 lcd.print("Z= ");
 lcd.print(Zacc);
}


//For dimmming LCD Backlight and Green Led with PWM
void dimmer(){

 analogWrite(LcdBackLight, brightness);
 analogWrite(GreenLed, brightnessLed);
 
 int lightSensor = analogRead(CDS);

  if (lightSensor < lowDim) {
  brightness = 3;
  brightnessLed = 10;
  }
  if (lightSensor > highDim) {
  brightness = 100;
  brightnessLed = 80;
  }

  //This serial print is to see ambiant light and adjust the value
  Serial.println("*");
  Serial.print("LightSensor  ");
  Serial.println(lightSensor);
  Serial.println("*");
}

void loop() {

  Sismik();
  XYZLcdPrint();
  dimmer();

}
  sensorValue acceleration = { analogRead(A0) , analogRead(A1) , analogRead(A2) };
  EEPROM_writeAnything(0, acceleration);
  EEPROM_readAnything(0, acceleration);

You create a local variable with the same name as the global variable. Not a good idea. You populate the instance, write that to EEPROM, and then read the same stuff you just wrote, storing it in the variable that immediately goes out of scope.

In other words, the read was useless.

  if ((long)(micros() - last_micros) >= debouncing_time * 1000) {

Casting an unsigned long to a signed long is useless.

Problem Solved,

It took me a while but I finally found it.

I remove the following line in the setup:

EEPROM.write(500, thresholdSET);

It was overwriting my last settings with the default thresholdSET
at the beginning of the code.

int thresholdSET = 15;

I don't know why he did the code like that but now my settings stays
in the EEPROM until I change it.