Devices & Components
Arduino Nano
Buzzer, Piezo
9V battery (generic)
SparkFun Triple Axis Accelerometer Breakout - ADXL335
Slide Switch
Jumper wires (generic)
5 mm LED: Red
Hardware & Tools
3D Printer (generic)
Project description
Code
SOS CODE
arduino
CODE
1#include <Wire.h> 2#include <Adafruit_Sensor.h> 3#include <Adafruit_ADXL345_U.h> 4 5int ledr = 13; 6int b1 = 2; 7double fXg = 0; 8double fYg = 0; 9double fZg = 0; 10double roll; 11double pitch; 12const float alpha = 0.5; 13 const int sens = 7; 14 15Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(); 16 17void setup(void) 18{ 19 pinMode(ledr, OUTPUT); 20 pinMode (b1, OUTPUT); 21 22 Serial.begin(9600); 23 if(!accel.begin()) 24 { 25 Serial.println("No ADXL345 sensor detected."); 26 while(1); 27 } 28 29 30 digitalWrite(ledr, HIGH); 31 digitalWrite(b1,HIGH); 32 delay(500); 33 digitalWrite(ledr, LOW); 34 digitalWrite(b1, LOW); 35 36 37 38} 39 40 41int get_roll(void){ 42 // Get a new sensor event 43 44 sensors_event_t event; 45 accel.getEvent(&event); 46 47 48 fXg = event.acceleration.x * alpha + (fXg * (1.0 - alpha)); 49 fYg = event.acceleration.y * alpha + (fYg * (1.0 - alpha)); 50 fZg = event.acceleration.z * alpha + (fZg * (1.0 - alpha)); 51 52 //Roll & Pitch Equations 53 roll = (atan2(-fYg, fZg)*180.0)/M_PI; 54 55 return roll; 56 57} 58 59int get_pitch(void){ 60 // Get a new sensor event 61 62 sensors_event_t event; 63 accel.getEvent(&event); 64 65 66 fXg = event.acceleration.x * alpha + (fXg * (1.0 - alpha)); 67 fYg = event.acceleration.y * alpha + (fYg * (1.0 - alpha)); 68 fZg = event.acceleration.z * alpha + (fZg * (1.0 - alpha)); 69 70 //Roll & Pitch Equations 71 72 pitch = (atan2(fXg, sqrt(fYg*fYg + fZg*fZg))*180.0)/M_PI; 73 74 return pitch; 75 76} 77 78 79void loop(void) 80{ 81 82 int oldRoll = get_roll(); 83 int oldPitch =get_pitch(); 84 Serial.print("roll:");Serial.print(oldRoll); 85 Serial.print("pitch:");Serial.println(oldPitch); 86 sensors_event_t event; 87 accel.getEvent(&event); 88 89 Serial.print("X: "); Serial.print(event.acceleration.x); Serial.print(" "); 90 Serial.print("Y: "); Serial.print(event.acceleration.y); Serial.print(" "); 91 Serial.print("Z: "); Serial.print(event.acceleration.z); Serial.print(" "); 92 delay(500); 93 94 int NewRoll = get_roll(); 95 int NewPitch =get_pitch(); 96 97if (NewRoll < (oldRoll-sens) && oldRoll != 0 && NewRoll != 0) { 98 // sound the alarm 99 sirena_policia(); 100 101 }else if (NewRoll > (oldRoll+sens) && oldRoll!= 0 && NewRoll != 0) { 102 // sound the alarm 103 sirena_policia(); 104 105 } 106 107 108if (NewPitch < (oldPitch-sens) && oldPitch != 0 && NewPitch != 0) { 109 // sound the alarm 110 sirena_policia(); 111 112 113 114 }else if (NewPitch > (oldPitch+sens) && oldPitch!= 0 && NewPitch != 0) { 115 // sound the alarm 116 117 118 sirena_policia(); 119 120 } 121 122 123 124} 125 126 127void sirena_policia() { //This function produces the 4th siren(POLICE) sound with led transition. 128for(int i=2;i<=10;i+=2) 129digitalWrite(i,HIGH); 130 for(int hz = 440; hz < 1000; hz++){ 131 tone(b1, hz, 50); 132 delay(5); 133 } 134for(int i=2;i<=10;i+=2) 135digitalWrite(i,LOW); 136 for(int i=4;i<=12;i+=2) 137digitalWrite(i,HIGH); 138 for(int hz = 1000; hz > 440; hz--){ 139 tone(b1, hz, 50); 140 delay(5); 141 } 142 }
Downloadable files
Diagram circuit SOS Alarm
the basic circuit
Diagram circuit SOS Alarm
Diagram circuit SOS Alarm
the basic circuit
Diagram circuit SOS Alarm
Documentation
SOS Enclosures
download and print
SOS Enclosures
SOS Enclosures
download and print
SOS Enclosures
Comments
Only logged in users can leave comments