/**
@author: s44310776, s44592947
@version: 10_19
Hardware Connections:
- Triple Axis Accellerometer
- 6 LEDs
- 1 Button Switch
*/
#include <SparkFun_ADXL345.h> //Acclerometer Library
/** Accelerometer configuration. */
ADXL345 adxl = ADXL345(); //Use I2C
/** LED array. Indexed from left to right when looking at FDR
with the LEDs on the bottom. */
int leds[] = {2, 3, 4, 5, 6, 7};
/// Counts the number of seconds elapsed since launch in decimal format.
int d_seconds = 0;
/// Counts the number of seconds elapsed since launch in binary format.
char b_seconds[5] = {0, 0, 0, 0, 0};
unsigned long last_time; // Used to count elapsed time.
bool launched = false; // True when the glider is launched.
bool landed = false; // True after the plane lands.
float launch_threshold = 2.5; // Threshold to exceed when launching (g-forces)
float land_threshold = 3.5; // Threshold to exceed when landing (g-forces)
int push_button = 10; // Button pin 10.
unsigned long duration; // Duration of button push.
void(* resetFunc)(void) = 0; //declare reset function at address 0
void setup() {
Serial.begin(9600);
//Accelerometer Initialisation
adxl.powerOn();
adxl.setRangeSetting(8); // Range in g (lower for more accuracy)
//Initialise Pins
for (int i; i < sizeof(leds)/sizeof(int); i++) {
pinMode(leds[i], OUTPUT);
}
pinMode(push_button, INPUT);
//Set last_time on startup.
last_time = millis();
}
void loop() {
//Take Accelerometer Reading
int x,y,z;
adxl.readAccel(&x,&y,&z);
//Convert to g-force
float g = sqrt(pow(x, 2) + pow(y, 2) + pow(z,2))/31;
Serial.print(g);
/*
* Set launched to true when g-force is above the threshold.
* Set landed when g-force is above the threshold and plane is in the air.
*/
if (g > launch_threshold && launched == false && landed == false) {
launched = true;
delay(1000);
} else if ((g > land_threshold && launched == true && landed == false) || (d_seconds >= 31)) {
launched = false;
landed = true;
}
// While the plane is launched count seconds and write the LEDs every second.
Serial.println(d_seconds);
if (launched && millis() - last_time > 1000 && d_seconds < 31) {
d_seconds++;
decimalToBinary(d_seconds, b_seconds);
writeLeds(b_seconds);
last_time = millis();
}
// Time the button push.
while (digitalRead(push_button) == LOW) {
// Count duration using delay up to 3 seconds.
if (duration < 30) {
duration++;
Serial.println(duration);
delay(100);
} else { // If pressed for more than 3 seconds, reset.
writeLeds("101010");
delay(2000);
writeLeds("000000");
resetFunc();
}
}
//Write all LEDs on if the duration of the button push is less than 3 seconds.
if (duration < 30 && duration > 0) {
duration = 0;
writeLeds("111111");
} else {
duration = 0;
}
}
/* @function decimalToBinary
* --------------------------
* @description - converts a decimal integer to a binary integer.
* @param {int} dec - the decimal integer to convert.
* @param {char} output[] - the array to output the integer in binary format.
*/
void decimalToBinary(int dec, char output[]) {
int index = 0;
while (dec != 0) {
/* Finds decimal%2 and adds to the binary value */
output[index] = (dec % 2) + '0';
dec /= 2;
index++;
}
while(index < 5) {
output[index] = '0';
index++;
}
output[index] = '\0';
}
/* @function writeLeds
* -------------------
* @description - write to LED pins to display the elapsed time.
* @param {char} input[] - the array containing the binary number to express.
*/
void writeLeds(char input[]) {
for (int i = 0; i < 6; i++) {
digitalWrite(leds[i], input[i] == '0' ? LOW : HIGH);
}
}
engg
最新推荐文章于 2024-08-05 19:18:46 发布