Browse Source

Code commented

master
BinHong Lee 7 years ago
parent
commit
f33c949d3c
1 changed files with 61 additions and 21 deletions
  1. +61
    -21
      main.c

+ 61
- 21
main.c View File

@@ -3,100 +3,140 @@
#include <stdint.h> #include <stdint.h>
#include <wiringPi.h> #include <wiringPi.h>
#include <wiringPiI2C.h> #include <wiringPiI2C.h>
#include <errno.h>


//Preset information about MPU3050 (gyro sensor)
#define MPU3050_PWR (0x6b) #define MPU3050_PWR (0x6b)
#define MPU3050_I2C (0x69) #define MPU3050_I2C (0x69)
#define MPU3050_REG (0x21) #define MPU3050_REG (0x21)
#define LED 0

//Preset buzzer WiringPi port
#define BUZZER 0

//Preset button interrupt WiringPi port
#define BUTTON 5 #define BUTTON 5
//Preset button debounce time
#define DEBOUNCE_MS 1000 #define DEBOUNCE_MS 1000


volatile int trigger;
//Declaring all global variables
volatile uint8_t trigger;
volatile uint8_t photoTaken; volatile uint8_t photoTaken;
volatile unsigned int prevTime = 0; volatile unsigned int prevTime = 0;
volatile uint8_t btnClicked = 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 currentTime = millis();
unsigned int deltaTime = currentTime - prevTime; 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) if (deltaTime > DEBOUNCE_MS)
{ {
//Set new values
btnClicked = 1; btnClicked = 1;
prevTime = currentTime; prevTime = currentTime;
printf("test\n");
if (trigger == 0) if (trigger == 0)
{ {
photoTaken = 0;
//Set trigger and turn off buzzer
trigger = 1; trigger = 1;
digitalWrite(LED, LOW);
printf("T R I G G E R R E D\n");
digitalWrite(BUZZER, LOW);
} }
else else
{ {
//Reset photoTaken and trigger
photoTaken = 0; photoTaken = 0;
trigger = 0; trigger = 0;
digitalWrite(LED, HIGH);

//Double short beep to inform rider
digitalWrite(BUZZER, HIGH);
delay(100); delay(100);
digitalWrite(LED, LOW);
digitalWrite(BUZZER, LOW);
delay(100); delay(100);
digitalWrite(LED, HIGH);
digitalWrite(BUZZER, HIGH);
delay(100); 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() int main()
{ {
//Initialize photoTaken and trigger
photoTaken = 0; photoTaken = 0;
trigger = 0; trigger = 0;


wiringPiSetup(); wiringPiSetup();
pinMode(BUTTON, INPUT); 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); int i2cSetup = wiringPiI2CSetup(MPU3050_I2C);


//Check if setups are successful
if (i2cSetup < 0||isrSetup < 0) if (i2cSetup < 0||isrSetup < 0)
{
return 0; return 0;
}


//Power I2C and begin tracking
wiringPiI2CWriteReg8(i2cSetup, MPU3050_PWR, 0x01); wiringPiI2CWriteReg8(i2cSetup, MPU3050_PWR, 0x01);

//Declare 'gyroInput' to be used to contain the input from gyro sensor later
int gyroInput; 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; 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); gyroInput = wiringPiI2CReadReg8(i2cSetup, MPU3050_REG);


if(gyroInput == 0)
if (gyroInput == 0)
{ {
//Increase count if gyro sensor returns 0
count++; count++;

//If the gyro sensor reading stayed at '0' consecutively for over a full second
if (count > 10 && trigger == 0 && photoTaken == 0) 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); delay(2000);


if (!btnClicked) 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"); system("raspistill -v -o photo.jpg && ./sendMail.sh");
photoTaken = 1; photoTaken = 1;
} }


//Reset button clicks
btnClicked = 0; btnClicked = 0;
} }
} }
else else
{ {
//Reset counter to 0
count = 0; count = 0;
digitalWrite(LED, LOW);
} }


//Only read value from gyro sensor every 0.1 sec
delay(100); delay(100);
} }

return 0; return 0;
} }

Loading…
Cancel
Save