sinester09
Published © LGPL

bike alarm

Protect your bike from thefts with an alarm. you can do it.

IntermediateShowcase (no instructions)1,744
bike alarm

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Slide Switch
Slide Switch
×1
Buzzer, Piezo
Buzzer, Piezo
×1
SparkFun Triple Axis Accelerometer Breakout - ADXL335
SparkFun Triple Axis Accelerometer Breakout - ADXL335
×1
9V battery (generic)
9V battery (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
5 mm LED: Red
5 mm LED: Red
×1

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)

Story

Read more

Custom parts and enclosures

SOS Enclosures

download and print

Schematics

Diagram circuit SOS Alarm

the basic circuit

Code

SOS CODE

Arduino
CODE
#include <Wire.h>
#include <Adafruit_Sensor.h> 
#include <Adafruit_ADXL345_U.h>

int ledr = 13;
int b1 = 2;
double fXg = 0;
double fYg = 0;
double fZg = 0;
double roll;
double pitch;
const float alpha = 0.5;
 const int sens = 7;

Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified();

void setup(void) 
{
  pinMode(ledr, OUTPUT);
  pinMode (b1, OUTPUT);  
  
   Serial.begin(9600);  
   if(!accel.begin())
   {
      Serial.println("No ADXL345 sensor detected.");
      while(1);
   }


  digitalWrite(ledr, HIGH);
  digitalWrite(b1,HIGH);
  delay(500);
  digitalWrite(ledr, LOW);
  digitalWrite(b1, LOW);


   
}


int get_roll(void){
 // Get a new sensor event 

 sensors_event_t event; 
 accel.getEvent(&event);
   

    fXg = event.acceleration.x * alpha + (fXg * (1.0 - alpha));
    fYg = event.acceleration.y * alpha + (fYg * (1.0 - alpha));
    fZg = event.acceleration.z * alpha + (fZg * (1.0 - alpha));
 
    //Roll & Pitch Equations
    roll  = (atan2(-fYg, fZg)*180.0)/M_PI;

 return roll;

}

int get_pitch(void){
 // Get a new sensor event 

 sensors_event_t event; 
 accel.getEvent(&event);
   

    fXg = event.acceleration.x * alpha + (fXg * (1.0 - alpha));
    fYg = event.acceleration.y * alpha + (fYg * (1.0 - alpha));
    fZg = event.acceleration.z * alpha + (fZg * (1.0 - alpha));
 
    //Roll & Pitch Equations

    pitch = (atan2(fXg, sqrt(fYg*fYg + fZg*fZg))*180.0)/M_PI;

 return pitch;

}


void loop(void) 
{
   
    int oldRoll = get_roll();
    int oldPitch =get_pitch();
    Serial.print("roll:");Serial.print(oldRoll);
    Serial.print("pitch:");Serial.println(oldPitch);
   sensors_event_t event; 
   accel.getEvent(&event);

   Serial.print("X: "); Serial.print(event.acceleration.x); Serial.print("  ");
   Serial.print("Y: "); Serial.print(event.acceleration.y); Serial.print("  ");
   Serial.print("Z: "); Serial.print(event.acceleration.z); Serial.print("  ");
   delay(500);

      int NewRoll = get_roll();
    int NewPitch =get_pitch();

if (NewRoll < (oldRoll-sens) && oldRoll != 0 && NewRoll != 0) {
   // sound the alarm 
    sirena_policia(); 

 }else if (NewRoll > (oldRoll+sens) && oldRoll!= 0 && NewRoll != 0) {
   // sound the alarm 
    sirena_policia(); 

 }

 
if (NewPitch < (oldPitch-sens) && oldPitch != 0 && NewPitch != 0) {
   // sound the alarm 
    sirena_policia(); 

  

 }else if (NewPitch > (oldPitch+sens) && oldPitch!= 0 && NewPitch != 0) {
   // sound the alarm 

  
  sirena_policia();

 }

    
   
}


void sirena_policia() {                             //This function produces the 4th siren(POLICE) sound with led transition.
for(int i=2;i<=10;i+=2)
digitalWrite(i,HIGH);
  for(int hz = 440; hz < 1000; hz++){
    tone(b1, hz, 50);
    delay(5);
  }
for(int i=2;i<=10;i+=2)
digitalWrite(i,LOW);
 for(int i=4;i<=12;i+=2)
digitalWrite(i,HIGH);
  for(int hz = 1000; hz > 440; hz--){
    tone(b1, hz, 50);
    delay(5);
    }
  }

Credits

sinester09

sinester09

0 projects • 0 followers

Comments