Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_PCD8544.h>
- #define SPEAKER 6
- #define NOTE_INRV 1.059463
- #define C4 261.626
- #define TRANS 0
- int BPM = 222;
- int noteDiv = 16;
- // 1 3 6 8 10
- //0 2 4 5 7 9 11
- const PROGMEM int octvs[] =
- {6, 5, 5, 6, 5, 5, 6, 5, 5, 6, 5, 5, 6, 5, 6, 5,//P7 bar 26
- 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 5, 4, 5, 4,
- 5, 5, 4, 5, 5, 4, 5, 5, 4, 5, 5, 4, 5, 5, 5, 4,
- 5, 5, 4, 5, 5, 4, 5, 4, 5, 5, 5, 5, 6, 5, 6, 5,//P8
- 6, 6, 5, 6, 6, 5, 6, 6, 5, 6, 6, 5, 6, 6, 6, 6,
- 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 6, 6, 6, 5,
- 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5,
- 6, 5, 5, 6, 5, 5, 6, 5, 5, 5, 5, 5, 5, 5, 5, 5,//P9
- 6, 5, 6, 5, 6, 5, 6, 5, 6, 6, 6, 5, 6, 6, 6, 6,
- 6, 6, 6, 6, 6, 6, 6, 5, 6, 5, 5, 6, 5, 5, 6, 5,
- 5, 5, 5, 5, 6, 5, 6, 5, 6, 6, 6, 5, 5, 5, 5, 6,
- 6, 6, 6, 5, 6, 6, 6, 5, 6, 6, 6, 5, 6, 6, 6, 5,//P10
- 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
- 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 6, 6, 6, 6,
- 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
- 7, 6, 7, 6, 7, 6, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6,//P11
- 3, 3, 3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 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,//P7 bar 26
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,//P8
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,//P9
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,//P10
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,//P11
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
- 16,48
- };
- const int noteCount = sizeof(notes) / sizeof(int);
- Adafruit_PCD8544 display = Adafruit_PCD8544(22, 23, 24, 25, 26);
- int height = display.height();
- int width = display.width();
- void setup() {
- // init LCD
- display.begin();
- display.setContrast(50);
- display.setTextSize(1);
- display.setTextColor(BLACK);
- display.setRotation(2); // using it upside down
- display.setCursor(0, 0);
- display.clearDisplay();
- display.display();
- }
- void loop() {
- for (int i = 0; i < noteCount; i++) {
- int octv = pgm_read_word_near(octvs + i);
- int note = pgm_read_word_near(notes + i);
- int beat = pgm_read_word_near(beats + i);
- int freq = freqCalc(octv, note);
- long dura = ((long)beat) * 1000 * 60 / BPM / noteDiv;
- long now = millis();
- display.clearDisplay();
- display.print("i=");
- display.println(i);
- display.print("o,n=");
- display.print(octv);
- display.print(",");
- display.println(note);
- display.print("b=");
- display.println(beat);
- display.print("f=");
- display.println(freq);
- display.print("t=");
- display.println(dura);
- display.display();
- dura -= millis() - now;
- if (freq != 0)
- tone(SPEAKER, freq);
- delay(dura);
- }
- noTone(SPEAKER);
- delay(500);
- display.clearDisplay();
- display.display();
- while (1);
- }
- int freqCalc(int oct, int note) {
- if (oct < 0 || note < 0)
- return (0);
- else
- return ((1 << oct) * (pow(NOTE_INRV , (note + TRANS))) * C4 / 16);
- }
Advertisement
Add Comment
Please, Sign In to add comment