Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Need help arduino e...
 
Share:
Notifications
Clear all

[Solved] Need help arduino earthquake detector

3 Posts
2 Users
0 Reactions
1,392 Views
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter  

hello guys, i am doing this project to detect earthquake, when there is still movement after 5 seconds the arduino will activate the relay. My problem is the timer resets immediately when the sensor goes back to its original position so the 5 second timer does not end and it keeps resetting.

i used adxl 335 accelerometer sensor

here is my code:

#define buzzer 12 // buzzer pin
#define led 13  //led pin
#define relay 11 // relay pin

#define x A0  // x_out pin of Accelerometer
#define y A1  // y_out pin of Accelerometer
#define z A2  // z_out pin of Accelerometer

/*variables*/

int xsample = 0;
int ysample = 0;
int zsample = 0;
long start;


unsigned long movementStarted;
unsigned long currentMillis;
unsigned long period = 5000;
/*Macros*/

#define samples 50
#define maxVal 4   // max change limit
#define minVal -4    // min change limit
bool moving = false;

void setup()
{
  pinMode(buzzer, OUTPUT);
  pinMode(led,    OUTPUT);
  pinMode(relay,  OUTPUT);

  for (int i = 0; i < samples; i++) // taking samples for calibration
  {
    xsample += analogRead(x);
    ysample += analogRead(y);
    zsample += analogRead(z);
  }

  xsample /= samples; // taking avg for x
  ysample /= samples; // taking avg for y
  zsample /= samples; // taking avg for z
  delay (3000);
}
void loop()
{
  currentMillis = millis();

  int value1 = analogRead(x); // reading x out
  int value2 = analogRead(y); // reading y out
  int value3 = analogRead(z); // reading z out

  int xValue = xsample - value1;  // finding change in x
  int yValue = ysample - value2;  // finding change in y
  int zValue = zsample - value3;  // finding change in z


  // Check if there is currently movement.
  if (xValue < minVal || xValue > maxVal  || yValue < minVal || yValue > maxVal  || zValue < minVal || zValue > maxVal)
  {
    if (moving == false)                          // Did we just started moving?
    {
      movementStarted = currentMillis;            // Capture the time we started moving
      tone(buzzer, 1000);
    }
    moving = true;                                // Flag that we are moving 
    if (currentMillis - movementStarted > period) // Check how long we have been moving for 
    {
      digitalWrite(relay, HIGH);                   
    }
  }
  else                                          // Not moving.
  {
    moving = false;                               // Flag that we are not moving
    noTone(buzzer); 
                                    // Turn relay OFF (active LOW)
  }
}

   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2791
 

Hi vjcp,

I do not have an accelerometer available (or earthquakes for that matter 😋  ).

This is an interesting challenge for sure though.
One key problem would be this:

Say we detection motion at 0 seconds. And again at 1 second, 2 seconds, 4 seconds, and ... 6 seconds.
This could be seen as an earthquake detection, right? But when we strictly stick to 5 seconds it would not be seen as such.

Maybe we'd need to define clearer what is considered motion that indicates an earth quake.
For example:

X number of motion detection within 5 seconds?
Or at the most X seconds between two motion detections?

 

Note: I'd make a separate function for motion detection to make code more readable. Something like this (returns true or false):

bool motionDetected()
{
  int value1 = analogRead(x); // reading x out
  int value2 = analogRead(y); // reading y out
  int value3 = analogRead(z); // reading z out

  int xValue = xsample - value1;  // finding change in x
  int yValue = ysample - value2;  // finding change in y
  int zValue = zsample - value3;  // finding change in z

  return (xValue < minVal || xValue > maxVal  || yValue < minVal || yValue > maxVal  || zValue < minVal || zValue > maxVal);
}

 


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2791
 

p.s. maybe this article is helpful for your project?

Is this the project you got your code? 


   
ReplyQuote
Share: