Hi People,
I've spent the last few days writing up some code trying to get 4 buttons, with a long debounce (conductive fabric) and a panic timer (in case the material twists) working, with very little success. This is my first time working with arrays and I thought I had it nutted, but I just don't know where I've gone wrong. Any help would be appreciated.
// constants won't change. They're used here to
// set pin numbers:
const int inputPin[] = {2, 4, 7, 8}; // these are the pins you connect the buttons to.
const int ledPin[] = {3, 5, 6, 9};
const int qty = sizeof(inputPin) / sizeof(inputPin[0]);
// Variables will change:
int ledState[qty]; // the current state of the output pin
boolean ledDir[qty]; // switch to tell LED to fade up or down
int buttonState[qty]; // the current reading from the input pin
int buttonRead[qty];
unsigned long microsStart;
int lastButtonState[] = {0, 0, 0, 0}; // the previous reading from the input pin
byte midiChannels[] = {1, 2, 3, 4}; // these are the midi channels each sensor transmits on
byte midiNotes[] = {36, 37, 38, 39}; // these are the midi notes sent by the sensors
byte midiVelocities[] = {127, 127, 127, 127}; // and velocities...
int noteCount; // Set a countdown timer for the Note On
int timer = 10; //set a global timer for note hold (is referred to by noteCount etc.)
long previousMillis = 0; //This is the counter used for the time, which in turn is used for the noteCount
long interval = 12;
long previousPanic = 0;
int panicTrigger = 1000;
int panicTime[] = {0, 0, 0, 0,};
int panic[qty];
void setup() {
// Set MIDI baud rate:
Serial.begin(9600);
for (int i = 0; i < qty; i++)
{
pinMode(inputPin[i], INPUT_PULLUP);
buttonState[i] = 0xFF;
buttonRead[i] = 1;
pinMode(ledPin[i], OUTPUT);
}
}
void loop() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
unsigned long currentPanic = millis();
// if (currentPanic - previousPanic > panicTrigger) { //SORT THIS OUT!!!
// previousPanic = currentPanic;
buttonFilter();
for (int j = 0; buttonRead[j] < qty; j++) {
// send note
for (int k = 0; panicTime[k] < panicTrigger; k++) {
if (buttonRead[j] == 1 && panicTime[k] < panicTrigger) {
panicTime[k]++;
}
else { //may need to remove
panicTime[k] = 0;
}
if (panicTime[k] >= panicTrigger) {
panic[k] = 1;
Serial.println("panic at button [k]");
}
}
if (buttonRead[j] == 1 && panic[j] != 1) {
noteOn(0x8f+midiChannels[j], midiNotes[j], midiVelocities[j]);
if (ledDir[j] == 1) {
ledState[j]++;
}
if (ledDir[j] == 0) {
ledState[j]--;
}
// if (panic[j] == 1) {
// // NEED OME NOTEOFF HERE
// noteOff(0x7f+midiChannels[j], midiNotes[j], midiVelocities[j]);
// }
}
}
// loop from the highest pin to the lowest:
for (int j = qty - 1; j >= 0; j--) {
// send note
if (buttonRead[j] == 1 && panic[j] != 1) {
noteOn(0x8f+midiChannels[j], midiNotes[j], midiVelocities[j]);
ledState[j] + 1;
}
/* else (buttonRead[j] == 1 && panic[j] != 1 && ledState[j] > 0) {
noteOn(0x8f+midiChannels[j], midiNotes[j], midiVelocities[j]);
ledState[j]--;
} */
if (panic[j] == 1) {
noteOff(0x7f+midiChannels[j], midiNotes[j], midiVelocities[j]);
ledState[j] = 0;
}
}
}
for (int k = 0; ledPin[k] < qty; k++) {
analogWrite(ledPin[k], ledState[k]);
}
}
void buttonFilter(void)
{
if (micros() - microsStart >= 2000) // minimum interval between bounces = 2 ms
{
for (int i = 0; i < qty; i++)
{
buttonState[i] = (buttonState[i] << 1) | digitalRead(inputPin[i]); // shift and read
if ((buttonState[i] & B11111) == B01111) // if rising and high for 3 stable reads
{
buttonRead[i] = 1;
}
if ((buttonState[i] & B11111) == B10000) // if falling and low for 3 stable reads
{
buttonRead[i] = 0;
}
}
}
microsStart = micros();
}
// plays a MIDI note. Doesn't check to see that
// cmd is greater than 127, or that data values are less than 127:
void noteOn(int cmd, int pitch, int velocity) {
Serial.write(cmd);
Serial.write(pitch);
Serial.write(velocity);
}
void noteOff(int cmd, int pitch, int velocity) {
Serial.write(cmd);
Serial.write(pitch);
Serial.write(velocity);
}