Components and supplies
D7S sensor
LED (generic)
1 relay module 5 Vdc 10A (assembled)
Arduino Nano
Piezo Buzzer
Resistor 470 ohm
Tools and machines
Soldering kit
Apps and platforms
Arduino IDE
Project description
Code
code
cpp
...
1/* 2 Copyright 2017 Alessandro Pasqualini 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 http://www.apache.org/licenses/LICENSE-2.0 7 Unless required by applicable law or agreed to in writing, software 8 distributed under the License is distributed on an "AS IS" BASIS, 9 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 See the License for the specific language governing permissions and 11 limitations under the License. 12 13 @author Alessandro Pasqualini <alessandro.pasqualini.1105@gmail.com> 14 @url https://github.com/alessandro1105 15 16 This project has been developed with the contribution of Futura Elettronica. 17 - http://www.futurashop.it 18 - http://www.elettronicain.it 19 - https://www.open-electronics.org 20*/ 21 22#include <D7S.h> 23 24int LED1 = 2; 25int LED2 = 3; 26int LED3 = 4; 27int LED4 = 5; 28int LEDSH = 6; 29int LEDRE = 8; 30int BUZZ = 7; 31int REL = 9; 32 33 34//old earthquake data 35float oldSI = 0; 36float oldPGA = 0; 37float currentPGA = 0; 38//flag variables to handle collapse/shutoff only one time during an earthquake 39bool shutoffHandled = false; 40bool collapseHandled = false; 41 42//function to handle shutoff event 43void handleShutoff() { 44 //put here the code to handle the shutoff event 45 Serial.println("-------------------- SHUTOFF! --------------------"); 46 Serial.println("Shutting down all device!"); 47 //stop all device 48 while (1) 49 ; 50} 51 52//function to handle collapse event 53void handleCollapse() { 54 //put here the code to handle the collapse event 55 Serial.println("-------------------- COLLAPSE! --------------------"); 56} 57 58 59void setup() { 60 // Open serial communications and wait for port to open: 61 Serial.begin(9600); 62 63pinMode(2, OUTPUT); 64pinMode(3, OUTPUT); 65pinMode(4, OUTPUT); 66pinMode(5, OUTPUT); 67pinMode(6, OUTPUT); 68pinMode(7, OUTPUT); 69pinMode(8, OUTPUT); 70pinMode(9, OUTPUT); 71 72 73 while (!Serial) { 74 ; // wait for serial port to connect. Needed for native USB port only 75 } 76 77 //--- STARTING --- 78 Serial.print("Starting D7S communications (it may take some time)..."); 79 //start D7S connection 80 D7S.begin(); 81 //wait until the D7S is ready 82 while (!D7S.isReady()) { 83 Serial.print("."); 84 delay(500); 85 } 86 Serial.println("STARTED"); 87 88 //--- SETTINGS --- 89 //setting the D7S to switch the axis at inizialization time 90 Serial.println("Setting D7S sensor to switch axis at inizialization time."); 91 D7S.setAxis(SWITCH_AT_INSTALLATION); 92 93 //--- INITIALIZZAZION --- 94 Serial.println("Initializing the D7S sensor in 2 seconds. Please keep it steady during the initializing process."); 95 delay(2000); 96 Serial.print("Initializing..."); 97 //start the initial installation procedure 98 D7S.initialize(); 99 //wait until the D7S is ready (the initializing process is ended) 100 while (!D7S.isReady()) { 101 Serial.print("."); 102 delay(500); 103 } 104 Serial.println("INITIALIZED!"); 105 106 //--- CHECKING FOR PREVIUS COLLAPSE --- 107 //check if there there was a collapse (if this is the first time the D7S is put in place the installation data may be wrong) 108 if (D7S.isInCollapse()) { 109 handleCollapse(); 110 } 111 112 //--- RESETTING EVENTS --- 113 //reset the events shutoff/collapse memorized into the D7S 114 D7S.resetEvents(); 115 116 delay(3000); 117 118 //--- READY TO GO --- 119 Serial.println("\nListening for earthquakes!"); 120 digitalWrite(LEDRE, HIGH); 121 122} 123 124void loop() { 125 126 //checking if there is an earthquake occuring right now 127 if (D7S.isEarthquakeOccuring()) { 128 129 //check if the shutoff event has been handled and if the shutoff condition is met 130 //the call of D7S.isInShutoff() is executed after to prevent useless I2C call 131 if (!shutoffHandled && D7S.isInShutoff()) { 132 133 digitalWrite(LED1, LOW); 134 digitalWrite(LED2, LOW); 135 digitalWrite(LED3, LOW); 136 digitalWrite(LED4, LOW); 137 digitalWrite(LEDRE, LOW); 138 digitalWrite(LEDSH, HIGH); 139 digitalWrite(BUZZ, HIGH); 140 digitalWrite(REL, HIGH); 141 handleShutoff(); 142 shutoffHandled = true; 143 144 } 145 146 //check if the shutoff event has been handled and if the shutoff condition is met 147 //the call of D7S.isInShutoff() is executed after to prevent useless I2C call 148 if (!collapseHandled && D7S.isInCollapse()) { 149 150 digitalWrite(LED1, HIGH); 151 digitalWrite(LED2, HIGH); 152 digitalWrite(LED3, HIGH); 153 digitalWrite(LED4, HIGH); 154 digitalWrite(LEDRE, LOW); 155 digitalWrite(LEDSH, HIGH); 156 digitalWrite(BUZZ, HIGH); 157 digitalWrite(REL, HIGH); 158 159 handleCollapse(); 160 collapseHandled = true; 161 } 162 163 //print information about the current earthquake 164 float currentSI = D7S.getInstantaneusSI(); 165 float currentPGA = D7S.getInstantaneusPGA(); 166 167 if (currentSI > oldSI || currentPGA > oldPGA) { 168 //getting instantaneus SI 169 Serial.print("\tInstantaneus SI: "); 170 Serial.print(currentSI); 171 Serial.println(" [m/s]"); 172 173 //getting instantaneus PGA 174 Serial.print("\tInstantaneus PGA (Peak Ground Acceleration): "); 175 Serial.print(currentPGA); 176 Serial.println(" [m/s^2]\n"); 177 178 //save the current data 179 oldSI = currentSI; 180 oldPGA = currentPGA; 181 182 if (currentPGA > 0 && currentPGA <= 0.1) { 183 digitalWrite(LED1, HIGH); 184 digitalWrite(LED2,LOW); 185 digitalWrite(LED3, LOW); 186digitalWrite(LED4, LOW); 187 } 188 if (currentPGA > 0.1 && currentPGA <= 0.2) { 189 190 digitalWrite(LED1, LOW); 191 digitalWrite(LED2, HIGH); 192 digitalWrite(LED3,LOW); 193 digitalWrite(LED4,LOW); 194 } 195if (currentPGA > 0.2 && currentPGA <= 0.3) { 196 digitalWrite(LED3, LOW); 197 digitalWrite(LED2, LOW); 198 digitalWrite(LED3, HIGH); 199 digitalWrite(LED4, LOW); 200} 201 202if (currentPGA > 0.3 ) { 203 digitalWrite(LED1, LOW); 204 digitalWrite(LED2, LOW); 205 digitalWrite(LED3, LOW); 206 digitalWrite(LED4, HIGH); 207 208 } 209 210 } else { 211 //reset the old earthquake data 212 oldPGA = 0; 213 oldSI = 0; 214 //reset the flag of the handled events 215 shutoffHandled = false; 216 collapseHandled = false; 217 //reset D7S events 218 D7S.resetEvents(); 219 220 } 221 } 222}
Downloadable files
Schematic
...
Schematic JPG.jpg
Comments
Only logged in users can leave comments