Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
Final stable version
  • Loading branch information
lemontyc authored Jun 12, 2017
1 parent 3498e17 commit f87d9f6
Show file tree
Hide file tree
Showing 5 changed files with 2,316 additions and 0 deletions.
72 changes: 72 additions & 0 deletions Accelerometer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Draw.cpp - Library for flashing Morse code.
Created by David A. Mellis, November 2, 2007.
Released into the public domain.
*/

#include "Arduino.h"
#include "Accelerometer.h"

// Use this pins for the Accelerometer axes
#define accelXpin A0
#define accelYpin A1
#define accelZpin A2

//-------------------Accelerometer variables-----------------
int posXShip,posYShip;
int xVoltage,yVoltage,zVoltage;
float xAcc,yAcc,zAcc,roll,pitch;
float scale=98;

//Callibration values
int xZero=502;
int zZero=512;
int yZero=497;
//Sensitivity to detect direction
int sense=20;
//----------------------------------------------------------

void Accelerometer::readVoltage(){
xVoltage=analogRead(accelXpin);
yVoltage=analogRead(accelYpin);
zVoltage=analogRead(accelZpin);
}

void Accelerometer::getRollPitch(){
xAcc=(xVoltage-xZero)/scale;
yAcc=(yVoltage-yZero)/scale;
zAcc=(zVoltage-zZero)/scale;
// apply trigonometry to get the pitch and roll:
pitch = atan(xAcc/sqrt(pow(yAcc,2) + pow(zAcc,2)));
roll = atan(yAcc/sqrt(pow(xAcc,2) + pow(zAcc,2)));
//convert radians into degrees
pitch = pitch * (180.0/PI);
roll = roll * (180.0/PI) ;
}

int Accelerometer::isShaking(){
readVoltage();
getRollPitch();
if(pitch>30||pitch<-30||roll>30||roll<-30){
delay(500);
readVoltage();
getRollPitch();
if(pitch>30||pitch<-30||roll>30||roll<-30){
return 0;
}
}else{
return 1;
}
}

int Accelerometer::getRoll(){
readVoltage();
getRollPitch();
return roll;
}

int Accelerometer::getPitch(){
readVoltage();
getRollPitch();
return pitch;
}
31 changes: 31 additions & 0 deletions Accelerometer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Morse.h - Library for flashing Morse code.
Created by David A. Mellis, November 2, 2007.
Released into the public domain.
*/
#ifndef Accelerometer_h
#define Accelerometer_h

#include "Arduino.h"


class Accelerometer
{
public:
/*
* Reads Voltage from accelerometer Pins
*/
void readVoltage();
/*
* Does some fancy trigonometry in order to get roll/pitch from voltage readings
* We had to calibrate the accelereometer BEFORE applying maths
* Reference: http://physics.rutgers.edu/~aatish/teach/srr/workshop3.pdf
*/
void getRollPitch();
int isShaking(); // Returns 1 if NOT shaking
int getPitch(); //Returns current pitch value
int getRoll(); //Returns current roll value
private:
};
#endif

Loading

0 comments on commit f87d9f6

Please sign in to comment.