2011-11-18

9) Syncuino: Cleared Code - Same Function


This example shows that the space for a certain set of functions on Arduino (in this case the step sequencer Syncuino) can be reduced by some simple coding tricks. This version of syncuino features also a bug-fix for holding notes. Of course it does also feature transposing notes by a ps/2 keyboard introduced in the last version.

Here´s the difference:
Version 8:
Binary sketch size: 12646 bytes (of a 126976 byte maximum)
Version 9 with cleared code:
Binary sketch size: 11602 bytes (of a 126976 byte maximum)

So the Arduino Mega needs 1044 bytes less, yust by ably programming. You also have less code to write.

1st example: Using loops

old code:
digitalWrite(22, HIGH);
digitalWrite(24, LOW);
digitalWrite(26, LOW);
digitalWrite(28, LOW);
digitalWrite(30, LOW);
digitalWrite(32, LOW);
digitalWrite(34, LOW);
digitalWrite(36, LOW);
digitalWrite(38, LOW);
digitalWrite(40, LOW);
digitalWrite(42, LOW);
digitalWrite(44, LOW);
digitalWrite(46, LOW);
digitalWrite(48, LOW);
digitalWrite(50, LOW);
digitalWrite(52, LOW);

new code:
digitalWrite(22, HIGH);
for(int i=24;i<53;i=i+2){digitalWrite(i, LOW);}



2nd example: Don´t use variables if not necessary

old code:
Shuffle_Flag = digitalRead(ShufflePin);
if(Shuffle_Flag == LOW) {
ShuffleDelay = 0;
}
if (Shuffle_Flag == HIGH){
ShuffleDelay = 2; // start every 2nd note two MIDI clock signals later (ShuffleDelay = 1...4)
}

new code:
if(digitalRead(ShufflePin) == LOW) {ShuffleDelay = 0;}
else if (digitalRead(ShufflePin) == HIGH){ShuffleDelay = 2;}

3rd example:
Use a sub program for code that appears more than once in the code.


Here´s the cleared and bug-fixed Syncuino Code (Version 9):



// Declaration of Varialbes

byte midi_start = 0xfa;
byte midi_stop = 0xfc;
byte midi_clock = 0xf8;
byte midi_continue = 0xfb;
int play_flag = 0;
byte data;
int clock_step=0;
int note = 0x3F;
int LowestNote=36;
int HighestNote=36+3*12; //3 Octaves over LowestNote

int Note1 = (96*1)/16-5;
int Note2 = (96*2)/16-5;
int Note3 = (96*3)/16-5;
int Note4 = (96*4)/16-5;
int Note5 = (96*5)/16-5;
int Note6 = (96*6)/16-5;
int Note7 = (96*7)/16-5;
int Note8 = (96*8)/16-5;
int Note9 = (96*9)/16-5;
int Note10 = (96*10)/16-5;
int Note11 = (96*11)/16-5;
int Note12 = (96*12)/16-5;
int Note13 = (96*13)/16-5;
int Note14 = (96*14)/16-5;
int Note15 = (96*15)/16-5;
int Note16 = (96*16)/16-5;

int SeqLength = 6*16; //Sequence length is 16*16th notes

int ShufflePin = 2;
int ShuffleDelay = 0;

int Seq1Pin = 3;
int Seq2Pin = 4;
int PlayMode = 1;

int HoldNote_Flag = HIGH;

int transpose = 0;
const int DataPin = 20; //yellow cable
const int IRQpin = 21; //blue cable
#include
PS2Keyboard keyboard;

// Initialization

void setup() {

Serial.begin(31250);

delay(1000);
keyboard.begin(DataPin, IRQpin);


pinMode(ShufflePin, INPUT); //Pin 0 for shuffle on/off as input

for(int i=22;i<53;i=i+2){pinMode(i, OUTPUT);} for(int i=2;i<6;i++){pinMode(i, INPUT);} digitalWrite(22, HIGH); for(int i=24;i<53;i=i+2){digitalWrite(i, LOW);} } // Main Programm void loop() { //------------------------------------------------------------------------------------------- // Check keyboard if (keyboard.available()) { // read the next key char c = keyboard.read(); // check for some of function-keys if (c == PS2_ESC) {transpose=0;} else if (c == PS2_F1) {transpose=1;} else if (c == PS2_F2) {transpose=2;} else if (c == PS2_F3) {transpose=3;} else if (c == PS2_F4) {transpose=4;} else if (c == PS2_F5) {transpose=5;} else if (c == PS2_F6) {transpose=6;} else if (c == PS2_F7) {transpose=7;} else if (c == PS2_F8) {transpose=8;} else if (c == PS2_F9) {transpose=9;} else if (c == PS2_F10) {transpose=10;} else if (c == PS2_F11) {transpose=11;} else if (c == PS2_F12) {transpose=12;} else {transpose=transpose;} } //------------------------------------------------------------------------------------------- // Check Shuffle if(digitalRead(ShufflePin) == LOW) {ShuffleDelay = 0;} else if (digitalRead(ShufflePin) == HIGH){ShuffleDelay = 2;} // start every 2nd note two MIDI clock signals later (ShuffleDelay = 1...4) Note1 = (96*1)/16-5; Note2 = (96*2)/16-5+ShuffleDelay; Note3 = (96*3)/16-5; Note4 = (96*4)/16-5+ShuffleDelay; Note5 = (96*5)/16-5; Note6 = (96*6)/16-5+ShuffleDelay; Note7 = (96*7)/16-5; Note8 = (96*8)/16-5+ShuffleDelay; Note9 = (96*9)/16-5; Note10 = (96*10)/16-5+ShuffleDelay; Note11 = (96*11)/16-5; Note12 = (96*12)/16-5+ShuffleDelay; Note13 = (96*13)/16-5; Note14 = (96*14)/16-5+ShuffleDelay; Note15 = (96*15)/16-5; Note16 = (96*16)/16-5+ShuffleDelay; //------------------------------------------------------------------------------------------- // Check PlayMode if(digitalRead(Seq1Pin) == HIGH) {PlayMode = 1;} //Play sequence forwards else if(digitalRead(Seq1Pin == LOW)) {PlayMode = 2;} //Play sequence backwards //------------------------------------------------------------------------------------------- // Check MIDI if(Serial.available() > 0) {
data = Serial.read();
if(data == midi_start) {
play_flag = 1;
clock_step=0;
}
else if(data == midi_continue) {
play_flag = 1;
}

