Devices & Components
Arduino Uno Rev3
Tactile Switch, Top Actuated
Buzzer, Piezo
Adafruit Bicolor LED Square Pixel Matrix with I2C Backpack
Rotary potentiometer (generic)
Software & Tools
Arduino IDE
Project description
Code
ledMatrixMetronome.ino
c_cpp
Note: you will need to install the Adafruit LED Backpack library and the Adafruit GFX library to run this code. They can be installed directly from the Arduino IDE using the Arduino library manager. For detailed instructions see: https://learn.adafruit.com/adafruit-led-backpack/0-8-8x8-matrix-arduino-wiring-and-setup
1#include <Wire.h> 2#include <Adafruit_GFX.h> 3#include "Adafruit_LEDBackpack.h" 4 5// LED matrix object 6Adafruit_BicolorMatrix matrix = Adafruit_BicolorMatrix(); 7 8// variables associated with to ptm switch 9const int switchPin = 2; 10int switchState = 0; 11 12// variables associated with potentiometer + smoothing 13const int numReadings = 10; 14double potReadings[numReadings]; 15int readIndex = 0; 16int potTotal = 0; 17int potAverage = 0; 18 19//variables associated with time keeping 20int state = 0; 21unsigned long previousTime = 0; 22int interval; 23 24// variables determining display pattern 25// 4/4 26const int x0[] = {0,0,4,4,0,0,4,4,0,0,4,4,0,0,4,4}; 27const int y0[] = {0,4,4,0,0,4,4,0,0,4,4,0,0,4,4,0}; 28const int z0[] = {2,2,2,2,3,3,3,3,1,1,1,1,0,0,0,0}; 29const int w0[] = {4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4}; 30const int h0[] = {4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4}; 31 32// 3/4 33const int x1[] = {2,0,4,2,0,4,2,0,4,2,0,4}; 34const int y1[] = {0,4,4,0,4,4,0,4,4,0,4,4}; 35const int z1[] = {2,2,2,3,3,3,1,1,1,0,0,0}; 36const int w1[] = {4,4,4,4,4,4,4,4,4,4,4,4}; 37const int h1[] = {4,4,4,4,4,4,4,4,4,4,4,4}; 38 39// 6/8 40const int x2[] = {0,0,4,4,0,0,0,0,4,4,0,0}; 41const int y2[] = {4,0,4,0,4,0,4,0,4,0,4,0}; 42const int z2[] = {1,1,1,3,3,3,2,2,2,0,0,0}; 43const int w2[] = {4,8,4,4,8,4,4,8,4,4,8,4}; 44const int h2[] = {4,4,4,4,4,4,4,4,4,4,4,4}; 45 46// pointers to current display pattern 47const int *xp = x0; 48const int *yp = y0; 49const int *zp = z0; 50const int *wp = w0; 51const int *hp = h0; 52 53// number of iterations of display pattern 54int stateLimit = 16; 55 56// default and number of display patterns 57int TimeSig = 0; 58int TimeSigLimit = 3; 59 60//variables associated with piezo 61const int piezoPin = 8; 62int modulo = 4; // accent the first beat of the bar 63 64 65void setup() { 66 Serial.begin(9600); 67 Serial.println("8x8 LED Matrix Test"); 68 69 matrix.begin(0x70); // pass in the address 70 matrix.setRotation(3); // rotate so pins are at 0th row 71 72 pinMode(switchPin, INPUT); 73 74 // initialise all pot readings to 0 75 for (int thisReading = 0; thisReading < numReadings; thisReading++) { 76 potReadings[thisReading] = 0; 77 } 78} 79 80 81void loop() { 82 83 //potentiometer smoothing 84 potTotal = potTotal - potReadings[readIndex]; //subtract the last reading 85 potReadings[readIndex] = analogRead(A0); 86 potTotal = potTotal + potReadings[readIndex]; //add current reading to total 87 readIndex = readIndex + 1; //advance to next position in array 88 // if we're at the end of the array... 89 if (readIndex >= numReadings) { 90 // ...wrap around to the beginning: 91 readIndex = 0; 92 } 93 potAverage = sqrt(potTotal/numReadings) * 100; //take the square root to get a non linear tempo response, multiply by 100 to increase the sensitivity of map (which can only handle integers). 94 delay(1); // delay in between reads for stability 95 96 // map the pot average to an interval (bpm = 1000/interval * 60). 97 interval = map(potAverage,0,3200,1500,200); //bpm can be set between 45bpm (1500 ms interval) and 300bpm (200ms interval). Must be mapped in reverse to encode non linear tempo mapping. 98 Serial.println(interval); 99 100 //check whether the button has been pressed to change time sig 101 switchState = digitalRead(switchPin); 102 if(switchState == HIGH){ 103 // move to next time signature 104 TimeSig += 1; 105 // if the time signature limit is reached go back to start 106 if (TimeSig == TimeSigLimit){ 107 TimeSig = 0; 108 } 109 // update the display pattern based on the time signature 110 switch(TimeSig){ 111 // 4/4 112 case 0: 113 xp = x0; 114 yp = y0; 115 zp = z0; 116 wp = w0; 117 hp = h0; 118 stateLimit = 16; 119 modulo = 4; 120 break; 121 // 3/4 122 case 1: 123 xp = x1; 124 yp = y1; 125 zp = z1; 126 wp = w1; 127 hp = h1; 128 stateLimit = 12; 129 modulo = 3; 130 break; 131 // 6/8 132 case 2: 133 xp = x2; 134 yp = y2; 135 zp = z2; 136 wp = w2; 137 hp = h2; 138 stateLimit = 12; 139 modulo = 6; 140 break; 141 } 142 // reset the state counter 143 state = 0; 144 // clear the display 145 matrix.clear(); 146 matrix.writeDisplay(); 147 // delay to allow button time to unpress 148 delay(250); 149 } 150 151 // update matrix with new pattern 152 matrix.fillRect(xp[state],yp[state],wp[state],hp[state],zp[state]); 153 154 //how much time has passed since the last beat? 155 unsigned long currentTime = millis(); 156 157 // if is time for the next beat 158 if (currentTime - previousTime > interval){ 159 // reset time keeping 160 previousTime = currentTime; 161 // update matrix display 162 matrix.writeDisplay(); 163 // if is the first beat of the bar play tone an octave higher (A at 880hz) 164 if(state % modulo == 0){ 165 tone(piezoPin,880,20); 166 } 167 // else play an A at 440 hz 168 else{ 169 tone(piezoPin,440,20); 170 } 171 // increase state 172 state += 1; 173 // reset state to 0 once it has reached state limit 174 if(state == stateLimit){ 175 state = 0; 176 } 177 } 178} 179
Downloadable files
Breadboard Layout for LED Matrix Metronome
For information about the wiring of the LED Matrix, see https://learn.adafruit.com/adafruit-led-backpack/0-8-8x8-matrix-arduino-wiring-and-setup
Breadboard Layout for LED Matrix Metronome
Image of Breadboard Layout
For information about the wiring of the LED Matrix, see https://learn.adafruit.com/adafruit-led-backpack/0-8-8x8-matrix-arduino-wiring-and-setup
Image of Breadboard Layout

Breadboard Layout for LED Matrix Metronome
For information about the wiring of the LED Matrix, see https://learn.adafruit.com/adafruit-led-backpack/0-8-8x8-matrix-arduino-wiring-and-setup
Breadboard Layout for LED Matrix Metronome
Image of Breadboard Layout
For information about the wiring of the LED Matrix, see https://learn.adafruit.com/adafruit-led-backpack/0-8-8x8-matrix-arduino-wiring-and-setup
Image of Breadboard Layout

Comments
Only logged in users can leave comments