Created
May 26, 2023 06:27
-
-
Save goran-mahovlic/87fd38cfb773c514d10a1b0e3f2ffe00 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#include <Adafruit_Sensor.h> | |
#include <Adafruit_ADXL345_U.h> | |
// Odredi pinove za LED diode | |
#define LED_ZELENA 4 | |
#define LED_ZUTA 3 | |
#define LED_CRVENA 2 | |
// Postavke jačine potresa | |
double nemaPotresa = 0.5; | |
double potresSlabi = 1.00; | |
double poresUmjereni = 2.00; | |
double potresJaki = 3.00; | |
// Privremene varijable za ubrzanje. | |
double AccX, AccY, AccZ; | |
/* Dodjeli jedinstveni broj */ | |
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345); | |
void setup(void) | |
{ | |
// Podesi brzinu serijske komunikacije na 9600 | |
Serial.begin(9600); | |
Serial.println("Detektor potresa"); Serial.println(""); | |
// Postavi LED diode kao izlaz | |
pinMode( LED_ZELENA, OUTPUT ); | |
pinMode( LED_ZUTA, OUTPUT ); | |
pinMode( LED_CRVENA, OUTPUT ); | |
// Postavi LED diode na Ugašeno | |
digitalWrite( LED_ZELENA, LOW ); | |
digitalWrite( LED_ZUTA, LOW ); | |
digitalWrite( LED_CRVENA, LOW ); | |
/* Inicijaliziraj senzor */ | |
if (!accel.begin()) | |
{ | |
/* Problem u inicijalizacij ADXL345 ... provjeri spojeve */ | |
Serial.println("ADXL345 nije pronađen ... Provjeri spojeve!"); | |
while (1); | |
} | |
/* Postavi na osjetljivost 2G */ | |
accel.setRange(ADXL345_RANGE_2_G); | |
Serial.println(""); | |
} | |
void loop(void) | |
{ | |
/* Dohvati događaj sa senzora */ | |
sensors_event_t event; | |
accel.getEvent(&event); | |
// Preuzmi podatke sa senzora | |
AccX = event.acceleration.x; | |
AccY = event.acceleration.y; | |
AccZ = event.acceleration.z; | |
/* Prikaži rezultate (ubrzanje m/s^2) */ | |
Serial.print("X: "); Serial.print(AccX); Serial.print(" "); | |
Serial.print("Y: "); Serial.print(AccY); Serial.print(" "); | |
Serial.print("Z: "); Serial.print(AccZ); Serial.print(" "); Serial.println("m/s^2 "); | |
// Ako je akceleracija po X Y ili Z veća od potresSlabi | |
if (AccX > potresSlabi || AccY > potresSlabi || AccZ > potresSlabi) { | |
// Slabo ubrzanje detektirano, upali zelenu LED diodu | |
digitalWrite( LED_ZELENA, HIGH ); | |
// Pričekaj jednu sekundu | |
delay(1000); | |
} | |
// Ako je akceleracija po X Y ili Z veća od potresUmjereni | |
else if (AccX > poresUmjereni || AccY > poresUmjereni || AccZ > poresUmjereni) { | |
// Umjereno ubrzanje detektirano, upali zutu LED diodu | |
digitalWrite( LED_ZUTA, HIGH ); | |
// Pričekaj jednu sekundu | |
delay(1000); | |
} | |
// Ako je akceleracija po X Y ili Z veća od potresJaki | |
else if (AccX > potresJaki || AccY > potresJaki || AccZ > potresJaki) { | |
// Jako ubrzanje detektirano, upali crvenu LED diodu | |
digitalWrite( LED_CRVENA, HIGH ); | |
// Pričekaj jednu sekundu | |
delay(1000); | |
} | |
// Ako nema akceleracije | |
else if (AccX < nemaPotresa && AccY < nemaPotresa && AccZ < nemaPotresa ){ | |
// Pričekaj 2 sekunde | |
delay(2000); | |
// Postavi LED diode na ugašeno | |
digitalWrite( LED_ZELENA, LOW ); | |
digitalWrite( LED_ZUTA, LOW ); | |
digitalWrite( LED_CRVENA, LOW ); | |
// nema potresa... | |
} | |
delay(50); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment