From f33c949d3ce62846ffef9483e7f61bbeba188b87 Mon Sep 17 00:00:00 2001 From: BinHong Lee Date: Mon, 17 Apr 2017 02:02:22 -0700 Subject: [PATCH] Code commented --- main.c | 82 +++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 61 insertions(+), 21 deletions(-) diff --git a/main.c b/main.c index f5582b3..413deb7 100644 --- a/main.c +++ b/main.c @@ -3,100 +3,140 @@ #include #include #include -#include +//Preset information about MPU3050 (gyro sensor) #define MPU3050_PWR (0x6b) #define MPU3050_I2C (0x69) #define MPU3050_REG (0x21) -#define LED 0 + +//Preset buzzer WiringPi port +#define BUZZER 0 + +//Preset button interrupt WiringPi port #define BUTTON 5 +//Preset button debounce time #define DEBOUNCE_MS 1000 -volatile int trigger; +//Declaring all global variables +volatile uint8_t trigger; volatile uint8_t photoTaken; volatile unsigned int prevTime = 0; volatile uint8_t btnClicked = 0; -void myInterrupt(void) +void buttonInterrupt() { + //Get the current time and its difference from last interrupt unsigned int currentTime = millis(); unsigned int deltaTime = currentTime - prevTime; - printf("%d\n", deltaTime); + //Check if the deltaTime from the last click is longer than the preset debounce time if (deltaTime > DEBOUNCE_MS) { + //Set new values btnClicked = 1; prevTime = currentTime; - printf("test\n"); + if (trigger == 0) { - photoTaken = 0; + //Set trigger and turn off buzzer trigger = 1; - digitalWrite(LED, LOW); - printf("T R I G G E R R E D\n"); + digitalWrite(BUZZER, LOW); } else { + //Reset photoTaken and trigger photoTaken = 0; trigger = 0; - digitalWrite(LED, HIGH); + + //Double short beep to inform rider + digitalWrite(BUZZER, HIGH); delay(100); - digitalWrite(LED, LOW); + digitalWrite(BUZZER, LOW); delay(100); - digitalWrite(LED, HIGH); + digitalWrite(BUZZER, HIGH); delay(100); - digitalWrite(LED, LOW); - printf("T R I G G E R R E D A G A I N\n"); + digitalWrite(BUZZER, LOW); } } } int main() { + //Initialize photoTaken and trigger photoTaken = 0; trigger = 0; wiringPiSetup(); pinMode(BUTTON, INPUT); - pinMode(LED, OUTPUT); - - int isrSetup = wiringPiISR(BUTTON, INT_EDGE_FALLING, &myInterrupt); + pinMode(BUZZER, OUTPUT); + + //Setup ISR (button) and I2C (gyro sensor) + int isrSetup = wiringPiISR(BUTTON, INT_EDGE_FALLING, &buttonInterrupt); int i2cSetup = wiringPiI2CSetup(MPU3050_I2C); + //Check if setups are successful if (i2cSetup < 0||isrSetup < 0) + { return 0; + } + //Power I2C and begin tracking wiringPiI2CWriteReg8(i2cSetup, MPU3050_PWR, 0x01); + + //Declare 'gyroInput' to be used to contain the input from gyro sensor later int gyroInput; + //Initialize 'count' to be used to keep track how many consecutive 0.1 secs had the gyro sensor stayed at 0 int count = 0; - while(1) + + //Double short beep to inform rider + digitalWrite(BUZZER, HIGH); + delay(100); + digitalWrite(BUZZER, LOW); + delay(100); + digitalWrite(BUZZER, HIGH); + delay(100); + digitalWrite(BUZZER, LOW); + + while (1) { + //Get gyro sensor input gyroInput = wiringPiI2CReadReg8(i2cSetup, MPU3050_REG); - if(gyroInput == 0) + if (gyroInput == 0) { + //Increase count if gyro sensor returns 0 count++; + + //If the gyro sensor reading stayed at '0' consecutively for over a full second if (count > 10 && trigger == 0 && photoTaken == 0) { - digitalWrite(LED, HIGH); + //Set off the buzzer + digitalWrite(BUZZER, HIGH); + //Allow rider 2 seconds to cancel taking photo and sending email delay(2000); if (!btnClicked) { + /* If rider did not press the button after 2 seconds, send an email asking + * for help from emergency contact attaching a photo taken on the spot + */ system("raspistill -v -o photo.jpg && ./sendMail.sh"); photoTaken = 1; } + //Reset button clicks btnClicked = 0; } } else { + //Reset counter to 0 count = 0; - digitalWrite(LED, LOW); } + //Only read value from gyro sensor every 0.1 sec delay(100); } + return 0; }