Advertisement
Guest User

Untitled

a guest
Aug 13th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #include "pitches.h"
  2.  
  3. const int xpin = A3; // x-axis of the accelerometer
  4. const int ypin = A2; // y-axis
  5. const int zpin = A1; // z-axis (only on 3-axis models)
  6.  
  7. int x = 0;
  8. int y = 0;
  9. int z = 0;
  10.  
  11. int lastx = 0;
  12. int lasty = 0;
  13. int lastz = 0;
  14.  
  15. int diffx = 0;
  16. int diffy = 0;
  17. int diffz = 0;
  18.  
  19. int piezo_pin = 11;
  20.  
  21. int flag = 0;
  22.  
  23. int limit = 20;
  24.  
  25. int total = 0;
  26.  
  27. void setup()
  28. {
  29. Serial.begin(9600);
  30. //tone(piezo_pin, NOTE_C4, 80);
  31. //delay(100);
  32. //tone(piezo_pin, NOTE_G3, 80);
  33. }
  34.  
  35.  
  36. void loop()
  37. {
  38.  
  39. if(flag == 0) {
  40.  
  41. }
  42.  
  43. if(flag == 1) {
  44. tone(piezo_pin, NOTE_C4);
  45. }
  46. if(flag == 0) {
  47. noTone(piezo_pin);
  48. }
  49.  
  50. x = analogRead(xpin);
  51. diffx = abs(x - lastx);
  52. Serial.print(diffx);
  53. lastx = x;
  54.  
  55. Serial.print("\t");
  56.  
  57. //if(diffx > limit) { xalarm(); }
  58.  
  59. y = analogRead(ypin);
  60. diffy = abs(y - lasty);
  61. Serial.print(diffy);
  62. lasty = y;
  63.  
  64. Serial.print("\t");
  65.  
  66. //if(diffy > limit) { yalarm(); }
  67.  
  68. z = analogRead(zpin);
  69. diffz = abs(z - lastz);
  70. Serial.print(diffz);
  71. lastz = z;
  72.  
  73. Serial.print("\t");
  74.  
  75.  
  76. //if(diffz > limit) { zalarm(); }
  77.  
  78. total = diffx + diffy + diffz;
  79.  
  80. Serial.print("t: ");
  81.  
  82. Serial.print(total);
  83.  
  84. Serial.println();
  85.  
  86. if(total > limit) {
  87. flag = 1;
  88. }
  89.  
  90. else {
  91. flag = 0;
  92. }
  93.  
  94. delay(100);
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement