// ControlPad Message WIP
#include <Bounce.h>
const int channel = 1;
int ledPin = 13;
byte pgcNum = 0;
const int analogPin = A0;
const int thresholdMin = 80; // minimum reading, avoid noise and false starts
const int peakTrackMillis = 12;
const int aftershockMillis = 25; // aftershocks & vibration reject
bool Mod0 = digitalRead(0);
bool Mod1 = digitalRead(1);
Bounce button0 = Bounce(0, 5);
Bounce button1 = Bounce(1, 5);
void setup() {
pinMode(13, OUTPUT);
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
}
void loop() {
button0.update();
button1.update();
if (button0.fallingEdge() && pgcNum < 127) { //FootSwitch 1
pgcNum++;
usbMIDI.sendProgramChange(pgcNum,channel);
Serial.print("FSInc");
digitalWrite(ledPin, HIGH);
delay(10);
digitalWrite(ledPin, LOW);
}
if (button1.fallingEdge()) { // FootSwitch 2
usbMIDI.sendRealTime(usbMIDI.Stop);
Serial.print("FSStop");
digitalWrite(ledPin, HIGH);
delay(10);
digitalWrite(ledPin, LOW);
}
int piezo = analogRead(analogPin);
peakDetect(piezo);
while (usbMIDI.read()) {
// ignore incoming messages
}
}
void peakDetect(int voltage) {
static int state; // 0=idle, 1=looking for peak, 2=ignore aftershocks
static int peak; // remember the highest reading
static elapsedMillis msec; // timer to end states 1 and 2
switch (state) {
// IDLE state: wait for any reading is above threshold. Do not set
// the threshold too low. You don't want to be too sensitive to slight
// vibration.
case 0:
if (voltage > thresholdMin) {
peak = voltage;
msec = 0;
state = 1;
}
return;
// Peak Tracking state: capture largest reading
case 1:
if (voltage > peak) {
peak = voltage;
}
if (msec >= peakTrackMillis && pgcNum < 127 ); { // ControlPad
Serial.print("PadInc");
pgcNum++;
usbMIDI.sendProgramChange(pgcNum,channel);
msec = 0;
state = 2;
}
if (Mod0) {
Serial.print("PadDec");
pgcNum--;
usbMIDI.sendProgramChange(pgcNum,channel);
msec = 0;
state = 2;
}
else {
if (Mod1) {
Serial.print("PadMidiStart");
usbMIDI.sendRealTime(usbMIDI.Start);
msec = 0;
state = 2;
}
return;
// Ignore Aftershock state: wait for things to be quiet again.
default:
if (voltage > thresholdMin) {
msec = 0; // keep resetting timer if above threshold
} else if (msec > aftershockMillis) {
state = 0; // go back to idle when
}
}
}
}
case 1:
if (voltage > peak) {
peak = voltage;
}
if (msec >= peakTrackMillis && pgcNum < 127 ); { // ControlPad
Serial.print("PadInc");
pgcNum++;
usbMIDI.sendProgramChange(pgcNum,channel);
}
if (Mod0) {
Serial.print("PadDec");
pgcNum--;
usbMIDI.sendProgramChange(pgcNum,channel);
}else{
if (Mod1) {
Serial.print("PadMidiStart");
usbMIDI.sendRealTime(usbMIDI.Start);
}
}
[COLOR="#FF0000"] msec = 0;
state = 2;[/COLOR]
return;
// Peak Tracking state: capture largest reading
case 1:
if (voltage > peak) {
peak = voltage;
}
if (msec >= peakTrackMillis && pgcNum < 127 ); { // ControlPad
Serial.print("PadInc");
pgcNum++;
usbMIDI.sendProgramChange(pgcNum,channel);
msec = 0;
state = 2;
}
if (Mod0) {
Serial.print("PadDec");
pgcNum--;
usbMIDI.sendProgramChange(pgcNum,channel);
}else{
if (Mod1) {
Serial.print("PadMidiStart");
usbMIDI.sendRealTime(usbMIDI.Start);
}
}
return;
// Peak Tracking state: capture largest reading
case 1:
if (voltage > peak) {
peak = voltage;
}
if (msec >= peakTrackMillis && pgcNum < 127 ); { // ControlPad
Serial.print("PadInc");
pgcNum++;
usbMIDI.sendProgramChange(pgcNum,channel);
if (Mod0) {
Serial.print("PadDec");
pgcNum--;
usbMIDI.sendProgramChange(pgcNum,channel);
}else{
if (Mod1) {
Serial.print("PadMidiStart");
usbMIDI.sendRealTime(usbMIDI.Start);
}
}
msec = 0;
state = 2;
}
return;
// ControlPad Message WIP
#include <Bounce.h>
const int channel = 1;
int ledPin = 13;
byte pgcNum = 0;
const int analogPin = A0;
const int thresholdMin = 80; // minimum reading, avoid noise and false starts
const int peakTrackMillis = 12;
const int aftershockMillis = 25; // aftershocks & vibration reject
bool Mod0 = digitalRead(0);
bool Mod1 = digitalRead(1);
Bounce button0 = Bounce(0, 10);
Bounce button1 = Bounce(1, 10);
void setup() {
pinMode(13, OUTPUT);
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
}
void loop() {
button0.update();
button1.update();
// Foot Switches
if (button0.fallingEdge() && pgcNum < 127) { //FootSwitch 1
pgcNum++;
usbMIDI.sendProgramChange(pgcNum,channel);
Serial.println("FS_PGC_Inc");
digitalWrite(ledPin, HIGH);
delay(10);
digitalWrite(ledPin, LOW);
}
if (button1.fallingEdge()) { // FootSwitch 2
usbMIDI.sendRealTime(usbMIDI.Stop);
Serial.println("FS_MidiStop");
digitalWrite(ledPin, HIGH);
delay(10);
digitalWrite(ledPin, LOW);
}
int piezo = analogRead(analogPin);
peakDetect(piezo);
// ControlPad
while (usbMIDI.read()) {
// ignore incoming messages
}
}
void peakDetect(int voltage) {
static int state; // 0=idle, 1=looking for peak, 2=ignore aftershocks
static int peak; // remember the highest reading
static elapsedMillis msec; // timer to end states 1 and 2
switch (state) {
// IDLE state: wait for any reading is above threshold. Do not set
// the threshold too low. You don't want to be too sensitive to slight
// vibration.
case 0:
if (voltage > thresholdMin) {
peak = voltage;
msec = 0;
state = 1;
}
return;
// Peak Tracking state: capture largest reading
case 1:
if (voltage > peak) {
peak = voltage;
}
if (msec >= peakTrackMillis && pgcNum < 127 ); { // ControlPad
Serial.println("Pad_PGC_Inc");
pgcNum++;
usbMIDI.sendProgramChange(pgcNum,channel);
if (Mod0 && pgcNum >= 1 ) {
Serial.println("Pad_PGC_Dec");
pgcNum--;
usbMIDI.sendProgramChange(pgcNum,channel);
}else{
if (Mod1) {
Serial.println("Pad_MidiStart");
usbMIDI.sendRealTime(usbMIDI.Start);
}
}
msec = 0;
state = 2;
}
return;
// Ignore Aftershock state: wait for things to be quiet again.
default:
if (voltage > thresholdMin) {
msec = 0; // keep resetting timer if above threshold
} else if (msec > aftershockMillis) {
state = 0; // go back to idle when
}
}
}
Then it really should not be added to an existing and crucial test.It's really only there to avoid wrapping at the top and bottom values and keep track of the pgcNum Value.
pgcNum++
if (pgcNum >= 128){
pgcNum = 127;
}
void peakDetect(int voltage) {
static int state; // 0=idle, 1=looking for peak, 2=ignore aftershocks
static int peak; // remember the highest reading
static elapsedMillis msec; // timer to end states 1 and 2
switch (state) {
// IDLE state: wait for any reading is above threshold. Do not set
// the threshold too low. You don't want to be too sensitive to slight
// vibration.
case 0:
if (voltage > thresholdMin) {
peak = voltage;
msec = 0;
state = 1;
}
return;
// Peak Tracking state: capture largest reading
case 1:
if (voltage > peak) {
peak = voltage;
}
if (msec >= peakTrackMillis ); {
Serial.println("Pad_PGC_Inc");
[COLOR="#FF0000"]// this is where it was... [/COLOR]
usbMIDI.sendProgramChange(pgcNum,channel);
if (Mod0 && pgcNum >= 1 ) {
Serial.println("Pad_PGC_Dec");
pgcNum--;
usbMIDI.sendProgramChange(pgcNum,channel);
}else{
if (Mod1) {
Serial.println("Pad_MidiStart");
usbMIDI.sendRealTime(usbMIDI.Start);
}else{ [COLOR="#FF0000"]//this is where it only happens with no mods[/COLOR]
[COLOR="#005500"] pgcNum++;
if (pgcNum > 127) {
pgcNum = 127
}[/COLOR]
}
}
msec = 0;
state = 2;
}
return;
// Ignore Aftershock state: wait for things to be quiet again.
default:
if (voltage > thresholdMin) {
msec = 0; // keep resetting timer if above threshold
} else if (msec > aftershockMillis) {
state = 0; // go back to idle when
}
}
}