Hello,
my first project! i'm using Mega 2560:
- divided into 8 section (A to G )
- each section is 1 X cd4067 + 1 UP button + 1 DN button ( button to go up or down in the cd4067 channel)
- 1 save button
So far:
- i can make each cd4067 go up and down.
- I'm using 2 section for now to simplify,
- code gathering from everywhere...
- i've found the way to have a start position
I want to be able to save the state (channel) of each cd4067 into each Program Change #(from 0 to 127)
So a Program Change # should be able to have 8 X (4 pin state) and make it change on receiving MIDI message....
I've found the MIDI library: #include <MIDI.h>
But not sure how to totally use it....
code sor far:
#include <Bounce2.h>
const int channel[] = { 22, 23, 24, 25};
const int buttonPinUp = 26;
const int buttonPinDn = 29;
int buttonStateUp = 0;
int lastButtonStateUp = LOW;
int buttonStateDn = 0;
int lastButtonStateDn = LOW;
int thisChannel = 8;
const int channelB[] = { 36, 37, 38, 39};
const int buttonPinUpB = 40;
const int buttonPinDnB = 41;
int buttonStateUpB = 0;
int lastButtonStateUpB = LOW;
int buttonStateDnB = 0;
int lastButtonStateDnB = LOW;
int thisChannelB = 1;
// Instantiate a Bounce object
Bounce debouncer1 = Bounce();
// Instantiate another Bounce object
Bounce debouncer2 = Bounce();
// Instantiate a Bounce object
Bounce debouncer1B = Bounce();
// Instantiate another Bounce object
Bounce debouncer2B = Bounce();
void setup() {
// initialize the pushbutton pin as an input:
pinMode(buttonPinUp, INPUT);
debouncer1.attach(buttonPinUp);
debouncer1.interval(5); // interval in ms
pinMode(buttonPinDn, INPUT);
debouncer2.attach(buttonPinDn);
debouncer2.interval(5); // interval in ms
// set up all pins as output:
for (int thisPin = 2; thisPin < 7; thisPin++) {
pinMode(thisPin, OUTPUT);
}
// initialize the pushbutton pin as an input:
pinMode(buttonPinUpB, INPUT);
debouncer1B.attach(buttonPinUpB);
debouncer1B.interval(5); // interval in ms
pinMode(buttonPinDnB, INPUT);
debouncer2B.attach(buttonPinDnB);
debouncer2B.interval(5); // interval in ms
// set up all pins as output:
for (int thisPinB = 2; thisPinB < 7; thisPinB++) {
pinMode(thisPinB, OUTPUT);
}
}
void loop() {
muxWrite(thisChannel); //start position
muxWriteB(thisChannelB); //start position
debouncer1.update();
debouncer2.update();
// Get the updated value :
int value1 = debouncer1.read();
int value2 = debouncer2.read();
// read the state of the pushbutton value:
buttonStateUp = digitalRead(buttonPinUp);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonStateUp == HIGH && thisChannel < 15 && value1 == LOW) {
thisChannel++;
muxWrite(thisChannel);
}
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
buttonStateDn = digitalRead(buttonPinDn);
if (buttonStateDn == HIGH && thisChannel > 0 && value2 == LOW) {
thisChannel--;
muxWrite(thisChannel);
}
debouncer1B.update();
debouncer2B.update();
// Get the updated value :
int value1B = debouncer1B.read();
int value2B = debouncer2B.read();
// read the state of the pushbutton value:
buttonStateUpB = digitalRead(buttonPinUpB);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonStateUpB == HIGH && thisChannelB < 15 && value1B == LOW) {
thisChannelB++;
muxWriteB(thisChannelB);
}
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
buttonStateDnB = digitalRead(buttonPinDnB);
if (buttonStateDnB == HIGH && thisChannelB > 0 && value2B == LOW) {
thisChannelB--;
muxWriteB(thisChannelB);
}
delay(50);
}
void muxWrite(int whichChannel) {
// iterate over the number of pins you're using:
for (int thisPin = 0; thisPin < 4; thisPin++) {
// calculate the state of this pin based on
// its bit value in whichChannel:
int pinState = bitRead(whichChannel, thisPin);
// turn the pin on or off:
digitalWrite(channel[thisPin],pinState);
}
}
void muxWriteB(int whichChannelB) {
// iterate over the number of pins you're using:
for (int thisPinB = 0; thisPinB < 4; thisPinB++) {
// calculate the state of this pin based on
// its bit value in whichChannel:
int pinStateB = bitRead(whichChannelB, thisPinB);
// turn the pin on or off:
digitalWrite(channelB[thisPinB],pinStateB);
}
}
any help is very appreciated!
regards,
Bent
mux.txt (3.86 KB)