30 Light StopJamb

/*

Lighted Stop Jamb Buttons: HW Combination action.

=========================================

This sketch is designed to scan 30 lighted tactile buttons.

ON/OFF messages are sent to HW on channel 5.

These ON/OFF messages, echoed back from HW, are used to turn the lights on and off.

A SET button in implemented via pin 69.

Outputs: pins 2 - 31

Inputs: pins 34 - 63

HW codes: 36 - 65

SET button: Pin 69

I/O is through Port 0

Output is to HW.

Equipment: Arduino Mega with one MIDI shield

created 2022 JAN 5

modified 2022 JAN5

by John Coenraads

*/


// Declarations==========================================


//Counters (old Fortran habit)

int i, j, k;


//Receive variables


byte noteStatusRx;


byte noteNumberRx;


byte noteVelocityRx;


int pin; //Arduino pin number


byte setState = 0; //flag to remember that set piston ON message has been sent


byte stopONArray [100]; //Tracks which stops are on and which LEDs are lit.


//Initialize =========================================================

void setup()

{

// Set MIDI baud rate:

Serial.begin(31250);


//Initialize input. Normally high (via internal pullups)

for (i = 34; i < 64; i++)

{

pinMode (i, INPUT_PULLUP);

}

//Initialize output (normally low)

for (i = 2; i < 32; i++)

{

pinMode (i, OUTPUT);

digitalWrite (i, LOW);

}


pinMode (69, INPUT_PULLUP); // General Cancel Piston


//Initialize Array to zero

for (i= 0; i < 100; i++){

stopONArray[i] = 0;

}

}


//Main Loop ===========================================================

void loop()

{

scanGroup();

scanSet();

processSerial ();

}


//Scan thirty Buttons

void scanGroup ()

{

for (pin = 34; pin < 64; pin++)

{

if (digitalRead(pin) == LOW)

{

toggleStop ();}

}

}


void toggleStop ()

{

if (stopONArray[pin] == 0)

{

// stopONArray[pin] = 1;

// digitalWrite (pin - 32, HIGH); //turn ON light

Serial.write (0x94); //note ON, channel 5

Serial.write (pin + 2);

Serial.write (0x7f); //medium velocity

}

else

{

// stopONArray[pin] = 0;

// digitalWrite (pin - 32, LOW); //turn OFF light

Serial.write (0x94); //note OFF,

Serial.write (pin + 2);

Serial.write (0); //zero velocity

}

delay (1); //debounce delay

while (digitalRead(pin) == LOW) {

}; //wait for button to be released

delay (1);


}


void scanSet()

{

if (digitalRead(69) == LOW) // set button

{

if (setState == 0)

{

Serial.write (0x94); //note ON,

Serial.write (69);

Serial.write (127); //med velocity

setState = 1;

}

else

{

Serial.write (0x94); //note OFF,

Serial.write (69);

Serial.write (0); //zero velocity

setState = 0;

}

}

delay (1); //debounce delay

while (digitalRead(69) == LOW) {}; //wait for button to be released

delay (1);

}


//Process data received for serial port 0 (from preceding Arduino): status, note number, velocity

//Note: this bare bones version does not handle running status.


void processSerial ()

{

if (Serial.available())

{

noteStatusRx = Serial.read ();

if (noteStatusRx > 0x7F) //is status byte

{

while (!Serial.available()) {

} //wait for serial data, port 0

noteNumberRx = Serial.read ();

while (!Serial.available()) {

} //wait for serial data, port 0

noteVelocityRx = Serial.read ();

if (noteStatusRx == 0x84) {noteVelocityRx = 0;} // HW doesn't use zero velocity for OFF

pin = noteNumberRx - 2;

if (noteVelocityRx != 0)

{

stopONArray[pin] = 1;

digitalWrite (pin - 32, HIGH); //turn ON light

}

else

{

stopONArray[pin] = 0;

digitalWrite (pin - 32, LOW); //turn OFF light

}

}

}

}