Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

143 řádky
3.1 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 MPU3050_PWR (0x6b)
  8. #define MPU3050_I2C (0x69)
  9. #define MPU3050_REG (0x21)
  10. //Preset buzzer WiringPi port
  11. #define BUZZER 0
  12. //Preset button interrupt WiringPi port
  13. #define BUTTON 5
  14. //Preset button debounce time
  15. #define DEBOUNCE_MS 1000
  16. //Declaring all global variables
  17. volatile uint8_t trigger;
  18. volatile uint8_t photoTaken;
  19. volatile unsigned int prevTime = 0;
  20. volatile uint8_t btnClicked = 0;
  21. void buttonInterrupt()
  22. {
  23. //Get the current time and its difference from last interrupt
  24. unsigned int currentTime = millis();
  25. unsigned int deltaTime = currentTime - prevTime;
  26. //Check if the deltaTime from the last click is longer than the preset debounce time
  27. if (deltaTime > DEBOUNCE_MS)
  28. {
  29. //Set new values
  30. btnClicked = 1;
  31. prevTime = currentTime;
  32. if (trigger == 0)
  33. {
  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. //Double short beep to inform rider
  44. digitalWrite(BUZZER, HIGH);
  45. delay(100);
  46. digitalWrite(BUZZER, LOW);
  47. delay(100);
  48. digitalWrite(BUZZER, HIGH);
  49. delay(100);
  50. digitalWrite(BUZZER, LOW);
  51. }
  52. }
  53. }
  54. int main()
  55. {
  56. //Initialize photoTaken and trigger
  57. photoTaken = 0;
  58. trigger = 0;
  59. wiringPiSetup();
  60. pinMode(BUTTON, INPUT);
  61. pinMode(BUZZER, OUTPUT);
  62. //Setup ISR (button) and I2C (gyro sensor)
  63. int isrSetup = wiringPiISR(BUTTON, INT_EDGE_FALLING, &buttonInterrupt);
  64. int i2cSetup = wiringPiI2CSetup(MPU3050_I2C);
  65. //Check if setups are successful
  66. if (i2cSetup < 0||isrSetup < 0)
  67. {
  68. return 0;
  69. }
  70. //Power I2C and begin tracking
  71. wiringPiI2CWriteReg8(i2cSetup, MPU3050_PWR, 0x01);
  72. //Declare 'gyroInput' to be used to contain the input from gyro sensor later
  73. int gyroInput;
  74. //Initialize 'count' to be used to keep track how many consecutive 0.1 secs had the gyro sensor stayed at 0
  75. int count = 0;
  76. //Double short beep to inform rider
  77. digitalWrite(BUZZER, HIGH);
  78. delay(100);
  79. digitalWrite(BUZZER, LOW);
  80. delay(100);
  81. digitalWrite(BUZZER, HIGH);
  82. delay(100);
  83. digitalWrite(BUZZER, LOW);
  84. while (1)
  85. {
  86. //Get gyro sensor input
  87. gyroInput = wiringPiI2CReadReg8(i2cSetup, MPU3050_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. }