I'm working on a project which is likely an earthquake alarm. The sketch that I got from the internet does not retain the "on" state of the indicators(LED and buzzer)when it is triggered. I want to modify the sketch that it makes the LED and buzzer retains their "on" state when the sensor(accelerometer) is triggered. Could someone help me ?

Here is the code:

#include <Wire.h>
#include <MPU6050.h>

#define minval -5
#define maxval 3

MPU6050 mpu;


void setup()
{       
  Serial.begin(115200);
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);

  delay (2000);

  // Initialize MPU6050
  Serial.println("Initialize MPU6050");

  while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
  { 
    Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
    delay(500);
  }

  mpu.setThreshold(3); 
  // Check settings
  checkSettings();
}



void checkSettings()
{
  Serial.println();
  Serial.print(" * Sleep Mode:        ");
  Serial.println(mpu.getSleepEnabled() ? "Enabled" : "Disabled");
  Serial.print(" * Clock Source:      ");
  switch(mpu.getClockSource())
  {
    case MPU6050_CLOCK_KEEP_RESET:
      Serial.println("Stops the clock and keeps the timing generator in reset"); 
      break;
    case MPU6050_CLOCK_EXTERNAL_19MHZ:
      Serial.println("PLL with external 19.2MHz reference"); 
      break;
    case MPU6050_CLOCK_EXTERNAL_32KHZ:
      Serial.println("PLL with external 32.768kHz reference");
      break;
    case MPU6050_CLOCK_PLL_ZGYRO:
      Serial.println("PLL with Z axis gyroscope reference");
      break;
    case MPU6050_CLOCK_PLL_YGYRO:
      Serial.println("PLL with Y axis gyroscope reference");
      break;
    case MPU6050_CLOCK_PLL_XGYRO:
      Serial.println("PLL with X axis gyroscope reference");
      break;
    case MPU6050_CLOCK_INTERNAL_8MHZ:
      Serial.println("Internal 8MHz oscillator"); 
      break;
  }

  Serial.print(" * Gyroscope:         ");
  switch(mpu.getScale())
  {
    case MPU6050_SCALE_2000DPS:
      Serial.println("2000 dps");
      break;
    case MPU6050_SCALE_1000DPS:
      Serial.println("1000 dps");
      break;
    case MPU6050_SCALE_500DPS:
       Serial.println("500 dps");
       break;
    case MPU6050_SCALE_250DPS:
       Serial.println("250 dps");
       break;
  }

  Serial.print(" * Gyroscope offsets: ");
  Serial.print(mpu.getGyroOffsetX());
  Serial.print(" / ");
  Serial.print(mpu.getGyroOffsetY());
  Serial.print(" / ");
  Serial.println(mpu.getGyroOffsetZ());
  Serial.println();
}

void loop()
{
  Vector rawGyro = mpu.readRawGyro();
  Vector normGyro = mpu.readNormalizeGyro();
  Serial.print(" Xraw = ");
  Serial.print(rawGyro.XAxis);
  Serial.print(" Yraw = ");
  Serial.print(rawGyro.YAxis);
  Serial.print(" Zraw = ");
  Serial.println(rawGyro.ZAxis); 

  if (normGyro.XAxis > maxval ||
      normGyro.XAxis < minval &&
      normGyro.YAxis > maxval ||
      normGyro.YAxis  < minval &&
      normGyro.ZAxis > maxval || 
      normGyro.ZAxis  < minval)
  {
    digitalWrite(7,HIGH);
    digitalWrite(8,HIGH);
    delay(300);
    digitalWrite(7,HIGH);
    digitalWrite(8,HIGH);
    delay(300);
    delay (1000);
  }
  else
  {
    digitalWrite(7,LOW);
    digitalWrite(8,LOW);
  }
  Serial.print(" Xnorm = ");
  Serial.print(normGyro.XAxis);
  Serial.print(" Ynorm = ");
  Serial.print(normGyro.YAxis);
  Serial.print(" Znorm = ");
  Serial.println(normGyro.ZAxis);
  delay(10);
}
  • 1
    i've seen your code before ..... you had someone delete all the LCD code from a sketch that you copied from the web ..... that resulted in a bunch of useless and redundant code being left behind .... one of the useless commands is obvious even for a beginner, i think.... it appears that you have not even bothered to read through the code because you have not deleted the redundant code or at least questioned why it is there ..... how do you expect to learn any programming if you always ask someone else to do it for you – jsotola Nov 30 at 22:32
  • Sorry dude, but I'm just afraid that I will mess up the code. I've been thinking that if i could remove the local variables inside the " void checkseetings; ", coz i think they are related to the LCD code. Could you help me ? – Hamdanie Impa Dec 1 at 4:16
  • Define a (boolean: bool) variable, set it to false initially.

  • When the sensor is activated, set the variable to true.

  • Define something that should be able to disactivate the buzzer/LEDs. And if that happens, set the variable to FALSE. Maybe this is a reset button ?

  • Than based on this variable set the buzzer and LEDs on or off.

So you get something like a global variable

bool alarmTriggered;

In setup:

alarmTriggered = false;

In loop:

if (AreSensorsTriggered(...)) // Create your own function
{
   alarmTriggered = true;
}
else if (IsResetButtonPressed(...)) // Create your own function
{
   alarmTriggered = false;
}


SetBuzzer(alarmTriggered); // Create a function that enables/disables buzzer
SetLEDs(alarmTriggered); // Create a function that enabled/disables LEDs
  • Sorry dude, I'm newbie when it comes to programming, could you modify it for me? – Hamdanie Impa Nov 30 at 13:10
  • How can I say this, but I can't apply it in the sketch, do i just copy/paste in the sketch ? sorry for the trouble dude . – Hamdanie Impa Nov 30 at 13:20
  • First you need to think how to switch off the LEDs/buzzer. – Michel Keijzers Nov 30 at 13:21
  • The leds/buzzer will only turn off when i reset the arduino – Hamdanie Impa Nov 30 at 13:23
  • That's why you need to think of a trigger also to switch it off (to set the boolean back to false). I cannot invent it, maybe after some time, or a 'reset alarm' button? – Michel Keijzers Nov 30 at 15:47

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.