else if(data == midi_stop) {
play_flag = 0;
clock_step=0;
sendMidiNote (0x80, note, 0x7F); //last note off

digitalWrite(22, HIGH);
for(int i=24;i<53;i=i+2){digitalWrite(i, LOW);} } else if((data == midi_clock) && (play_flag == 1) && PlayMode == 1) { Sync1(); //Play sequence forwards } else if((data == midi_clock) && (play_flag == 1) && PlayMode == 2) { Sync2(); //Play sequence backwards } } } //------------------------------------------------------------------------------------------- // Functions void Sync1() { // play forwards 16 x 16th notes, repeat after the cycle is finshed clock_step = clock_step+1; if (clock_step==Note1){PlayNote1();digitalWrite(22, HIGH);digitalWrite(52, LOW);} else if (clock_step==Note2){PlayNote2();digitalWrite(24, HIGH);digitalWrite(22, LOW);} else if (clock_step==Note3){PlayNote3();digitalWrite(26, HIGH);digitalWrite(24, LOW);} else if (clock_step==Note4){PlayNote4();digitalWrite(28, HIGH);digitalWrite(26, LOW);} else if (clock_step==Note5){PlayNote5();digitalWrite(30, HIGH);digitalWrite(28, LOW);} else if (clock_step==Note6){PlayNote6();digitalWrite(32, HIGH);digitalWrite(30, LOW);} else if (clock_step==Note7){PlayNote7();digitalWrite(34, HIGH);digitalWrite(32, LOW);} else if (clock_step==Note8){PlayNote8();digitalWrite(36, HIGH);digitalWrite(34, LOW);} else if (clock_step==Note9){PlayNote9();digitalWrite(38, HIGH);digitalWrite(36, LOW);} else if (clock_step==Note10){PlayNote10();digitalWrite(40, HIGH);digitalWrite(38, LOW);} else if (clock_step==Note11){PlayNote11();digitalWrite(42, HIGH);digitalWrite(40, LOW);} else if (clock_step==Note12){PlayNote12();digitalWrite(44, HIGH);digitalWrite(42, LOW);} else if (clock_step==Note13){PlayNote13();digitalWrite(46, HIGH);digitalWrite(44, LOW);} else if (clock_step==Note14){PlayNote14();digitalWrite(48, HIGH);digitalWrite(46, LOW);} else if (clock_step==Note15){PlayNote15();digitalWrite(50, HIGH);digitalWrite(48, LOW);} else if (clock_step==Note16){PlayNote16();digitalWrite(52, HIGH);digitalWrite(50, LOW);} else if (clock_step==SeqLength){clock_step=0;} } void Sync2() { // play backwards 16 x 16th notes, repeat after the cycle is finshed, clock_step = clock_step+1; if (clock_step==Note1){PlayNote16();digitalWrite(52, HIGH);digitalWrite(22, LOW);} else if (clock_step==Note2){PlayNote15();digitalWrite(50, HIGH);digitalWrite(52, LOW);} else if (clock_step==Note3){PlayNote14();digitalWrite(48, HIGH);digitalWrite(50, LOW);} else if (clock_step==Note4){PlayNote13();digitalWrite(46, HIGH);digitalWrite(48, LOW);} else if (clock_step==Note5){PlayNote12();digitalWrite(44, HIGH);digitalWrite(46, LOW);} else if (clock_step==Note6){PlayNote11();digitalWrite(42, HIGH);digitalWrite(44, LOW);} else if (clock_step==Note7){PlayNote10();digitalWrite(40, HIGH);digitalWrite(42, LOW);} else if (clock_step==Note8){PlayNote9();digitalWrite(38, HIGH);digitalWrite(40, LOW);} else if (clock_step==Note9){PlayNote8();digitalWrite(36, HIGH);digitalWrite(38, LOW);} else if (clock_step==Note10){PlayNote7();digitalWrite(34, HIGH);digitalWrite(36, LOW);} else if (clock_step==Note11){PlayNote6();digitalWrite(32, HIGH);digitalWrite(34, LOW);} else if (clock_step==Note12){PlayNote5();digitalWrite(30, HIGH);digitalWrite(32, LOW);} else if (clock_step==Note13){PlayNote4();digitalWrite(28, HIGH);digitalWrite(30, LOW);} else if (clock_step==Note14){PlayNote3();digitalWrite(26, HIGH);digitalWrite(28, LOW);} else if (clock_step==Note15){PlayNote2();digitalWrite(24, HIGH);digitalWrite(26, LOW);} else if (clock_step==Note16){PlayNote1();digitalWrite(22, HIGH);digitalWrite(24, LOW);} else if (clock_step==SeqLength){clock_step=0;} } void sendMidiNote (byte midiCommand, byte noteValue, byte velocityValue){ Serial.print(midiCommand, BYTE); Serial.print(noteValue, BYTE); Serial.print(velocityValue, BYTE); } void PlayNote1() { if (analogRead(0) > 127 && HoldNote_Flag==HIGH) {
note = map(analogRead(0), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
note = map(analogRead(0), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote2() {

if (analogRead(1) > 127 && HoldNote_Flag==HIGH) {
note = map(analogRead(1), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(1) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
note = map(analogRead(1), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}


void PlayNote3() {
if (analogRead(2) > 127 && HoldNote_Flag==HIGH) {
note = map(analogRead(2), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(2) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
note = map(analogRead(2), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote4() {
if (analogRead(3) > 127 && HoldNote_Flag==HIGH) {
note = map(analogRead(3), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(3) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
note = map(analogRead(3), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote5() {
if (analogRead(4) > 127 && HoldNote_Flag==HIGH) {
note = map(analogRead(4), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(4) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
note = map(analogRead(4), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote6() {
if (analogRead(5) > 127 && HoldNote_Flag==HIGH) {
note = map(analogRead(5), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(5) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
note = map(analogRead(5), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote7() {
if (analogRead(6) > 127 && HoldNote_Flag==HIGH) {
note = map(analogRead(6), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(6) > 127 && HoldNote_Flag==LOW) {
note = map(analogRead(6), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote8() {
if (analogRead(7) > 127 && HoldNote_Flag==HIGH) {
note = map(analogRead(7), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(7) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
note = map(analogRead(7), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote9() {
if (analogRead(8) > 127 && HoldNote_Flag==HIGH) {
note = map(analogRead(8), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(8) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
note = map(analogRead(8), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}


void PlayNote10() {
if (analogRead(9) > 127 && HoldNote_Flag==HIGH) {
note = map(analogRead(9), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(9) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
note = map(analogRead(9), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote11() {
if (analogRead(10) > 127 && HoldNote_Flag==HIGH) {
note = map(analogRead(10), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(10) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
note = map(analogRead(10), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote12() {
if (analogRead(11) > 127 && HoldNote_Flag==HIGH) {
note = map(analogRead(11), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(11) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
note = map(analogRead(11), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote13() {
if (analogRead(12) > 127 && HoldNote_Flag==HIGH) {
note = map(analogRead(12), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(12) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
note = map(analogRead(12), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote14() {
if (analogRead(13) > 127 && HoldNote_Flag==HIGH) {
note = map(analogRead(13), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(13) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
note = map(analogRead(13), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote15() {
if (analogRead(14) > 127 && HoldNote_Flag==HIGH) {
note = map(analogRead(14), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(14) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
note = map(analogRead(14), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote16() {
if (analogRead(15) > 127 && HoldNote_Flag==HIGH) {
note = map(analogRead(15), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(15) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
note = map(analogRead(15), 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}



2011-08-30

8) Transposing notes with a ps/2 keyboard

Back with a new idea: I´d like to transpose the sequence by the function keys (F1-F12) of a standard ps/2 keyboard and use it like a "digital guitar" to play a MIDI synthesizer!

Here´s the functionality of the ps/2 connector: http://arduino.cc/playground/Main/PS2Keyboard

Note1: Pin20 is the "DataPin", Pin21 is the ClockPin of the ps/2 connection.
Note2: You have to extend the PS2Keyboard library to the function keys. Here are the scan-codes for the ps/2 keyboard: http://www.beyondlogic.org/keyboard/keybrd.htm

Have fun!





// Declaration of Varialbes

byte midi_start = 0xfa;
byte midi_stop = 0xfc;
byte midi_clock = 0xf8;
byte midi_continue = 0xfb;
int play_flag = 0;
byte data;
int clock_step=0;
int note = 0x3F;
int noteval = 0;
int LowestNote=36;
int HighestNote=36+3*12; //3 Octaves over LowestNote

int Note1 = (96*1)/16-5;
int Note2 = (96*2)/16-5;
int Note3 = (96*3)/16-5;
int Note4 = (96*4)/16-5;
int Note5 = (96*5)/16-5;
int Note6 = (96*6)/16-5;
int Note7 = (96*7)/16-5;
int Note8 = (96*8)/16-5;
int Note9 = (96*9)/16-5;
int Note10 = (96*10)/16-5;
int Note11 = (96*11)/16-5;
int Note12 = (96*12)/16-5;
int Note13 = (96*13)/16-5;
int Note14 = (96*14)/16-5;
int Note15 = (96*15)/16-5;
int Note16 = (96*16)/16-5;

int SeqLength = 6*16; //Sequence length is 16*16th notes

int ShufflePin = 2;
int Shuffle_Flag = 0;
int ShuffleDelay = 0;

int Seq1_Flag = HIGH;
int Seq2_Flag = LOW;
int Seq1Pin = 3;
int Seq2Pin = 4;
int PlayMode = 1;

int HoldNote_Flag = HIGH;

int transpose = 0;
const int DataPin = 20;
const int IRQpin = 21;

#include <PS2Keyboard.h>
PS2Keyboard keyboard;

// Initialization

void setup() {

Serial.begin(31250);

//pinMode(20, INPUT);
//pinMode(21, INPUT);
delay(1000);
keyboard.begin(DataPin, IRQpin);


pinMode(ShufflePin, INPUT); //Pin 0 for shuffle on/off as input

pinMode(22, OUTPUT);
pinMode(24, OUTPUT);
pinMode(26, OUTPUT);
pinMode(28, OUTPUT);
pinMode(30, OUTPUT);
pinMode(32, OUTPUT);
pinMode(34, OUTPUT);
pinMode(36, OUTPUT);
pinMode(38, OUTPUT);
pinMode(40, OUTPUT);
pinMode(42, OUTPUT);
pinMode(44, OUTPUT);
pinMode(46, OUTPUT);
pinMode(48, OUTPUT);
pinMode(50, OUTPUT);
pinMode(52, OUTPUT);

pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);

digitalWrite(22, HIGH);
digitalWrite(24, LOW);
digitalWrite(26, LOW);
digitalWrite(28, LOW);
digitalWrite(30, LOW);
digitalWrite(32, LOW);
digitalWrite(34, LOW);
digitalWrite(36, LOW);
digitalWrite(38, LOW);
digitalWrite(40, LOW);
digitalWrite(42, LOW);
digitalWrite(44, LOW);
digitalWrite(46, LOW);
digitalWrite(48, LOW);
digitalWrite(50, LOW);
digitalWrite(52, LOW);
}


// Main Programm

void loop() {

//-------------------------------------------------------------------------------------------
// Check keyboard
if (keyboard.available()) {

// read the next key
char c = keyboard.read();

// check for some of function-keys
if (c == PS2_ESC) {transpose=0;}
else if (c == PS2_F1) {transpose=1;}
else if (c == PS2_F2) {transpose=2;}
else if (c == PS2_F3) {transpose=3;}
else if (c == PS2_F4) {transpose=4;}
else if (c == PS2_F5) {transpose=5;}
else if (c == PS2_F6) {transpose=6;}
else if (c == PS2_F7) {transpose=7;}
else if (c == PS2_F8) {transpose=8;}
else if (c == PS2_F9) {transpose=9;}
else if (c == PS2_F10) {transpose=10;}
else if (c == PS2_F11) {transpose=11;}
else if (c == PS2_F12) {transpose=12;}
else {transpose=transpose;}
}


//-------------------------------------------------------------------------------------------
// Check Shuffle
Shuffle_Flag = digitalRead(ShufflePin);

if(Shuffle_Flag == LOW) {
ShuffleDelay = 0;
}

if (Shuffle_Flag == HIGH){

ShuffleDelay = 2; // start every 2nd note two MIDI clock signals later (ShuffleDelay = 1...4)

}


//-------------------------------------------------------------------------------------------
// Check PlayMode
Seq1_Flag = digitalRead(Seq1Pin); //Play sequence forwards
Seq2_Flag = digitalRead(Seq2Pin); //Play sequence backwards

if(Seq1_Flag == HIGH && Seq2_Flag == LOW) {
PlayMode = 1; //Play sequence forwards
}

if (Seq1_Flag == LOW && Seq2_Flag == HIGH){
PlayMode = 2; //Play sequence backwards
}


//-------------------------------------------------------------------------------------------
// Check MIDI
if(Serial.available() > 0) {
data = Serial.read();
if(data == midi_start) {
play_flag = 1;
clock_step=0;
}
else if(data == midi_continue) {
play_flag = 1;
}

else if(data == midi_stop) {
play_flag = 0;
clock_step=0;
sendMidiNote (0x80, note, 0x7F); //last note off
digitalWrite(22, HIGH);
digitalWrite(24, LOW);
digitalWrite(26, LOW);
digitalWrite(28, LOW);
digitalWrite(30, LOW);
digitalWrite(32, LOW);
digitalWrite(34, LOW);
digitalWrite(36, LOW);
digitalWrite(38, LOW);
digitalWrite(40, LOW);
digitalWrite(42, LOW);
digitalWrite(44, LOW);
digitalWrite(46, LOW);
digitalWrite(48, LOW);
digitalWrite(50, LOW);
digitalWrite(52, LOW);
}
else if((data == midi_clock) && (play_flag == 1) && PlayMode == 1) {
Sync1(); //Play sequence forwards
}

else if((data == midi_clock) && (play_flag == 1) && PlayMode == 2) {
Sync2(); //Play sequence backwards
}
}
}


//-------------------------------------------------------------------------------------------
// Functions

void Sync1() { // play forwards 16 x 16th notes, repeat after the cycle is finshed

clock_step = clock_step+1;

Note1 = (96*1)/16-5;
Note2 = (96*2)/16-5+ShuffleDelay;
Note3 = (96*3)/16-5;
Note4 = (96*4)/16-5+ShuffleDelay;
Note5 = (96*5)/16-5;
Note6 = (96*6)/16-5+ShuffleDelay;
Note7 = (96*7)/16-5;
Note8 = (96*8)/16-5+ShuffleDelay;
Note9 = (96*9)/16-5;
Note10 = (96*10)/16-5+ShuffleDelay;
Note11 = (96*11)/16-5;
Note12 = (96*12)/16-5+ShuffleDelay;
Note13 = (96*13)/16-5;
Note14 = (96*14)/16-5+ShuffleDelay;
Note15 = (96*15)/16-5;
Note16 = (96*16)/16-5+ShuffleDelay;


if (clock_step==Note1){
PlayNote1();
digitalWrite(22, HIGH);
digitalWrite(52, LOW);
}


if (clock_step==Note2){
PlayNote2();
digitalWrite(24, HIGH);
digitalWrite(22, LOW);
}


if (clock_step==Note3){
PlayNote3();
digitalWrite(26, HIGH);
digitalWrite(24, LOW);
}


if (clock_step==Note4){
PlayNote4();
digitalWrite(28, HIGH);
digitalWrite(26, LOW);
}


if (clock_step==Note5){
PlayNote5();
digitalWrite(30, HIGH);
digitalWrite(28, LOW);
}


if (clock_step==Note6){
PlayNote6();
digitalWrite(32, HIGH);
digitalWrite(30, LOW);
}


if (clock_step==Note7){
PlayNote7();
digitalWrite(34, HIGH);
digitalWrite(32, LOW);
}


if (clock_step==Note8){
PlayNote8();
digitalWrite(36, HIGH);
digitalWrite(34, LOW);
}


if (clock_step==Note9){
PlayNote9();
digitalWrite(38, HIGH);
digitalWrite(36, LOW);
}


if (clock_step==Note10){
PlayNote10();
digitalWrite(40, HIGH);
digitalWrite(38, LOW);
}


if (clock_step==Note11){
PlayNote11();
digitalWrite(42, HIGH);
digitalWrite(40, LOW);
}


if (clock_step==Note12){
PlayNote12();
digitalWrite(44, HIGH);
digitalWrite(42, LOW);
}


if (clock_step==Note13){
PlayNote13();
digitalWrite(46, HIGH);
digitalWrite(44, LOW);
}


if (clock_step==Note14){
PlayNote14();
digitalWrite(48, HIGH);
digitalWrite(46, LOW);
}


if (clock_step==Note15){
PlayNote15();
digitalWrite(50, HIGH);
digitalWrite(48, LOW);
}


if (clock_step==Note16){
PlayNote16();
digitalWrite(52, HIGH);
digitalWrite(50, LOW);
}


else if (clock_step==SeqLength){
clock_step=0;
}
}

void Sync2() { // play backwards 16 x 16th notes, repeat after the cycle is finshed,
clock_step = clock_step+1;

Note1 = (96*1)/16-5;
Note2 = (96*2)/16-5+ShuffleDelay;
Note3 = (96*3)/16-5;
Note4 = (96*4)/16-5+ShuffleDelay;
Note5 = (96*5)/16-5;
Note6 = (96*6)/16-5+ShuffleDelay;
Note7 = (96*7)/16-5;
Note8 = (96*8)/16-5+ShuffleDelay;
Note9 = (96*9)/16-5;
Note10 = (96*10)/16-5+ShuffleDelay;
Note11 = (96*11)/16-5;
Note12 = (96*12)/16-5+ShuffleDelay;
Note13 = (96*13)/16-5;
Note14 = (96*14)/16-5+ShuffleDelay;
Note15 = (96*15)/16-5;
Note16 = (96*16)/16-5+ShuffleDelay;

if (clock_step==Note1){
PlayNote16();
digitalWrite(52, HIGH);
digitalWrite(22, LOW);
}

if (clock_step==Note2){
PlayNote15();
digitalWrite(50, HIGH);
digitalWrite(52, LOW);
}

if (clock_step==Note3){
PlayNote14();
digitalWrite(48, HIGH);
digitalWrite(50, LOW);
}


if (clock_step==Note4){
PlayNote13();
digitalWrite(46, HIGH);
digitalWrite(48, LOW);
}


if (clock_step==Note5){
PlayNote12();
digitalWrite(44, HIGH);
digitalWrite(46, LOW);
}


if (clock_step==Note6){
PlayNote11();
digitalWrite(42, HIGH);
digitalWrite(44, LOW);
}


if (clock_step==Note7){
PlayNote10();
digitalWrite(40, HIGH);
digitalWrite(42, LOW);
}


if (clock_step==Note8){
PlayNote9();
digitalWrite(38, HIGH);
digitalWrite(40, LOW);
}


if (clock_step==Note9){
PlayNote8();
digitalWrite(36, HIGH);
digitalWrite(38, LOW);
}


if (clock_step==Note10){
PlayNote7();
digitalWrite(34, HIGH);
digitalWrite(36, LOW);
}


if (clock_step==Note11){
PlayNote6();
digitalWrite(32, HIGH);
digitalWrite(34, LOW);
}


if (clock_step==Note12){
PlayNote5();
digitalWrite(30, HIGH);
digitalWrite(32, LOW);
}


if (clock_step==Note13){
PlayNote4();
digitalWrite(28, HIGH);
digitalWrite(30, LOW);
}


if (clock_step==Note14){
PlayNote3();
digitalWrite(26, HIGH);
digitalWrite(28, LOW);
}


if (clock_step==Note15){
PlayNote2();
digitalWrite(24, HIGH);
digitalWrite(26, LOW);
}


if (clock_step==Note16){
PlayNote1();
digitalWrite(22, HIGH);
digitalWrite(24, LOW);
}


else if (clock_step==SeqLength){
clock_step=0;
}
}


void sendMidiNote (byte midiCommand, byte noteValue, byte velocityValue){
Serial.print(midiCommand, BYTE);
Serial.print(noteValue, BYTE);
Serial.print(velocityValue, BYTE);
}


void PlayNote1() {
if (analogRead(0) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote2() {

if (analogRead(1) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(1);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}


void PlayNote3() {
if (analogRead(2) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(2);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote4() {
if (analogRead(3) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(3);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote5() {
if (analogRead(4) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(4);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote6() {
if (analogRead(5) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(5);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote7() {
if (analogRead(6) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(6);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote8() {
if (analogRead(7) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(7);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote9() {
if (analogRead(8) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(8);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}


void PlayNote10() {
if (analogRead(9) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(9);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote11() {
if (analogRead(10) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(10);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote12() {
if (analogRead(11) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(11);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote13() {
if (analogRead(12) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(12);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote14() {
if (analogRead(13) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(13);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote15() {
if (analogRead(14) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(14);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote16() {
if (analogRead(15) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(15);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote) + transpose;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

2011-02-21

7) Holding Notes

Now notes are held until the next note starts, for poti values unter 128 (= 1/8th of the parameter range).


// Declaration of Varialbes

byte midi_start = 0xfa;
byte midi_stop = 0xfc;
byte midi_clock = 0xf8;
byte midi_continue = 0xfb;
int play_flag = 0;
byte data;
int clock_step=0;
int note = 0x3F;
int noteval = 0;
int LowestNote=36;
int HighestNote=36+3*12; //3 Octaves over LowestNote

int Note1 = (96*1)/16-5;
int Note2 = (96*2)/16-5;
int Note3 = (96*3)/16-5;
int Note4 = (96*4)/16-5;
int Note5 = (96*5)/16-5;
int Note6 = (96*6)/16-5;
int Note7 = (96*7)/16-5;
int Note8 = (96*8)/16-5;
int Note9 = (96*9)/16-5;
int Note10 = (96*10)/16-5;
int Note11 = (96*11)/16-5;
int Note12 = (96*12)/16-5;
int Note13 = (96*13)/16-5;
int Note14 = (96*14)/16-5;
int Note15 = (96*15)/16-5;
int Note16 = (96*16)/16-5;

int SeqLength = 6*16; //Sequence length is 16*16th notes

int ShufflePin = 2;
int Shuffle_Flag = 0;
int ShuffleDelay = 0;

int Seq1_Flag = HIGH;
int Seq2_Flag = LOW;
int Seq1Pin = 3;
int Seq2Pin = 4;
int PlayMode = 1;

int HoldNote_Flag = HIGH;


// Initialization

void setup() {

Serial.begin(31250);

pinMode(ShufflePin, INPUT); //Pin 0 for shuffle on/off as input

pinMode(22, OUTPUT);
pinMode(24, OUTPUT);
pinMode(26, OUTPUT);
pinMode(28, OUTPUT);
pinMode(30, OUTPUT);
pinMode(32, OUTPUT);
pinMode(34, OUTPUT);
pinMode(36, OUTPUT);
pinMode(38, OUTPUT);
pinMode(40, OUTPUT);
pinMode(42, OUTPUT);
pinMode(44, OUTPUT);
pinMode(46, OUTPUT);
pinMode(48, OUTPUT);
pinMode(50, OUTPUT);
pinMode(52, OUTPUT);

pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);

digitalWrite(22, HIGH);
digitalWrite(24, LOW);
digitalWrite(26, LOW);
digitalWrite(28, LOW);
digitalWrite(30, LOW);
digitalWrite(32, LOW);
digitalWrite(34, LOW);
digitalWrite(36, LOW);
digitalWrite(38, LOW);
digitalWrite(40, LOW);
digitalWrite(42, LOW);
digitalWrite(44, LOW);
digitalWrite(46, LOW);
digitalWrite(48, LOW);
digitalWrite(50, LOW);
digitalWrite(52, LOW);
}


// Main Programm

void loop() {

Shuffle_Flag = digitalRead(ShufflePin);

if(Shuffle_Flag == LOW) {
ShuffleDelay = 0;
}

if (Shuffle_Flag == HIGH){

ShuffleDelay = 2; // start every 2nd note two MIDI clock signals later (ShuffleDelay = 1...4)

}


Seq1_Flag = digitalRead(Seq1Pin); //Play sequence forwards
Seq2_Flag = digitalRead(Seq2Pin); //Play sequence backwards

if(Seq1_Flag == HIGH && Seq2_Flag == LOW) {
PlayMode = 1; //Play sequence forwards
}

if (Seq1_Flag == LOW && Seq2_Flag == HIGH){
PlayMode = 2; //Play sequence backwards
}



if(Serial.available() > 0) {
data = Serial.read();
if(data == midi_start) {
play_flag = 1;
clock_step=0;
}
else if(data == midi_continue) {
play_flag = 1;
}

else if(data == midi_stop) {
play_flag = 0;
clock_step=0;
sendMidiNote (0x80, note, 0x7F); //last note off
digitalWrite(22, HIGH);
digitalWrite(24, LOW);
digitalWrite(26, LOW);
digitalWrite(28, LOW);
digitalWrite(30, LOW);
digitalWrite(32, LOW);
digitalWrite(34, LOW);
digitalWrite(36, LOW);
digitalWrite(38, LOW);
digitalWrite(40, LOW);
digitalWrite(42, LOW);
digitalWrite(44, LOW);
digitalWrite(46, LOW);
digitalWrite(48, LOW);
digitalWrite(50, LOW);
digitalWrite(52, LOW);
}
else if((data == midi_clock) && (play_flag == 1) && PlayMode == 1) {
Sync1(); //Play sequence forwards
}

else if((data == midi_clock) && (play_flag == 1) && PlayMode == 2) {
Sync2(); //Play sequence backwards
}
}
}


// Functions


void Sync1() { // play forwards 16 x 16th notes, repeat after the cycle is finshed

clock_step = clock_step+1;

Note1 = (96*1)/16-5;
Note2 = (96*2)/16-5+ShuffleDelay;
Note3 = (96*3)/16-5;
Note4 = (96*4)/16-5+ShuffleDelay;
Note5 = (96*5)/16-5;
Note6 = (96*6)/16-5+ShuffleDelay;
Note7 = (96*7)/16-5;
Note8 = (96*8)/16-5+ShuffleDelay;
Note9 = (96*9)/16-5;
Note10 = (96*10)/16-5+ShuffleDelay;
Note11 = (96*11)/16-5;
Note12 = (96*12)/16-5+ShuffleDelay;
Note13 = (96*13)/16-5;
Note14 = (96*14)/16-5+ShuffleDelay;
Note15 = (96*15)/16-5;
Note16 = (96*16)/16-5+ShuffleDelay;


if (clock_step==Note1){
PlayNote1();
digitalWrite(22, HIGH);
digitalWrite(52, LOW);
}


if (clock_step==Note2){
PlayNote2();
digitalWrite(24, HIGH);
digitalWrite(22, LOW);
}


if (clock_step==Note3){
PlayNote3();
digitalWrite(26, HIGH);
digitalWrite(24, LOW);
}


if (clock_step==Note4){
PlayNote4();
digitalWrite(28, HIGH);
digitalWrite(26, LOW);
}


if (clock_step==Note5){
PlayNote5();
digitalWrite(30, HIGH);
digitalWrite(28, LOW);
}


if (clock_step==Note6){
PlayNote6();
digitalWrite(32, HIGH);
digitalWrite(30, LOW);
}


if (clock_step==Note7){
PlayNote7();
digitalWrite(34, HIGH);
digitalWrite(32, LOW);
}


if (clock_step==Note8){
PlayNote8();
digitalWrite(36, HIGH);
digitalWrite(34, LOW);
}


if (clock_step==Note9){
PlayNote9();
digitalWrite(38, HIGH);
digitalWrite(36, LOW);
}


if (clock_step==Note10){
PlayNote10();
digitalWrite(40, HIGH);
digitalWrite(38, LOW);
}


if (clock_step==Note11){
PlayNote11();
digitalWrite(42, HIGH);
digitalWrite(40, LOW);
}


if (clock_step==Note12){
PlayNote12();
digitalWrite(44, HIGH);
digitalWrite(42, LOW);
}


if (clock_step==Note13){
PlayNote13();
digitalWrite(46, HIGH);
digitalWrite(44, LOW);
}


if (clock_step==Note14){
PlayNote14();
digitalWrite(48, HIGH);
digitalWrite(46, LOW);
}


if (clock_step==Note15){
PlayNote15();
digitalWrite(50, HIGH);
digitalWrite(48, LOW);
}


if (clock_step==Note16){
PlayNote16();
digitalWrite(52, HIGH);
digitalWrite(50, LOW);
}


else if (clock_step==SeqLength){
clock_step=0;
}
}

void Sync2() { // play backwards 16 x 16th notes, repeat after the cycle is finshed,
clock_step = clock_step+1;

Note1 = (96*1)/16-5;
Note2 = (96*2)/16-5+ShuffleDelay;
Note3 = (96*3)/16-5;
Note4 = (96*4)/16-5+ShuffleDelay;
Note5 = (96*5)/16-5;
Note6 = (96*6)/16-5+ShuffleDelay;
Note7 = (96*7)/16-5;
Note8 = (96*8)/16-5+ShuffleDelay;
Note9 = (96*9)/16-5;
Note10 = (96*10)/16-5+ShuffleDelay;
Note11 = (96*11)/16-5;
Note12 = (96*12)/16-5+ShuffleDelay;
Note13 = (96*13)/16-5;
Note14 = (96*14)/16-5+ShuffleDelay;
Note15 = (96*15)/16-5;
Note16 = (96*16)/16-5+ShuffleDelay;

if (clock_step==Note1){
PlayNote16();
digitalWrite(52, HIGH);
digitalWrite(22, LOW);
}

if (clock_step==Note2){
PlayNote15();
digitalWrite(50, HIGH);
digitalWrite(52, LOW);
}

if (clock_step==Note3){
PlayNote14();
digitalWrite(48, HIGH);
digitalWrite(50, LOW);
}


if (clock_step==Note4){
PlayNote13();
digitalWrite(46, HIGH);
digitalWrite(48, LOW);
}


if (clock_step==Note5){
PlayNote12();
digitalWrite(44, HIGH);
digitalWrite(46, LOW);
}


if (clock_step==Note6){
PlayNote11();
digitalWrite(42, HIGH);
digitalWrite(44, LOW);
}


if (clock_step==Note7){
PlayNote10();
digitalWrite(40, HIGH);
digitalWrite(42, LOW);
}


if (clock_step==Note8){
PlayNote9();
digitalWrite(38, HIGH);
digitalWrite(40, LOW);
}


if (clock_step==Note9){
PlayNote8();
digitalWrite(36, HIGH);
digitalWrite(38, LOW);
}


if (clock_step==Note10){
PlayNote7();
digitalWrite(34, HIGH);
digitalWrite(36, LOW);
}


if (clock_step==Note11){
PlayNote6();
digitalWrite(32, HIGH);
digitalWrite(34, LOW);
}


if (clock_step==Note12){
PlayNote5();
digitalWrite(30, HIGH);
digitalWrite(32, LOW);
}


if (clock_step==Note13){
PlayNote4();
digitalWrite(28, HIGH);
digitalWrite(30, LOW);
}


if (clock_step==Note14){
PlayNote3();
digitalWrite(26, HIGH);
digitalWrite(28, LOW);
}


if (clock_step==Note15){
PlayNote2();
digitalWrite(24, HIGH);
digitalWrite(26, LOW);
}


if (clock_step==Note16){
PlayNote1();
digitalWrite(22, HIGH);
digitalWrite(24, LOW);
}


else if (clock_step==SeqLength){
clock_step=0;
}
}


void sendMidiNote (byte midiCommand, byte noteValue, byte velocityValue){
Serial.print(midiCommand, BYTE);
Serial.print(noteValue, BYTE);
Serial.print(velocityValue, BYTE);
}


void PlayNote1() {
if (analogRead(0) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote2() {

if (analogRead(1) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(1);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}


void PlayNote3() {
if (analogRead(2) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(2);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote4() {
if (analogRead(3) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(3);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote5() {
if (analogRead(4) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(4);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote6() {
if (analogRead(5) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(5);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote7() {
if (analogRead(6) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(6);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote8() {
if (analogRead(7) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(7);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote9() {
if (analogRead(8) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(8);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}


void PlayNote10() {
if (analogRead(9) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(9);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote11() {
if (analogRead(10) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(10);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote12() {
if (analogRead(11) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(11);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote13() {
if (analogRead(12) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(12);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote14() {
if (analogRead(13) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(13);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote15() {
if (analogRead(14) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(14);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

void PlayNote16() {
if (analogRead(15) > 127 && HoldNote_Flag==HIGH) {
//sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(15);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
else if (analogRead(0) > 127 && HoldNote_Flag==LOW) {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
}

2010-12-15

6) Introducing variations in playing the sequence

Now we introduce the possibility to switch between forward and backward playing while the sequencer is running. The code is now also made clearer, by leaving unimportant parts away.

Parts list
-2 x Resistor 220 Ohm
-1 x Resistor 100 kOhm
-1 x Diode
-1 x opto coupler IC GNY17-2
-2 x 5-pole DIN female connector (180°)
-1 x MIDI cable
-1 x MIDI sync master (here: Roland TR-505)
-1 x MIDI sync slave (here: DIY synthesizer with MIDI-input)
-1 x Arduino (here: Arduino Mega. For this demo also Arduinos with less input and output channels will work)
-N x Potentiometer 10 kOhm for N steps
-Switch for turning shuffle on and off
-1 x Switch for 2 posibilities of playing the step order



Schematics
--> like before, but with a switch that connects either pin 3 to ground and pin 4 to 5V and wise versa, if the switch is switched. Use pull up resistors like in the examples in the Fritzing software.


Software


// Declaration of Varialbes
byte midi_start = 0xfa;
byte midi_stop = 0xfc;
byte midi_clock = 0xf8;
byte midi_continue = 0xfb;
int play_flag = 0;
byte data;
int clock_step=0;
int note = 0x3F;
int noteval = 0;
int LowestNote=36;
int HighestNote=36+3*12; //3 Octaves over LowestNote

int Note1 = (96*1)/16-5;
int Note2 = (96*2)/16-5;
int Note3 = (96*3)/16-5;
int Note4 = (96*4)/16-5;
int Note5 = (96*5)/16-5;
int Note6 = (96*6)/16-5;
int Note7 = (96*7)/16-5;
int Note8 = (96*8)/16-5;
int Note9 = (96*9)/16-5;
int Note10 = (96*10)/16-5;
int Note11 = (96*11)/16-5;
int Note12 = (96*12)/16-5;
int Note13 = (96*13)/16-5;
int Note14 = (96*14)/16-5;
int Note15 = (96*15)/16-5;
int Note16 = (96*16)/16-5;

int SeqLength = 6*16; //Sequence length is 16*16th notes

int ShufflePin = 2;
int Shuffle_Flag = 0;
int ShuffleDelay = 0;

int Seq1_Flag = HIGH;
int Seq2_Flag = LOW;
int Seq1Pin = 3;
int Seq2Pin = 4;
int PlayMode = 1;

// Initialization
void setup() {
Serial.begin(31250);

pinMode(ShufflePin, INPUT); //Pin 0 for shuffle on/off as input

pinMode(22, OUTPUT);
pinMode(24, OUTPUT);
pinMode(26, OUTPUT);
pinMode(28, OUTPUT);
pinMode(30, OUTPUT);
pinMode(32, OUTPUT);
pinMode(34, OUTPUT);
pinMode(36, OUTPUT);
pinMode(38, OUTPUT);
pinMode(40, OUTPUT);
pinMode(42, OUTPUT);
pinMode(44, OUTPUT);
pinMode(46, OUTPUT);
pinMode(48, OUTPUT);
pinMode(50, OUTPUT);
pinMode(52, OUTPUT);

digitalWrite(22, HIGH);
digitalWrite(24, LOW);
digitalWrite(26, LOW);
digitalWrite(28, LOW);
digitalWrite(30, LOW);
digitalWrite(32, LOW);
digitalWrite(34, LOW);
digitalWrite(36, LOW);
digitalWrite(38, LOW);
digitalWrite(40, LOW);
digitalWrite(42, LOW);
digitalWrite(44, LOW);
digitalWrite(46, LOW);
digitalWrite(48, LOW);
digitalWrite(50, LOW);
digitalWrite(52, LOW);
}

// Main Programm
void loop() {

Shuffle_Flag = digitalRead(ShufflePin);

if(Shuffle_Flag == LOW) {
ShuffleDelay = 0;
}
if (Shuffle_Flag == HIGH){
ShuffleDelay = 2; // start every 2nd note two MIDI clock signals later (ShuffleDelay = 1...4)
}


Seq1_Flag = digitalRead(Seq1Pin); //Play sequence forwards
Seq2_Flag = digitalRead(Seq2Pin); //Play sequence backwards

if(Seq1_Flag == HIGH && Seq2_Flag == LOW) {
PlayMode = 1; //Play sequence forwards
}
if (Seq1_Flag == LOW && Seq2_Flag == HIGH){
PlayMode = 2; //Play sequence backwards
}


if(Serial.available() > 0) {
data = Serial.read();
if(data == midi_start) {
play_flag = 1;
clock_step=0;
}
else if(data == midi_continue) {
play_flag = 1;
}
else if(data == midi_stop) {
play_flag = 0;
clock_step=0;
sendMidiNote (0x80, note, 0x7F); //last note off
digitalWrite(22, HIGH);
digitalWrite(24, LOW);
digitalWrite(26, LOW);
digitalWrite(28, LOW);
digitalWrite(30, LOW);
digitalWrite(32, LOW);
digitalWrite(34, LOW);
digitalWrite(36, LOW);
digitalWrite(38, LOW);
digitalWrite(40, LOW);
digitalWrite(42, LOW);
digitalWrite(44, LOW);
digitalWrite(46, LOW);
digitalWrite(48, LOW);
digitalWrite(50, LOW);
digitalWrite(52, LOW);
}
else if((data == midi_clock) && (play_flag == 1) && PlayMode == 1) {
Sync1(); //Play sequence forwards
}
else if((data == midi_clock) && (play_flag == 1) && PlayMode == 2) {
Sync2(); //Play sequence backwards
}

}
}


// Functions

void Sync1() { // play forwards 16 x 16th notes, repeat after the cycle is finshed
clock_step = clock_step+1;

Note1 = (96*1)/16-5;
Note2 = (96*2)/16-5+ShuffleDelay;
Note3 = (96*3)/16-5;
Note4 = (96*4)/16-5+ShuffleDelay;
Note5 = (96*5)/16-5;
Note6 = (96*6)/16-5+ShuffleDelay;
Note7 = (96*7)/16-5;
Note8 = (96*8)/16-5+ShuffleDelay;
Note9 = (96*9)/16-5;
Note10 = (96*10)/16-5+ShuffleDelay;
Note11 = (96*11)/16-5;
Note12 = (96*12)/16-5+ShuffleDelay;
Note13 = (96*13)/16-5;
Note14 = (96*14)/16-5+ShuffleDelay;
Note15 = (96*15)/16-5;
Note16 = (96*16)/16-5+ShuffleDelay;

if (clock_step==Note1){
PlayNote1();
digitalWrite(22, HIGH);
digitalWrite(52, LOW);
}

if (clock_step==Note2){
PlayNote2();
digitalWrite(24, HIGH);
digitalWrite(22, LOW);
}

if (clock_step==Note3){
PlayNote3();
digitalWrite(26, HIGH);
digitalWrite(24, LOW);
}

if (clock_step==Note4){
PlayNote4();
digitalWrite(28, HIGH);
digitalWrite(26, LOW);
}

if (clock_step==Note5){
PlayNote5();
digitalWrite(30, HIGH);
digitalWrite(28, LOW);
}

if (clock_step==Note6){
PlayNote6();
digitalWrite(32, HIGH);
digitalWrite(30, LOW);
}

if (clock_step==Note7){
PlayNote7();
digitalWrite(34, HIGH);
digitalWrite(32, LOW);
}

if (clock_step==Note8){
PlayNote8();
digitalWrite(36, HIGH);
digitalWrite(34, LOW);
}

if (clock_step==Note9){
PlayNote9();
digitalWrite(38, HIGH);
digitalWrite(36, LOW);
}

if (clock_step==Note10){
PlayNote10();
digitalWrite(40, HIGH);
digitalWrite(38, LOW);
}

if (clock_step==Note11){
PlayNote11();
digitalWrite(42, HIGH);
digitalWrite(40, LOW);
}

if (clock_step==Note12){
PlayNote12();
digitalWrite(44, HIGH);
digitalWrite(42, LOW);
}

if (clock_step==Note13){
PlayNote13();
digitalWrite(46, HIGH);
digitalWrite(44, LOW);
}

if (clock_step==Note14){
PlayNote14();
digitalWrite(48, HIGH);
digitalWrite(46, LOW);
}

if (clock_step==Note15){
PlayNote15();
digitalWrite(50, HIGH);
digitalWrite(48, LOW);
}

if (clock_step==Note16){
PlayNote16();
digitalWrite(52, HIGH);
digitalWrite(50, LOW);
}

else if (clock_step==SeqLength){
clock_step=0;
}
}


void Sync2() { // play backwards 16 x 16th notes, repeat after the cycle is finshed,
clock_step = clock_step+1;

Note1 = (96*1)/16-5;
Note2 = (96*2)/16-5+ShuffleDelay;
Note3 = (96*3)/16-5;
Note4 = (96*4)/16-5+ShuffleDelay;
Note5 = (96*5)/16-5;
Note6 = (96*6)/16-5+ShuffleDelay;
Note7 = (96*7)/16-5;
Note8 = (96*8)/16-5+ShuffleDelay;
Note9 = (96*9)/16-5;
Note10 = (96*10)/16-5+ShuffleDelay;
Note11 = (96*11)/16-5;
Note12 = (96*12)/16-5+ShuffleDelay;
Note13 = (96*13)/16-5;
Note14 = (96*14)/16-5+ShuffleDelay;
Note15 = (96*15)/16-5;
Note16 = (96*16)/16-5+ShuffleDelay;

if (clock_step==Note1){
PlayNote16();
digitalWrite(52, HIGH);
digitalWrite(22, LOW);
}

if (clock_step==Note2){
PlayNote15();
digitalWrite(50, HIGH);
digitalWrite(52, LOW);
}

if (clock_step==Note3){
PlayNote14();
digitalWrite(48, HIGH);
digitalWrite(50, LOW);
}

if (clock_step==Note4){
PlayNote13();
digitalWrite(46, HIGH);
digitalWrite(48, LOW);
}

if (clock_step==Note5){
PlayNote12();
digitalWrite(44, HIGH);
digitalWrite(46, LOW);
}

if (clock_step==Note6){
PlayNote11();
digitalWrite(42, HIGH);
digitalWrite(44, LOW);
}

if (clock_step==Note7){
PlayNote10();
digitalWrite(40, HIGH);
digitalWrite(42, LOW);
}

if (clock_step==Note8){
PlayNote9();
digitalWrite(38, HIGH);
digitalWrite(40, LOW);
}

if (clock_step==Note9){
PlayNote8();
digitalWrite(36, HIGH);
digitalWrite(38, LOW);
}

if (clock_step==Note10){
PlayNote7();
digitalWrite(34, HIGH);
digitalWrite(36, LOW);
}

if (clock_step==Note11){
PlayNote6();
digitalWrite(32, HIGH);
digitalWrite(34, LOW);
}

if (clock_step==Note12){
PlayNote5();
digitalWrite(30, HIGH);
digitalWrite(32, LOW);
}

if (clock_step==Note13){
PlayNote4();
digitalWrite(28, HIGH);
digitalWrite(30, LOW);
}

if (clock_step==Note14){
PlayNote3();
digitalWrite(26, HIGH);
digitalWrite(28, LOW);
}

if (clock_step==Note15){
PlayNote2();
digitalWrite(24, HIGH);
digitalWrite(26, LOW);
}

if (clock_step==Note16){
PlayNote1();
digitalWrite(22, HIGH);
digitalWrite(24, LOW);
}

else if (clock_step==SeqLength){
clock_step=0;
}
}



void sendMidiNote (byte midiCommand, byte noteValue, byte velocityValue){
Serial.print(midiCommand, BYTE);
Serial.print(noteValue, BYTE);
Serial.print(velocityValue, BYTE);
}

void PlayNote1() {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on

}

void PlayNote2() {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(1);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

void PlayNote3() {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(2);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

void PlayNote4() {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(3);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

void PlayNote5() {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(4);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

void PlayNote6() {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(5);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

void PlayNote7() {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(6);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

void PlayNote8() {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(7);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

void PlayNote9() {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(8);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

void PlayNote10() {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(9);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

void PlayNote11() {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(10);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

void PlayNote12() {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(11);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

void PlayNote13() {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(12);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

void PlayNote14() {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(13);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

void PlayNote15() {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(14);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

void PlayNote16() {
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(15);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

2010-11-19

5) One LED lights up at each step

Up to now everything works and
in this step we want to light up a LED at each step.We asign one digital output for each step and connect a LED to it. Since only one LED will light at a time, we need only one resistor to limit the current for all LEDs.





Parts list
-2 x Resistor 220 Ohm
-1 x Resistor 100 kOhm
-1 x Diode
-1 x opto coupler IC GNY17-2
-2 x 5-pole DIN female connector (180°)
-1 x MIDI cable
-1 x MIDI sync master (here: Roland TR-505)
-1 x MIDI sync slave (here: DIY synthesizer with MIDI-input)
-1 x Arduino (here: Arduino Mega. For this demo also Arduinos with less input and output channels will work)
-N x Potentiometer 10 kOhm
-N x Switch for muting N steps
-N x LEDs for N steps
-1 x Resistor for the LEDs
-Switch for turning shuffle on and off
-10 kOhm pull-down Resistor for the switch

Schematics















Software (tested and working)




// Declaration of Varialbes
byte midi_start = 0xfa;
byte midi_stop = 0xfc;
byte midi_clock = 0xf8;
byte midi_continue = 0xfb;
int play_flag = 0;
byte data;
int clock_step;
int note;
int noteval;
int LowestNote;
int HighestNote;

int Note1;
int Note2;
int Note3;
int Note4;
int Note5;
int Note6;
int Note7;
int Note8;
int Note9;
int Note10;
int Note11;
int Note12;
int Note13;
int Note14;
int Note15;
int Note16;

int SeqLength;

int ShufflePin;
int Shuffle_Flag;
int ShuffleDelay;

// Initialization
void setup() {
Serial.begin(31250);
clock_step=0;
note = 0x3F;
noteval = 0;
LowestNote=36;
HighestNote=36+3*12; //3 Octaves over LowestNote

ShufflePin = 2;
pinMode(ShufflePin, INPUT); //Pin 0 for shuffle on/off as input
Shuffle_Flag = 0;
ShuffleDelay = 0;

Note1 = (96*1)/16-5;
Note2 = (96*2)/16-5;
Note3 = (96*3)/16-5;
Note4 = (96*4)/16-5;
Note5 = (96*5)/16-5;
Note6 = (96*6)/16-5;
Note7 = (96*7)/16-5;
Note8 = (96*8)/16-5;
Note9 = (96*9)/16-5;
Note10 = (96*10)/16-5;
Note11 = (96*11)/16-5;
Note12 = (96*12)/16-5;
Note13 = (96*13)/16-5;
Note14 = (96*14)/16-5;
Note15 = (96*15)/16-5;
Note16 = (96*16)/16-5;

SeqLength = 6*16; //Sequence length is 16*16th notes

pinMode(22, OUTPUT);
pinMode(24, OUTPUT);
pinMode(26, OUTPUT);
pinMode(28, OUTPUT);
pinMode(30, OUTPUT);
pinMode(32, OUTPUT);
pinMode(34, OUTPUT);
pinMode(36, OUTPUT);
pinMode(38, OUTPUT);
pinMode(40, OUTPUT);
pinMode(42, OUTPUT);
pinMode(44, OUTPUT);
pinMode(46, OUTPUT);
pinMode(48, OUTPUT);
pinMode(50, OUTPUT);
pinMode(52, OUTPUT);

digitalWrite(22, HIGH);
digitalWrite(24, LOW);
digitalWrite(26, LOW);
digitalWrite(28, LOW);
digitalWrite(30, LOW);
digitalWrite(32, LOW);
digitalWrite(34, LOW);
digitalWrite(36, LOW);
digitalWrite(38, LOW);
digitalWrite(40, LOW);
digitalWrite(42, LOW);
digitalWrite(44, LOW);
digitalWrite(46, LOW);
digitalWrite(48, LOW);
digitalWrite(50, LOW);
digitalWrite(52, LOW);
}

// Main Programm
void loop() {

Shuffle_Flag = digitalRead(ShufflePin);

if(Shuffle_Flag == LOW) {
ShuffleDelay = 0;
}
if (Shuffle_Flag == HIGH){
ShuffleDelay = 2; // start every 2nd note two MIDI clock signals later
}

if(Serial.available() > 0) {
data = Serial.read();
if(data == midi_start) {
play_flag = 1;
clock_step=0;
}
else if(data == midi_continue) {
play_flag = 1;
}
else if(data == midi_stop) {
play_flag = 0;
clock_step=0;
sendMidiNote (0x80, note, 0x7F); //last note off
digitalWrite(22, HIGH);
digitalWrite(24, LOW);
digitalWrite(26, LOW);
digitalWrite(28, LOW);
digitalWrite(30, LOW);
digitalWrite(32, LOW);
digitalWrite(34, LOW);
digitalWrite(36, LOW);
digitalWrite(38, LOW);
digitalWrite(40, LOW);
digitalWrite(42, LOW);
digitalWrite(44, LOW);
digitalWrite(46, LOW);
digitalWrite(48, LOW);
digitalWrite(50, LOW);
digitalWrite(52, LOW);
}
else if((data == midi_clock) && (play_flag == 1)) {
Sync();
}
}
}


// Functions

void Sync() { // play 16 x 16th notes, repeat after the cycle is finshed
clock_step = clock_step+1;

Note1 = (96*1)/16-5;
Note2 = (96*2)/16-5+ShuffleDelay;
Note3 = (96*3)/16-5;
Note4 = (96*4)/16-5+ShuffleDelay;
Note5 = (96*5)/16-5;
Note6 = (96*6)/16-5+ShuffleDelay;
Note7 = (96*7)/16-5;
Note8 = (96*8)/16-5+ShuffleDelay;
Note9 = (96*9)/16-5;
Note10 = (96*10)/16-5+ShuffleDelay;
Note11 = (96*11)/16-5;
Note12 = (96*12)/16-5+ShuffleDelay;
Note13 = (96*13)/16-5;
Note14 = (96*14)/16-5+ShuffleDelay;
Note15 = (96*15)/16-5;
Note16 = (96*16)/16-5+ShuffleDelay;

if (clock_step==Note1){
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
digitalWrite(22, HIGH);
digitalWrite(52, LOW);
}

if (clock_step==Note2){
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(1);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
digitalWrite(24, HIGH);
digitalWrite(22, LOW);
}

if (clock_step==Note3){
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(2);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
digitalWrite(26, HIGH);
digitalWrite(24, LOW);
}

if (clock_step==Note4){
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(3);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
digitalWrite(28, HIGH);
digitalWrite(26, LOW);
}

if (clock_step==Note5){
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(4);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
digitalWrite(30, HIGH);
digitalWrite(28, LOW);
}

if (clock_step==Note6){
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(5);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
digitalWrite(32, HIGH);
digitalWrite(30, LOW);
}

if (clock_step==Note7){
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(6);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
digitalWrite(34, HIGH);
digitalWrite(32, LOW);
}

if (clock_step==Note8){
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(7);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
digitalWrite(36, HIGH);
digitalWrite(34, LOW);
}

if (clock_step==Note9){
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(8);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
digitalWrite(38, HIGH);
digitalWrite(36, LOW);
}

if (clock_step==Note10){
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(9);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
digitalWrite(40, HIGH);
digitalWrite(38, LOW);
}

if (clock_step==Note11){
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(10);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
digitalWrite(42, HIGH);
digitalWrite(40, LOW);
}

if (clock_step==Note12){
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(11);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
digitalWrite(44, HIGH);
digitalWrite(42, LOW);
}

if (clock_step==Note13){
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(12);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
digitalWrite(46, HIGH);
digitalWrite(44, LOW);
}

if (clock_step==Note14){
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(13);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
digitalWrite(48, HIGH);
digitalWrite(46, LOW);
}

if (clock_step==Note15){
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(14);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
digitalWrite(50, HIGH);
digitalWrite(48, LOW);
}

if (clock_step==Note16){
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(15);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
digitalWrite(52, HIGH);
digitalWrite(50, LOW);
}

else if (clock_step==SeqLength){
clock_step=0;
}
}

void sendMidiNote (byte midiCommand, byte noteValue, byte velocityValue){
Serial.print(midiCommand, BYTE);
Serial.print(noteValue, BYTE);
Serial.print(velocityValue, BYTE);
}

2010-11-15

4) Implementation of a shuffle rhythm function

Now we will implement a shuffle function to our Arduino Mega based step sequencer. A hardware switch will apply a HIGH signal to a digital input (ShufflePin is Pin 2) and to turn the shuffle on, where against a LOW signal at this pin means shuffle off. The rest is done by the software.


Parts list
-2 x Resistor 220 Ohm
-1 x Resistor 100 kOhm
-1 x Diode
-1 x opto coupler IC GNY17-2
-2 x 5-pole DIN fe
male connector (180°)
-1 x MIDI cable
-1 x MIDI sync master (here: Roland TR-505)
-1 x MIDI sync slave (here: DIY synthesizer with MIDI-input)
-1 x Arduino (here: Arduino Mega. For this demo also Arduinos with less input and output channels will work)
-N x Potentiometer 10 kOhm

-Switch for turning shuffle on and off
-10 kOhm
pull-down Resistor for the switch


Schematics



















Software

If the readout at the ShufflePin (pin 2) is LOW then the Shuffle_Flag=0 causing the ShuffleDelay to be 0. Then the sequencer runs as in the chapters before this one. However if the ShufflePin is set to HIGH by closing the respective switch, Shuffle_Flag will be 1. This means that every second 16th note will be delayed by 1 MIDI clock signals. The result is a shuffle rhythm. The effect can be increased, e.g. by setting ShuffleDelay = 2...4.


// Declaration of Varialbes
byte midi_start = 0xfa;
byte midi_stop = 0xfc;
byte midi_clock = 0xf8;
byte midi_continue = 0xfb;
int play_flag = 0;
byte data;
int clock_step;
int note;
int noteval;
int LowestNote;
int HighestNote;

int Note1;
int Note2;
int Note3;
int Note4;
int Note5;
int Note6;
int Note7;
int Note8;
int Note9;
int Note10;
int Note11;
int Note12;
int Note13;
int Note14;
int Note15;
int Note16;

int SeqLength;

int ShufflePin;
int Shuffle_Flag;
int ShuffleDelay;

// Initialization
void setup() {
Serial.begin(31250);
clock_step=0;
note = 0x3F;
noteval = 0;
LowestNote=36;
HighestNote=36+3*12; //3 Octaves over LowestNote

ShufflePin = 2;
pinMode(ShufflePin, INPUT); //Pin 0 for shuffle on/off as input
Shuffle_Flag = 0;
ShuffleDelay = 0;

Note1 = (96*1)/16-5;
Note2 = (96*2)/16-5;
Note3 = (96*3)/16-5;
Note4 = (96*4)/16-5;
Note5 = (96*5)/16-5;
Note6 = (96*6)/16-5;
Note7 = (96*7)/16-5;
Note8 = (96*8)/16-5;
Note9 = (96*9)/16-5;
Note10 = (96*10)/16-5;
Note11 = (96*11)/16-5;
Note12 = (96*12)/16-5;
Note13 = (96*13)/16-5;
Note14 = (96*14)/16-5;
Note15 = (96*15)/16-5;
Note16 = (96*16)/16-5;

SeqLength = 6*16; //Sequence length is 16*16th notes
}

// Main Programm
void loop() {

Shuffle_Flag = digitalRead(ShufflePin);

if(Shuffle_Flag == LOW) {
ShuffleDelay = 0;
}
if (Shuffle_Flag == HIGH){
ShuffleDelay = 2; // start every 2nd note two MIDI clock signals later
}

if(Serial.available() > 0) {
data = Serial.read();
if(data == midi_start) {
play_flag = 1;
clock_step=0;
}
else if(data == midi_continue) {
play_flag = 1;
}
else if(data == midi_stop) {
play_flag = 0;
clock_step=0;
sendMidiNote (0x80, note, 0x7F); //last note off
}
else if((data == midi_clock) && (play_flag == 1)) {
Sync();
}
}
}


// Functions

void Sync() { // play 8 fixed 16th notes, repeat after the cycle is finshed
clock_step = clock_step+1;

Note1 = (96*1)/16-5;
Note2 = (96*2)/16-5+ShuffleDelay;
Note3 = (96*3)/16-5;
Note4 = (96*4)/16-5+ShuffleDelay;
Note5 = (96*5)/16-5;
Note6 = (96*6)/16-5+ShuffleDelay;
Note7 = (96*7)/16-5;
Note8 = (96*8)/16-5+ShuffleDelay;
Note9 = (96*9)/16-5;
Note10 = (96*10)/16-5+ShuffleDelay;
Note11 = (96*11)/16-5;
Note12 = (96*12)/16-5+ShuffleDelay;
Note13 = (96*13)/16-5;
Note14 = (96*14)/16-5+ShuffleDelay;
Note15 = (96*15)/16-5;
Note16 = (96*16)/16-5+ShuffleDelay;

if (clock_step==Note1){ //1st step
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(0);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

if (clock_step==Note2){ //2nd step
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(1);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

if (clock_step==Note3){ //3nd step
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(2);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

if (clock_step==Note4){ //4nd step
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(3);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

if (clock_step==Note5){ //4nd step
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(4);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

if (clock_step==Note6){ //4nd step
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(5);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

if (clock_step==Note7){ //4nd step
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(6);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

if (clock_step==Note8){ //4nd step
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(7);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

if (clock_step==Note9){ //4nd step
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(8);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

if (clock_step==Note10){ //4nd step
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(9);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

if (clock_step==Note11){ //4nd step
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(10);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

if (clock_step==Note12){ //4nd step
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(11);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

if (clock_step==Note13){ //4nd step
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(12);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

if (clock_step==Note14){ //4nd step
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(13);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

if (clock_step==Note15){ //4nd step
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(14);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

if (clock_step==Note16){ //4nd step
sendMidiNote (0x80, note, 0x7F); //last note off
noteval = analogRead(15);
note = map(noteval, 0, 1023, LowestNote, HighestNote);
sendMidiNote (0x90, note, 0x7F); //note of this step on
}

else if (clock_step==SeqLength){
clock_step=0;
}
}

void sendMidiNote (byte midiCommand, byte noteValue, byte velocityValue){
Serial.print(midiCommand, BYTE);
Serial.print(noteValue, BYTE);
Serial.print(velocityValue, BYTE);
}