Advertisement
Guest User

Arduino metronome

a guest
Oct 18th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2. #include "pitches.h"
  3.  
  4.  
  5. //PINS
  6. const int SPEAKER_PIN = 8;
  7. const int LED_PIN = 13;
  8. //const int BUTTON_PIN = 7;
  9. const int knockSensor = A0;
  10. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  11.  
  12. const int threshold = 200;
  13. const long debounceDelay = 50;
  14. const boolean ACCENT = true;
  15.  
  16. int BPM = 0;
  17.  
  18. unsigned long last;
  19. unsigned long tdelay;
  20. int signiture = 4;
  21.  
  22. long lastDebounceTime = 0;
  23. int buttonState;
  24. int lastButtonState = LOW;
  25.  
  26. const int taps_len = 24;
  27. unsigned long taps[taps_len];
  28. int next_tap = 0;
  29. int total_taps = 0;
  30.  
  31. int noteDuration;
  32. int beat;
  33.  
  34. void setup() {
  35. Serial.begin(9600);
  36.  
  37. pinMode(LED_PIN, OUTPUT);
  38. pinMode(SPEAKER_PIN, OUTPUT);
  39. //pinMode(BUTTON_PIN, INPUT);
  40.  
  41. lcd.begin(16, 2);
  42.  
  43. beat = 0;
  44. //calculate seconds per beat
  45. tdelay = 60000/BPM;
  46. last = millis();
  47.  
  48. // to calculate the note duration, take one second
  49. // divided by the note type.
  50. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
  51. noteDuration = 1000 / 16;
  52. }
  53.  
  54. void loop() {
  55. int elapsed = millis() - last;
  56.  
  57. int reading = analogRead(knockSensor);
  58.  
  59.  
  60. if ((millis() - lastDebounceTime) > debounceDelay) {
  61.  
  62. // only toggle the LED if the new button state is HIGH
  63. if (reading >= threshold) {
  64. if((millis() - lastDebounceTime) > 2000) {
  65. reset_taps();
  66. }
  67. lcd.setCursor(15, 0);
  68. lcd.write(-1);
  69. taps[next_tap] = millis();
  70. Serial.println(String(next_tap) + ": " + String(taps[next_tap]));
  71. //lcd.setCursor(0, 1);
  72. //lcd.print(taps[next_tap]);
  73.  
  74. BPM = bpms();
  75. Serial.println("BPM: " + String(BPM));
  76.  
  77. next_tap = ++next_tap % taps_len;
  78. total_taps++;
  79.  
  80. lastDebounceTime = millis();
  81. } else {
  82. lcd.setCursor(15, 0);
  83. lcd.print(" ");
  84. }
  85. }
  86.  
  87. if(BPM == 0) {
  88. return;
  89. }
  90.  
  91. if(elapsed > noteDuration) {
  92. digitalWrite(LED_PIN, LOW);
  93. }
  94.  
  95. tdelay = 60000/BPM;
  96.  
  97. if(elapsed < tdelay) {
  98. return;
  99. }
  100.  
  101.  
  102. lcd.clear();
  103. lcd.print("BPM: " + String(BPM));
  104.  
  105. int play_note = NOTE_C4;
  106.  
  107.  
  108. beat = beat % signiture;
  109. lcd.setCursor(0, 1);
  110. lcd.print(String(beat+1));
  111.  
  112. if(ACCENT && beat == 0) {
  113. play_note = NOTE_C6;
  114. }
  115.  
  116. tone(SPEAKER_PIN, play_note, noteDuration);
  117. digitalWrite(LED_PIN, HIGH);
  118.  
  119.  
  120. last = millis();
  121. beat++;
  122. }
  123.  
  124. int bpms() {
  125. if(total_taps < signiture) {
  126. return 0;
  127. }
  128.  
  129. unsigned long total = 0;
  130. for(int i=1; i<=signiture; i++) {
  131. int tap = next_tap - i;
  132.  
  133. if(tap < 0) {
  134. tap = taps_len + tap;
  135. }
  136.  
  137. if(i>1) {
  138. Serial.print(String(tap+1%taps_len) + ": " + String(taps[tap+1%taps_len]) + " - ");
  139. Serial.print(String(tap) + ": " + String(taps[tap]));
  140.  
  141. total += (taps[tap+1%taps_len] - taps[tap]);
  142. }
  143. Serial.println(total);
  144. }
  145.  
  146. return 60000/(total / (signiture - 1));
  147. }
  148.  
  149. void reset_taps() {
  150. beat = 0;
  151. for(int i=0;i<taps_len;i++) {
  152. taps[i] = 0;
  153. }
  154. next_tap = 0;
  155. total_taps = 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement