-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Final stable version
- Loading branch information
Showing
5 changed files
with
2,316 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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; | ||
} |
This file contains 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
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 | ||
|
Oops, something went wrong.