Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include "Adafruit_Trellis.h" //need adafruit trellis library
- String results[] = {"86420","86420","86420","86420",
- "86420","86420","86420","86420",
- "86420","86420","86420","86420",
- "86420","86420","86420","86420",};
- //array of each step, keeps track of which drum tracks should play. For example, if the first digit is 8 it's off and 9 is on, sending a trigger out
- // on that specific step. even numbers = off, odd numbers = on.
- Adafruit_Trellis matrix0 = Adafruit_Trellis();
- Adafruit_TrellisSet trellis = Adafruit_TrellisSet(&matrix0);
- int BPM = 300;
- int channel;
- int currentchan= 0;
- #define numKeys (16)
- #define INTPIN A2
- void setup() {
- Serial.begin(9600);
- pinMode(A1, INPUT); // potentiometer for BPM control
- pinMode(INTPIN, INPUT); // INT pin requires a pullup
- digitalWrite(INTPIN, HIGH);
- pinMode(12,OUTPUT);
- for(int j=2;j<7;j++){
- pinMode(j, INPUT); // Buttons
- digitalWrite(j, HIGH);
- }
- for(int j=7;j<12;j++){
- pinMode(j, OUTPUT); // Ouput LEDS, triggers Wav_Trigger with 5v pulse
- }
- trellis.begin(0x70); // begin() with the addresses of each panel in order
- // light up all the LEDs in order
- for (uint8_t i=0; i<numKeys; i++) {
- trellis.setLED(i);
- trellis.writeDisplay();
- delay(50);
- }
- // then turn them off
- for (uint8_t i=0; i<numKeys; i++) {
- trellis.clrLED(i);
- trellis.writeDisplay();
- delay(50);
- }
- }
- void loop() {
- for (uint8_t i=0; i<numKeys; i++) {
- int BPM= analogRead(A1);
- BPM = map(BPM, 0, 1023, 400, 100);
- // int BPMdisplay= (60000 /BPM);
- //Serial.println(BPMdisplay);
- for(int b=2;b<7;b++){
- if (digitalRead(b) == LOW){ //change channels
- channel = b - 1;
- if ((digitalRead(2) == LOW) && (digitalRead(6)== LOW)){ //reset all paterns on each channel when these 2 buttons are held down together
- for (int r=0;r<16;r++){
- results[r]= "86420";
- trellis.clrLED(r);
- trellis.writeDisplay();
- }
- for (uint8_t i=0; i<numKeys; i++) {
- trellis.setLED(i);
- trellis.writeDisplay();
- delay(20);
- }
- // then turn them off
- for (uint8_t i=0; i<numKeys; i++) {
- trellis.clrLED(i);
- trellis.writeDisplay();
- delay(20);
- }
- }
- break;
- }
- }
- if(currentchan != channel){ //clear current board LEDs so you can input steps on a different channel
- for (uint8_t i=0; i<numKeys; i++) {
- trellis.clrLED(i);
- trellis.writeDisplay();
- }
- currentchan = channel;
- if (currentchan == 1){
- for (uint8_t i=0; i<numKeys; i++) {
- if(results[i].indexOf('9') > -1){
- trellis.setLED(i);
- trellis.writeDisplay();
- }
- }
- }
- if (currentchan == 2){
- for (uint8_t i=0; i<numKeys; i++) {
- if(results[i].indexOf('7') > -1){
- trellis.setLED(i);
- trellis.writeDisplay();
- }
- }
- }
- if (currentchan == 3){
- for (uint8_t i=0; i<numKeys; i++) {
- if(results[i].indexOf('5') > -1){
- trellis.setLED(i);
- trellis.writeDisplay();
- }
- }
- }
- if (currentchan == 4){
- for (uint8_t i=0; i<numKeys; i++) {
- if(results[i].indexOf('3') > -1){
- trellis.setLED(i);
- trellis.writeDisplay();
- }
- }
- }
- if (currentchan == 5){
- for (uint8_t i=0; i<numKeys; i++) {
- if(results[i].indexOf('1') > -1){
- trellis.setLED(i);
- trellis.writeDisplay();
- }
- }
- }
- }
- BlinkPattern(i,currentchan,BPM);
- if (trellis.readSwitches()) { // If a button was just pressed or released...
- for (uint8_t i=0; i<numKeys; i++) { // go through every button
- if (trellis.justPressed(i)) { // if it was pressed...
- if (trellis.isLED(i)){ // Alternate the LED
- trellis.clrLED(i);
- if (currentchan == 1){
- if(results[i].indexOf('9') > -1){ //If Button is activated, array is changed
- results[i].replace('9','8');
- }
- }
- if (currentchan == 2){
- if(results[i].indexOf('7') > -1){ //If Button is activated, array is changed
- results[i].replace('7','6');
- }
- }
- if (currentchan == 3){
- if(results[i].indexOf('5') > -1){ //If Button is activated, array is changed
- results[i].replace('5','4');
- }
- }
- if (currentchan == 4){
- if(results[i].indexOf('3') > -1){ //If Button is activated, array is changed
- results[i].replace('3','2');
- }
- }
- if (currentchan == 5){
- if(results[i].indexOf('1') > -1){ //If Button is activated, array is changed
- results[i].replace('1','0');
- }
- }
- }
- else{
- if (currentchan == 1){
- trellis.setLED(i); //Array value changed to default if button not activated
- if(results[i].indexOf('8') > -1){
- results[i].replace('8','9');
- }
- }
- if (currentchan == 2){
- trellis.setLED(i); //Array value changed to default if button not activated
- if(results[i].indexOf('6') > -1){
- results[i].replace('6','7');
- }
- }
- if (currentchan == 3){
- trellis.setLED(i); //Array value changed to default if button not activated
- if(results[i].indexOf('4') > -1){
- results[i].replace('4','5');
- }
- }
- if (currentchan == 4){
- trellis.setLED(i); //Array value changed to default if button not activated
- if(results[i].indexOf('2') > -1){
- results[i].replace('2','3');
- }
- }
- if (currentchan == 5){
- trellis.setLED(i); //Array value changed to default if button not activated
- if(results[i].indexOf('0') > -1){
- results[i].replace('0','1');
- }
- }
- }
- }
- }
- trellis.writeDisplay(); // tell the trellis to set the LEDs we requested
- }
- }
- // PrintArray();
- }
- void BlinkPattern(uint8_t i, int channel, int BPM)
- {
- if (trellis.isLED(i)== 1){
- trellis.clrLED(i);
- trellis.writeDisplay();
- int counter = Outputs(i);
- delay((BPM/4)-counter);
- // digitalWrite(pinON, LOW);
- trellis.setLED(i);
- trellis.writeDisplay();
- delay((BPM *3)/4);
- }
- else{
- int counter = Outputs(i);
- trellis.setLED(i);
- trellis.writeDisplay();
- delay((BPM/2)-counter);
- trellis.clrLED(i);
- trellis.writeDisplay();
- delay(BPM/2);
- // digitalWrite(pinON, LOW);
- }
- }
- void PrintArray(){ // for testing purposes
- Serial.println("Array");
- for (int i =0; i< 4; i++) Serial.println(results[i]);
- Serial.println(" ");
- for (int i =4; i< 8; i++) Serial.println(results[i]);
- Serial.println(" ");
- for (int i =8; i< 12; i++) Serial.println(results[i]);
- Serial.println(" ");
- for (int i =12; i<16 ; i++) Serial.println(results[i]);
- Serial.println(" ");
- }
- int Outputs(uint8_t i){ //checks array for each step and if the digit is odd, fires off a trigger pulse.
- int counter = 0;
- if(results[i].indexOf('9') > -1){ //Output
- digitalWrite(7, HIGH);
- delay(10);
- digitalWrite(7, LOW);
- counter = counter + 10;
- }
- if(results[i].indexOf('7') > -1){ //Output
- digitalWrite(8, HIGH);
- delay(10);
- digitalWrite(8, LOW);
- counter = counter + 10;
- }
- if(results[i].indexOf('5') > -1){ //Output
- digitalWrite(9, HIGH);
- delay(10);
- digitalWrite(9, LOW);
- counter = counter + 10;
- }
- if(results[i].indexOf('3') > -1){ //Output
- digitalWrite(10, HIGH);
- delay(10);
- digitalWrite(10, LOW);
- counter = counter + 10;
- }
- if(results[i].indexOf('1') > -1){ //Output
- digitalWrite(11, HIGH);
- delay(10);
- digitalWrite(11, LOW);
- counter = counter + 10;
- }
- return counter;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement