Hello,
I'm working on a MIDI controlled drumset in which my snare drum will use 2 drumsticks activated by solenoids through a relay module.
I've written my code to alternate drumsticks with each MIDI NOTE-ON signal but for some reason, it appears that the variable that dictates which drumstick to use (snareStick) is not updating and prevents them from alternating.
At the moment, each snare NOTE-ON signal:
- skips the variable update
- turns on the built-in LED
- and hits with the first stick
void HandleNoteOn(byte channel, byte pitch, byte velocity) {
switch (pitch) {
case 38://----------------------Snare = D2/38
if(snareStick = 1){
snareStick = 2; digitalWrite(LED_BUILTIN, HIGH);
Hit(snare1);
}
else{
snareStick = 1; digitalWrite(LED_BUILTIN, LOW);
Hit(snare2);
}
break;
default:
break;
}
}
This code is pretty simple so I imagine there is some disconnect in scope with the MIDI.read & HandleNoteOn functions or some other factor I wouldn't know about.
Here's my entire code:
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx DEFFINITIONS xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
//---------SETUP PARAMETERS---------
const int hitDur = 80; //solenoid ON duration for each hit (in milliseconds)
int snareStick = 1; //snare stick that plays first (alternates afterwards)
//---------------PINS---------------
const int snare1 = 2;
const int snare2 = 3;
const int bass = 4;
const int cymbal = 5;
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx MAIN-FUNCTIONS xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//=====================================SETUP============================
void setup() {
//---------------MIDI SETUP--------------
MIDI.begin(10);
MIDI.setHandleNoteOn(HandleNoteOn);
//---------------PIN MODES---------------
pinMode( snare1, OUTPUT );
pinMode( snare2, OUTPUT );
pinMode( bass, OUTPUT );
pinMode( cymbal, OUTPUT );
pinMode(LED_BUILTIN, OUTPUT);
//---------------RUN TEST---------------
Test(); //test solenoids at start
}
//======================================LOOP============================
void loop() {
MIDI.read();
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx SUB-FUNCTIONS xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//===============================HandleNoteOn===========================
void HandleNoteOn(byte channel, byte pitch, byte velocity) {
switch (pitch) {
case 38://----------------------Snare = D2/38
if(snareStick = 1){
snareStick = 2; digitalWrite(LED_BUILTIN, HIGH);
Hit(snare1);
}
else{
snareStick = 1; digitalWrite(LED_BUILTIN, LOW);
Hit(snare2);
}
break;
case 35://----------------------Bass = B1/35
Hit(bass);
break;
case 44://----------------------Cymbal = G#2/44
Hit(cymbal);
break;
default:
break;
}
}
//=================================== Hit ===================================
void Hit(int drum) {
digitalWrite(drum, HIGH); //turn solenoid on
delay(hitDur); // wait
digitalWrite(drum, LOW); //turn solenoid off
}
//================================== Test ===================================
void Test() { //for debugging
Hit(snare1); delay(400);
Hit(snare2); delay(400);
Hit(bass); delay(400);
Hit(cymbal); delay(400);
}
Thanks for your help!
