// https://forum.arduino.cc/t/how-to-setup-array-structure-for-melody-sequences/1404240
// tone sequencer v1
// thisnote = row * column + column
#include <avr/pgmspace.h> // offload arrays from RAM to PROGMEM
/*
s or 6 = 1/16 (sixteenth)
e or 8 = 1/8 (eighth)
q or 4 = 1/4 (quarter)
t or 3 = 1/3 (third)
h or 2 = 1/2 (half)
w or 1 = 1/1 (whole)
r or 0 = rest
*/
unsigned long columnSpeed = 50; // milliseconds per element - speed control
byte tonePin = 3; // PWM pin
const char startup[] PROGMEM = { // single-dimension array of sequenced elements
"GN4..q...."
"FN4....qq."
"EN4...q..q"
"CN4.q....."
};
const char shutdown[] PROGMEM = {
"GN4.....4."
"FN4..44..."
"EN4.4..4.."
"CN4......4"
};
void setup() {
pinMode(tonePin, OUTPUT);
}
void loop() {
sequencer(startup, sizeof(startup));
sequencer(shutdown, sizeof(shutdown));
}
void sequencer(char list[], int listLength) {
for (int col = 0; col < listLength; col++) {
for (int row = 0; row < ledCount; row++) {
switch (pgm_read_byte(&list[row * listLength + col])) { // locate element with row and column
case '*': digitalWrite(ledPin[row], HIGH); break; // turn LED ON
case '.': digitalWrite(ledPin[row], LOW); break; // turn LED OFF
case '6':
case 's': duration = 1000 / 16; break;
case '4':
case 'q': duration = 1000 / 4; break;
case '8':
case 'e': duration = 1000 / 8; break;
case '3':
case 't': duration = 1000 / 3; break;
case '2':
case 'h': duration = 1000 / 2; break;
case '1':
case 'w': duration = 1000 / 1; break;
default: break;
}
tone (tonePin, note, duration)
delay(duration * .8);
}
delay(columnSpeed); // regulate the speed of the sequence
}
}