25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.c 1.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <wiringPi.h>
  5. #include <wiringPiI2C.h>
  6. #include <errno.h>
  7. #define MPU3050_PWR (0x6b)
  8. #define MPU3050_I2C (0x69)
  9. #define MPU3050_REG (0x21)
  10. #define LED 0
  11. #define BUTTON 5
  12. #define DEBOUNCE_MS 1000
  13. volatile int trigger;
  14. volatile uint8_t photoTaken;
  15. volatile unsigned int prevTime = 0;
  16. volatile uint8_t btnClicked = 0;
  17. void myInterrupt(void)
  18. {
  19. unsigned int currentTime = millis();
  20. unsigned int deltaTime = currentTime - prevTime;
  21. printf("%d\n", deltaTime);
  22. if (deltaTime > DEBOUNCE_MS)
  23. {
  24. btnClicked = 1;
  25. prevTime = currentTime;
  26. printf("test\n");
  27. if (trigger == 0)
  28. {
  29. photoTaken = 0;
  30. trigger = 1;
  31. digitalWrite(LED, LOW);
  32. printf("T R I G G E R R E D\n");
  33. }
  34. else
  35. {
  36. photoTaken = 0;
  37. trigger = 0;
  38. digitalWrite(LED, HIGH);
  39. delay(100);
  40. digitalWrite(LED, LOW);
  41. delay(100);
  42. digitalWrite(LED, HIGH);
  43. delay(100);
  44. digitalWrite(LED, LOW);
  45. printf("T R I G G E R R E D A G A I N\n");
  46. }
  47. }
  48. }
  49. int main()
  50. {
  51. photoTaken = 0;
  52. trigger = 0;
  53. wiringPiSetup();
  54. pinMode(BUTTON, INPUT);
  55. pinMode(LED, OUTPUT);
  56. int isrSetup = wiringPiISR(BUTTON, INT_EDGE_FALLING, &myInterrupt);
  57. int i2cSetup = wiringPiI2CSetup(MPU3050_I2C);
  58. if (i2cSetup < 0||isrSetup < 0)
  59. return 0;
  60. wiringPiI2CWriteReg8(i2cSetup, MPU3050_PWR, 0x01);
  61. int gyroInput;
  62. int count = 0;
  63. while(1)
  64. {
  65. gyroInput = wiringPiI2CReadReg8(i2cSetup, MPU3050_REG);
  66. if(gyroInput == 0)
  67. {
  68. count++;
  69. if (count > 10 && trigger == 0 && photoTaken == 0)
  70. {
  71. digitalWrite(LED, HIGH);
  72. delay(2000);
  73. if (!btnClicked)
  74. {
  75. system("raspistill -v -o photo.jpg && ./sendMail.sh");
  76. photoTaken = 1;
  77. }
  78. btnClicked = 0;
  79. }
  80. }
  81. else
  82. {
  83. count = 0;
  84. digitalWrite(LED, LOW);
  85. }
  86. delay(100);
  87. }
  88. return 0;
  89. }