leo3065

Arduino Speaker Test

Jul 3rd, 2016
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.62 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Adafruit_GFX.h>
  3. #include <Adafruit_PCD8544.h>
  4.  
  5. #define SPEAKER 6
  6.  
  7. #define NOTE_INRV 1.059463
  8. #define C4 261.626
  9. #define TRANS 0
  10.  
  11. int BPM = 222;
  12. int noteDiv = 16;
  13.  
  14. //  1   3       6   8   10
  15. //0   2   4   5   7   9   11
  16. const PROGMEM int octvs[] =
  17. {6, 5, 5, 6, 5, 5, 6, 5, 5, 6, 5, 5, 6, 5, 6, 5,//P7 bar 26
  18.  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 5, 4, 5, 4,
  19.  5, 5, 4, 5, 5, 4, 5, 5, 4, 5, 5, 4, 5, 5, 5, 4,
  20.  5, 5, 4, 5, 5, 4, 5, 4, 5, 5, 5, 5, 6, 5, 6, 5,//P8
  21.  6, 6, 5, 6, 6, 5, 6, 6, 5, 6, 6, 5, 6, 6, 6, 6,
  22.  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 6, 6, 6, 5,
  23.  6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5,
  24.  6, 5, 5, 6, 5, 5, 6, 5, 5, 5, 5, 5, 5, 5, 5, 5,//P9
  25.  6, 5, 6, 5, 6, 5, 6, 5, 6, 6, 6, 5, 6, 6, 6, 6,
  26.  6, 6, 6, 6, 6, 6, 6, 5, 6, 5, 5, 6, 5, 5, 6, 5,
  27.  5, 5, 5, 5, 6, 5, 6, 5, 6, 6, 6, 5, 5, 5, 5, 6,
  28.  6, 6, 6, 5, 6, 6, 6, 5, 6, 6, 6, 5, 6, 6, 6, 5,//P10
  29.  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  30.  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 6, 6, 6, 6,
  31.  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  32.  7, 6, 7, 6, 7, 6, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6,//P11
  33.  3, 3, 3, 4, 3, 3, 4, 4, 3, 4, 4, 4, 4, 4, 4, 5,
  34.  4, 4, 4, 5, 4, 4, 5, 5, 4, 5, 5, 5, 4, 5, 5, 6,
  35.  6,-1
  36.  };
  37. const PROGMEM int notes[] =
  38. { 2,11, 7, 2,11, 7, 2,11, 7, 0, 7, 2, 0, 2, 0, 2,//P7 bar 26
  39.  11, 7, 2,11, 7, 2,11, 7, 2, 6, 2, 9, 6, 9, 2, 9,
  40.   9, 4,11, 9, 4,11, 9, 4,11, 7, 4,11, 7, 4, 7,11,
  41.   6, 4,11, 6, 4,11, 7,11, 9, 2,11, 2, 0, 2, 2, 2,//P8
  42.   4, 0, 9, 4, 0, 9, 4, 0, 9, 6, 0, 9, 7, 0, 6, 7,
  43.   9, 4, 9, 0, 7, 4, 7, 0, 6, 0, 6, 9, 4, 0, 4, 9,
  44.   4,11, 4, 6, 2,11, 2, 6, 1,11, 1, 6, 4,11, 4, 6,
  45.   2,11, 7, 2,11, 7, 2,11, 7,11, 7, 4,11, 7,11, 4,//P9
  46.   0, 7, 0, 4, 2, 7, 2, 4, 4, 0, 4, 7, 7, 4, 7, 0,
  47.   6, 7, 6, 0, 4, 0, 4, 7, 2,11, 6, 2,11, 6, 0, 6,
  48.  11, 6,11, 3, 3,11, 3, 6, 6, 3, 6, 6,11, 6,11, 3,
  49.   9, 4, 9,11, 7, 4, 7,11, 6, 4, 6,11, 7, 2, 7,11,//P10
  50.  11, 7, 4,11, 7, 4,11, 7, 4, 7, 4,11, 7, 4, 7, 9,
  51.  11, 7,11, 7, 4, 1, 4, 1,11, 7, 4, 0, 7, 4, 7, 0,
  52.  11, 7, 2,11, 7, 2,11, 7, 2, 9, 7, 2,11, 7,11, 2,
  53.   2, 9, 2, 6, 0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  75.  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  76.  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  77. 16,48
  78.  };
  79.  
  80. const int noteCount = sizeof(notes) / sizeof(int);
  81.  
  82.  
  83. Adafruit_PCD8544 display = Adafruit_PCD8544(22, 23, 24, 25, 26);
  84. int height = display.height();
  85. int width = display.width();
  86.  
  87. void setup() {
  88.  
  89.  
  90.   // init LCD
  91.   display.begin();
  92.   display.setContrast(50);
  93.   display.setTextSize(1);
  94.   display.setTextColor(BLACK);
  95.   display.setRotation(2); // using it upside down
  96.   display.setCursor(0, 0);
  97.   display.clearDisplay();
  98.   display.display();
  99. }
  100.  
  101. void loop() {
  102.   for (int i = 0; i < noteCount; i++) {
  103.     int octv = pgm_read_word_near(octvs + i);
  104.     int note = pgm_read_word_near(notes + i);
  105.     int beat = pgm_read_word_near(beats + i);
  106.     int freq = freqCalc(octv, note);
  107.     long dura = ((long)beat) * 1000 * 60 / BPM / noteDiv;
  108.     long now = millis();
  109.  
  110.     display.clearDisplay();
  111.     display.print("i=");
  112.     display.println(i);
  113.     display.print("o,n=");
  114.     display.print(octv);
  115.     display.print(",");
  116.     display.println(note);
  117.     display.print("b=");
  118.     display.println(beat);
  119.     display.print("f=");
  120.     display.println(freq);
  121.     display.print("t=");
  122.     display.println(dura);
  123.     display.display();
  124.  
  125.     dura -= millis() - now;
  126.     if (freq != 0)
  127.       tone(SPEAKER, freq);
  128.     delay(dura);
  129.   }
  130.   noTone(SPEAKER);
  131.   delay(500);
  132.   display.clearDisplay();
  133.   display.display();
  134.   while (1);
  135. }
  136.  
  137. int freqCalc(int oct, int note) {
  138.   if (oct < 0 || note < 0)
  139.     return (0);
  140.   else
  141.     return ((1 << oct) * (pow(NOTE_INRV , (note + TRANS))) * C4 / 16);
  142. }
Advertisement
Add Comment
Please, Sign In to add comment