#include <SoftwareSerial.h>
#include <TimerOne.h>
int mval;
const int onecount = 52;
const int othercount = 53;
int metronome = 130;
int delaycount = 0;
int metVal = 120;
int cmdnum = 0;
int CMDpotPin = 0;
int StateCMDpotPin = 1;
int METpotPin = A2;
int Stateval = 0;
int CMDval = 0; // variable to store the value coming from the sensor
const byte anaIn = A0;
const byte anaStateIn = A1;
const byte anaMetronomeIn = A2;
int snum = 1;
SoftwareSerial mySerial(10, 11);
int btnnum = 22;
int buttonPin;
int allbuttonNum = 18;
int x;
int bynum;
byte TempCMDToChange;
boolean currentStates[18];//stroage for current measured button state
boolean lastStates[18] ;//storage for last measured button state
boolean debouncedStates[18] ;//debounced button state
int debounceInterval[18];//wait 20 ms for button pin to settle
unsigned long timeOfLastButtonEvents[18];//store the last time the button state changed
int PinToChange;
int StateToChange;
int nextbyte = 0;
int BounceItInterval = 5;
String fromsoftware;
char* message = "";
char* TempStatemessage = "";
char* whencmds[3] = {
"Push", "Held", "Released"
};
char* cmds[8] = {
"Note On", "Note Off", "After Touch", "Continuous Controller", "Patch Change", "Channel Pressure", "Pitch Bend", "Non-Musical Commands"
};
char firstchar;
int StateValue = 0;
int PadToChange;
char PadValue;
byte PushCMDToChange ;
byte RelCMDToChange;
byte HeldCMDToChange;
int CMDValue = 1;
byte NoteToChange;
int NoteValue = 1;
byte VelToChange;
int VelValue = 1;
char TrackToChange;
int TrackValue = 1;
char BarsToChange;
int BarsValue = 1;
char LengthToChange;
int LengthValue = 1;
char ActBarToChange;
int ActBarValue = 1;
char BankToChange;
int BankValue = 1;
int val = 0;
int padpin = 22;
int otherbtnpin = 38;
int banknum = 0;
int stillHIGH;
char thispadpin;
int SelectedPad = 1;
int mnum = 0;
void setup() {
Timer1.initialize(1000000); // set a timer of length 100000 microseconds (or 0.1 sec - or 10Hz => the led will blink 5 times, 5 cycles of on-and-off, per second)
Timer1.attachInterrupt( RealMetronome ); // attach the service routine here
pinMode(onecount, OUTPUT);
pinMode(othercount, OUTPUT);
pinMode(13, OUTPUT);
pinMode(52, OUTPUT);
pinMode(53, OUTPUT);
pinMode(22, INPUT);//this time we will set the pin as INPUT
pinMode(23, INPUT);
pinMode(24, INPUT);
pinMode(25, INPUT);
pinMode(26, INPUT);
pinMode(27, INPUT);
pinMode(28, INPUT);
pinMode(29, INPUT);
pinMode(31, INPUT);
pinMode(32, INPUT);
pinMode(33, INPUT);
pinMode(34, INPUT);
pinMode(35, INPUT);
pinMode(36, INPUT);
pinMode(37, INPUT);
pinMode(38, INPUT);
pinMode(39, INPUT);
int pn = 1;
int interval = 0;
int padpin = 22;
digitalWrite(13, LOW);
while (pn <= allbuttonNum) {
debounceInterval[interval] = BounceItInterval;
digitalWrite(pn, LOW);
debouncedStates[pn] = LOW;
lastStates[pn] = LOW;
currentStates[pn] = LOW;
padpin++;
interval++;
pn++;
}
//Serial.begin(31250);//initialize Serial connection
Serial.begin(9600);//initialize Serial connection
}
void loop() {
digitalWrite(othercount, LOW);
digitalWrite(onecount, LOW);
//DoMetronome();
Serial.println(metVal);
//POT KNOB CHECKS will be before to ensure the right settings are placed to a button
//CODE GOES HERE
//LoadPadSettings(); // Load settings from file
//GET State Setting From State POT
GETStatePotpin();
//GET Command Setting From Comand POT
GETCommandPotpin();
SelectedPad = 0;
padpin = 22;
otherbtnpin = 38;
int pn = 1;
while (pn < allbuttonNum) {
currentStates[pn] = digitalRead(padpin);
unsigned long currentTime = millis();
if (currentStates[pn] != lastStates[pn]) {
timeOfLastButtonEvents[pn] = currentTime;
}
if (currentTime - timeOfLastButtonEvents[pn] > debounceInterval[pn]) { //if enough time has passed
if ((pn > 0) && (pn < 16)) {
//PAD BUTTONS
if (currentStates[pn] != debouncedStates[pn]) { //if the current state is still different than our last stored debounced state
debouncedStates[pn] = currentStates[pn];//update the debounced state
//trigger an event
PadToChange = 0;
//PushCMDToChange = 0;
//RelCMDToChange = 0;
//HeldCMDToChange = 0;
if (debouncedStates[pn] == HIGH) {
PadToChange = pn;
if ((debouncedStates[pn] == HIGH) && (digitalRead(38) == HIGH) && (pn < 16)) {
//Do Push Select Button
digitalWrite(13, 1 );
SelectedPad = PadToChange;
Serial.println(SelectedPad);
}
//PushCMDToChange = 143 + cmdnum; //Get Current setting From Text File Or ini fro SD Card.
VelToChange = 127;
thispadpin = padpin;
stillHIGH = 1;
StateToChange = 1;
pushbuttonClicked(padpin);
Serial.println("pressed");
digitalWrite(13, 1 );
} else {
VelToChange = 0;
thispadpin = padpin;
//RelCMDToChange = 127 + cmdnum;
//PadToChange = pn;
StateToChange = 0;
stillHIGH = 0;
relbuttonClicked(padpin);
Serial.println("released");
digitalWrite(13, 0 );
}
while (stillHIGH > 0) {
if (debouncedStates[pn] == HIGH) {
thispadpin = padpin;
//VelToChange = '127';
//HeldCMDToChange = 144 + cmdnum;
//PadToChange = pn;
StateToChange = 1;
stillHIGH = 0;
//heldbuttonClicked(22);
Serial.println("HELD...");
digitalWrite(13, 1 );
}
}
}
//Serial.println(message);
pn++;
padpin++;
}
else {
//All OTHER BUTTONS
if (currentStates[pn] != debouncedStates[pn]) { //if the current state is still different than our last stored debounced state
debouncedStates[pn] = currentStates[pn];//update the debounced state
//trigger an event
if (debouncedStates[pn] == HIGH) {
if ((pn == 17) && (digitalRead(38) == HIGH)) {
//Do Push Select Button
otherbtnpin = 38;
int spadnum = 38;
for (int spnum = 38; spnum < 39; spnum++) {
if ((debouncedStates[pn] == HIGH) && (digitalRead(38) == HIGH) && (pn > 16)) {
//Do Push Select Button
stillHIGH = 0;
digitalWrite(13, 1 );
Serial.println("Select Push");
}
spadnum++;
}
}
if ((pn == 18) && (digitalRead(39) == HIGH)) {
//Do Push Save Button
otherbtnpin = 39;
stillHIGH = 0;
if (PadToChange > 0) {
digitalWrite(13, 1 );
TempCMDToChange = 143 + cmdnum;
Serial.println("Save Push");
}
}
}
else {
if ((pn == 17) && (digitalRead(otherbtnpin) == LOW)) {
//Do Release Select Button
stillHIGH = 0;
otherbtnpin = 38;
SelectedPad = PadToChange;
stillHIGH = 0;
//ChangeSelectedPad;
digitalWrite(13, 0 );
Serial.println("Select release");
}
if ((pn == 18) && (digitalRead(otherbtnpin) == LOW)) {
//Do Release Save Button
if (PadToChange > 0) {
stillHIGH = 0;
otherbtnpin = 39;
SavePadSetting();
digitalWrite(13, 0 );
Serial.println("Save Release");
}
}
}
}
otherbtnpin++;
padpin++;
pn++;
}
}
lastStates[pn] = currentStates[pn];
}
}
void RealMetronome()
{
if (delaycount >= 4) {
delaycount = 1;
} else {
delaycount++;
}
if (delaycount == 1) {
digitalWrite(othercount, LOW);
digitalWrite(onecount, HIGH);
} else {
digitalWrite(onecount, LOW);
digitalWrite(othercount, HIGH);
}
}
void GETCommandPotpin() {
int val = analogRead(CMDpotPin);
switch (val)
{
case 0 ... 127:
message = cmds[0];
cmdnum = 1;
break;
case 128 ... 254:
message = cmds[1];
cmdnum = 2;
break;
case 255 ... 381:
message = cmds[2];
cmdnum = 3;
break;
case 382 ... 508:
message = cmds[3];
cmdnum = 4;
break;
case 509 ... 635:
message = cmds[4];
cmdnum = 5;
break;
case 636 ... 762:
message = cmds[5];
cmdnum = 6;
break;
case 763 ... 889:
message = cmds[6];
cmdnum = 7;
break;
case 890 ... 1016:
message = cmds[7];
cmdnum = 8;
break;
default:
message = "Null Command";
}
}
void GETMetVAL() {
switch (mval)
{
case 0 ... 1023:
if (analogRead(A3) < 10) {
metVal = 10;
} else {
mval = analogRead(A3);
metVal = mval;
}
break;
default:
metVal = 130;
}
}
void GETStatePotpin() {
int Sval = analogRead(StateCMDpotPin);
snum = 0;
switch (Sval)
{
case 0 ... 341:
TempStatemessage = whencmds[0];
snum = 1;
break;
case 342 ... 682:
TempStatemessage = whencmds[1];
snum = 2;
break;
case 683 ... 1023:
TempStatemessage = whencmds[2];
snum = 3;
break;
default:
TempStatemessage = "Null State";
}
}
void SavePadSetting() {
//Search for the correct state to save
if (whencmds[snum] == "Push") {
PushCMDToChange = TempCMDToChange;
}
if (whencmds[snum] == "Held") {
HeldCMDToChange = TempCMDToChange;
}
if (whencmds[snum] == "Release") {
RelCMDToChange = TempCMDToChange;
}
//then save chages to file
//Located at SelectedPad (retruns the currently selected pad as a int);
//Settings are automaticly reloaded as they are applyed with the next loop rotaion.
}
void pushbuttonClicked(int btn) {
if (btn == padpin && debouncedStates[btn] == HIGH) {
pushchangeAllStates();
}
}
void heldbuttonClicked(int btn) {
if (btn == padpin && debouncedStates[btn] == HIGH) {
heldchangeAllStates();
}
}
void relbuttonClicked(int btn) {
if (btn == padpin && debouncedStates[btn] == HIGH) {
relchangeAllStates();
}
}
void pushchangeAllStates() {
noteOn(PushCMDToChange, NoteToChange, VelToChange);
}
void heldchangeAllStates() {
noteOn(HeldCMDToChange, NoteToChange, VelToChange);
}
void relchangeAllStates() {
noteOn(RelCMDToChange, NoteToChange, VelToChange);
}
void noteOn(byte cmd, byte pitch, byte velocity) {
//mySerial.write(cmd);
//mySerial.write(pitch);
// mySerial.write(velocity);
//Serial.write(cmd);
//Serial.write(pitch);
//Serial.write(velocity);
}