You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
3.0 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <wiringPi.h>
  5. #include <wiringPiI2C.h>
  6. //Preset information about MPU3050 (gyro sensor)
  7. #define GYRO_I2C (0x69)
  8. #define GYRO_REG (0x21)
  9. //Preset buzzer WiringPi port
  10. #define BUZZER 0
  11. //Preset button interrupt WiringPi port
  12. #define BUTTON 5
  13. //Preset button debounce time
  14. #define DEBOUNCE_MS 1000
  15. //Declaring all global variables
  16. volatile uint8_t trigger;
  17. volatile uint8_t photoTaken;
  18. volatile unsigned int prevTime = 0;
  19. volatile uint8_t btnClicked = 0;
  20. void buttonInterrupt()
  21. {
  22. //Get the current time and its difference from last interrupt
  23. unsigned int currentTime = millis();
  24. unsigned int deltaTime = currentTime - prevTime;
  25. //Check if the deltaTime from the last click is longer than the preset debounce time
  26. if (deltaTime > DEBOUNCE_MS)
  27. {
  28. //Set new values
  29. btnClicked = 1;
  30. prevTime = currentTime;
  31. if (trigger == 0)
  32. {
  33. printf("Paused\n");
  34. //Set trigger and turn off buzzer
  35. trigger = 1;
  36. digitalWrite(BUZZER, LOW);
  37. }
  38. else
  39. {
  40. //Reset photoTaken and trigger
  41. photoTaken = 0;
  42. trigger = 0;
  43. printf("Continue\n");
  44. //Double short beep to inform rider
  45. digitalWrite(BUZZER, HIGH);
  46. delay(100);
  47. digitalWrite(BUZZER, LOW);
  48. delay(100);
  49. digitalWrite(BUZZER, HIGH);
  50. delay(100);
  51. digitalWrite(BUZZER, LOW);
  52. }
  53. }
  54. }
  55. int main()
  56. {
  57. //Initialize photoTaken and trigger
  58. photoTaken = 0;
  59. trigger = 0;
  60. wiringPiSetup();
  61. pinMode(BUTTON, INPUT);
  62. pinMode(BUZZER, OUTPUT);
  63. //Setup ISR (button) and I2C (gyro sensor)
  64. int isrSetup = wiringPiISR(BUTTON, INT_EDGE_FALLING, &buttonInterrupt);
  65. int i2cSetup = wiringPiI2CSetup(GYRO_I2C);
  66. //Check if setups are successful
  67. if (i2cSetup < 0||isrSetup < 0)
  68. {
  69. return 0;
  70. }
  71. //Declare 'gyroInput' to be used to contain the input from gyro sensor later
  72. int gyroInput;
  73. //Initialize 'count' to be used to keep track how many consecutive 0.1 secs had the gyro sensor stayed at 0
  74. int count = 0;
  75. //Double short beep to inform rider
  76. digitalWrite(BUZZER, HIGH);
  77. delay(100);
  78. digitalWrite(BUZZER, LOW);
  79. delay(100);
  80. digitalWrite(BUZZER, HIGH);
  81. delay(100);
  82. digitalWrite(BUZZER, LOW);
  83. printf("While loop begins~\n");
  84. while (1)
  85. {
  86. //Get gyro sensor input
  87. gyroInput = wiringPiI2CReadReg8(i2cSetup, GYRO_REG);
  88. if (gyroInput == 0)
  89. {
  90. //Increase count if gyro sensor returns 0
  91. count++;
  92. //If the gyro sensor reading stayed at '0' consecutively for over a full second
  93. if (count > 10 && trigger == 0 && photoTaken == 0)
  94. {
  95. //Set off the buzzer
  96. digitalWrite(BUZZER, HIGH);
  97. //Allow rider 2 seconds to cancel taking photo and sending email
  98. delay(2000);
  99. if (!btnClicked)
  100. {
  101. /* If rider did not press the button after 2 seconds, send an email asking
  102. * for help from emergency contact attaching a photo taken on the spot
  103. */
  104. system("raspistill -v -o photo.jpg && ./sendMail.sh");
  105. photoTaken = 1;
  106. }
  107. //Reset button clicks
  108. btnClicked = 0;
  109. }
  110. }
  111. else
  112. {
  113. //Reset counter to 0
  114. count = 0;
  115. }
  116. //Only read value from gyro sensor every 0.1 sec
  117. delay(100);
  118. }
  119. return 0;
  120. }