Advertisement
Guest User

Digital metronome - Arduino

a guest
Dec 25th, 2023
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Autor: Martin Chlebovec (martinius96)
  2. //Projekt: Metronóm - UART only
  3. //Revízia: 29. May 2020
  4. //Donate for more: paypal.me/chlebovec
  5.  
  6. //Nastavenie vývodov Arduina k jednotlivým perifériám (Vstupy, výstupy)
  7. const int BUZZER = 9;
  8. const int LED_BLU = 8;
  9. const int LED_RED = 7;
  10. const int BUT_CHANGE_TICK = 6;
  11. const int BUT_plusone = 5;
  12. const int BUT_minusone = 4;
  13. const int BUT_plusten = 3;
  14. const int BUT_minusten = 2;
  15.  
  16. //Nastavenie počiatočných časových premenných s millis()
  17. unsigned long lastDebounceTime1 = 0;
  18. unsigned long lastDebounceTime2 = 0;
  19. unsigned long lastDebounceTime3 = 0;
  20. unsigned long lastDebounceTime4 = 0;
  21. unsigned long lastDebounceTime5 = 0;
  22. unsigned long debounceInterval = 50;
  23. unsigned long elapsedInterval = 0;
  24. unsigned long interval = 1000; //interval tikání v milisekundách
  25.  
  26. //Nastavenie počiatočných stavov tlačidiel
  27. int buttonState1 = HIGH;
  28. int buttonState2 = HIGH;
  29. int buttonState3 = HIGH;
  30. int buttonState4 = HIGH;
  31. int buttonState5 = HIGH;
  32.  
  33. //Nastavenie posledných stavov tlačidiel
  34. int lastButtonState1 = HIGH;
  35. int lastButtonState2 = HIGH;
  36. int lastButtonState3 = HIGH;
  37. int lastButtonState4 = HIGH;
  38. int lastButtonState5 = HIGH;
  39.  
  40. //Deklarácia hodnôt frekvencií pre doby + poslednú dobu, rytmus, atď...
  41. const int freqFirst = 1700;
  42. const int freqFourths = 1500;
  43. int rythm = 4;
  44. int freq = 0;
  45. int bpm = 0;
  46. int tick_case = 0;
  47. int actual = 0;
  48.  
  49. //Defínicia funkcie pre výpis BPM
  50. void showBPM()
  51. {
  52.   // bpm = 60000 / interval;
  53.   Serial.print("BPM: ");
  54.   Serial.println(bpm);
  55. }
  56.  
  57. //Defínicia funkcie pre výpis rytmusu
  58. void showRythm()
  59. {
  60.   Serial.print("Takt: ");
  61.   Serial.print(rythm);
  62.   Serial.println("/4");
  63. }
  64.  
  65. //Defínicia funkcie s volaním funkcií pre výpis BPM, rytmusu
  66. void aktualizaceDispleje()
  67. {
  68.   showBPM();
  69.   showRythm();
  70. }
  71.  
  72. void setup() {
  73.   //počiatočné nastavenie výstupov a vstupov
  74.   pinMode(LED_RED, OUTPUT);
  75.   pinMode(LED_BLU, OUTPUT);
  76.   pinMode(BUZZER, OUTPUT);
  77.   pinMode(BUT_CHANGE_TICK, INPUT_PULLUP);
  78.   pinMode(BUT_plusone, INPUT_PULLUP);
  79.   pinMode(BUT_minusone, INPUT_PULLUP);
  80.   pinMode(BUT_plusten, INPUT_PULLUP);
  81.   pinMode(BUT_minusten, INPUT_PULLUP);
  82.   Serial.begin(115200);
  83.  
  84. }
  85.  
  86. void loop() {
  87.   int reading1 = digitalRead(BUT_plusone);
  88.   int reading2 = digitalRead(BUT_minusone);
  89.   int reading3 = digitalRead(BUT_plusten);
  90.   int reading4 = digitalRead(BUT_minusten);
  91.   int reading5 = digitalRead(BUT_CHANGE_TICK);
  92.  
  93.   if (reading1 != lastButtonState1) {
  94.     lastDebounceTime1 = millis();
  95.   }
  96.  
  97.   if (reading2 != lastButtonState2) {
  98.     lastDebounceTime2 = millis();
  99.   }
  100.  
  101.   if (reading3 != lastButtonState3) {
  102.     lastDebounceTime3 = millis();
  103.   }
  104.  
  105.   if (reading4 != lastButtonState4) {
  106.     lastDebounceTime4 = millis();
  107.   }
  108.  
  109.   if (reading5 != lastButtonState5) {
  110.     lastDebounceTime5 = millis();
  111.   }
  112.  
  113.   if ((millis() - lastDebounceTime1) >= debounceInterval) {
  114.     if (reading1 != buttonState1) {
  115.       buttonState1 = reading1;
  116.       if (buttonState1 == HIGH) {
  117.         bpm = bpm + 1; //bmp + 1
  118.         interval = 60000 / bpm;
  119.         freq = freqFirst;
  120.         aktualizaceDispleje();
  121.         digitalWrite(LED_RED, LOW);
  122.         digitalWrite(LED_BLU, HIGH);
  123.         tone(BUZZER, freq);
  124.         actual = 1;
  125.         elapsedInterval = millis();
  126.       }
  127.     }
  128.   }
  129.  
  130.   if ((millis() - lastDebounceTime2) >= debounceInterval) {
  131.     if (reading2 != buttonState2) {
  132.       buttonState2 = reading2;
  133.       if (buttonState2 == HIGH) {
  134.         bpm = bpm - 1; //bmp - 1
  135.         if (bpm < 0) {
  136.           bpm = 0;
  137.         }
  138.         interval = 60000 / bpm;
  139.         freq = freqFirst;
  140.         aktualizaceDispleje();
  141.         digitalWrite(LED_RED, LOW);
  142.         digitalWrite(LED_BLU, HIGH);
  143.         tone(BUZZER, freq);
  144.         actual = 1;
  145.         elapsedInterval = millis();
  146.       }
  147.     }
  148.   }
  149.  
  150.   if ((millis() - lastDebounceTime3) >= debounceInterval) {
  151.     if (reading3 != buttonState3) {
  152.       buttonState3 = reading3;
  153.       if (buttonState3 == HIGH) {
  154.         bpm = bpm + 10; //bmp + 10
  155.         interval = 60000 / bpm;
  156.         freq = freqFirst;
  157.         aktualizaceDispleje();
  158.         digitalWrite(LED_RED, LOW);
  159.         digitalWrite(LED_BLU, HIGH);
  160.         tone(BUZZER, freq);
  161.         actual = 1;
  162.         elapsedInterval = millis();
  163.       }
  164.     }
  165.   }
  166.  
  167.   if ((millis() - lastDebounceTime4) >= debounceInterval) {
  168.     if (reading4 != buttonState4) {
  169.       buttonState4 = reading4;
  170.       if (buttonState4 == HIGH) {
  171.         bpm = bpm - 10; //bmp - 10
  172.         if (bpm < 0) {
  173.           bpm = 0;
  174.         }
  175.         interval = 60000 / bpm;
  176.         freq = freqFirst;
  177.         aktualizaceDispleje();
  178.         digitalWrite(LED_RED, LOW);
  179.         digitalWrite(LED_BLU, HIGH);
  180.         tone(BUZZER, freq);
  181.         actual = 1;
  182.         elapsedInterval = millis();
  183.       }
  184.     }
  185.   }
  186.  
  187.   if ((millis() - lastDebounceTime5) >= debounceInterval) {
  188.     if (reading5 != buttonState5) {
  189.       buttonState5 = reading5;
  190.       if (buttonState5 == HIGH) {
  191.         tick_case++;
  192.         switch (tick_case) {
  193.           case 0:
  194.             rythm = 4; // 4/4 takt
  195.             break;
  196.           case 1:
  197.             rythm = 5; // 5/4 takt
  198.             break;
  199.           case 2:
  200.             rythm = 3; // 3/4 takt
  201.             break;
  202.           default:
  203.             tick_case = 0;
  204.             rythm = 4;
  205.             break;
  206.         }
  207.         aktualizaceDispleje();
  208.       }
  209.     }
  210.   }
  211.  
  212.   if (freq != 0 && bpm != 0) {
  213.     if ((millis() - elapsedInterval) >= debounceInterval) { //50ms beep
  214.       digitalWrite(LED_RED, LOW);
  215.       digitalWrite(LED_BLU, LOW);
  216.       noTone(BUZZER);
  217.     }
  218.     if ((millis() - elapsedInterval) >= interval) {
  219.       actual++;
  220.       if (actual > rythm) {
  221.         actual = 1;
  222.       }
  223.       if (actual < rythm) {
  224.         freq = freqFirst;
  225.         digitalWrite(LED_RED, LOW);
  226.         digitalWrite(LED_BLU, HIGH);
  227.       }
  228.       if (actual == rythm) {
  229.         freq = freqFourths;
  230.         digitalWrite(LED_RED, HIGH);
  231.         digitalWrite(LED_BLU, LOW);
  232.       }
  233.       tone(BUZZER, freq);
  234.       elapsedInterval = millis();
  235.     }
  236.   }
  237.  
  238.   lastButtonState1 = reading1;
  239.   lastButtonState2 = reading2;
  240.   lastButtonState3 = reading3;
  241.   lastButtonState4 = reading4;
  242.   lastButtonState5 = reading5;
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement