#include <MIDI.h>
#include "Controller.h"
int AD_PIN1 = A0;
int AD_PIN2 = A3;
byte NUMBER_BUTTONS = 8;
//Button (Analog Pin, KCButton, CC Number, Channel, Debounce Time)
//** Command parameter 0=NOTE 1=CC 2=Toggle CC **
Button BU1(A0, 1, 0, 61, 127, 1, 5 );
Button BU2(A0, 2, 0, 1, 127, 1, 5 );
Button BU3(A0, 3, 0, 52, 127, 1, 5 );
Button BU4(A0, 4, 0, 69, 8, 1, 5 );
Button BU5(A3, 1, 0, 60, 127, 1, 5 );
Button BU6(A3, 2, 0, 61, 0, 1, 5 );
Button BU7(A3, 3, 0, 53, 127, 1, 5 );
Button BU8(A3, 4, 0, 69, 9, 1, 5 );
//Add buttons used to array below like this-> Button *BUTTONS[] {&BU1, &BU2, &BU3, &BU4, &BU5, &BU6, &BU7, &BU8};
Button *BUTTONS[] {&BU1, &BU2, &BU3, &BU4, &BU5, &BU6, &BU7, &BU8};
void controlChange(byte control, byte value, byte channel) {
uint8_t msg[3] { uint8_t(0xB0 | channel), control, value };
Serial1.write(msg, 3);
}
void updateButtons() {
// Cycle through Button array
for (int i = 0; i < NUMBER_BUTTONS; i = i + 1) {
byte message = BUTTONS[i]->getValue();
if (message == 1) { // Button is pressed
controlChange(BUTTONS[i]->Bcc, BUTTONS[i]->Bvalue, BUTTONS[i]->Bchannel);
break;
}
if (message == 0) { // Button is not pressed
//controlChange(BUTTONS[i]->Bcc, 0, BUTTONS[i]->Bchannel);
}
}
}
void setup() {
Serial1.begin(31250);
Serial.begin(115200);
}
void loop() {
int adValue = analogRead(AD_PIN1);
Serial.println(buttonFromValue(adValue));
int adValue2 = analogRead(AD_PIN2);
Serial.println(buttonFromValue2(adValue2));
if (NUMBER_BUTTONS != 0) updateButtons();
}
// Returns the button number based on the analog value
byte buttonFromValue(int adValue) {
if (adValue > 300 && adValue < 500) {
return 1;
}
if (adValue > 500 && adValue < 700) {
return 2;
}
if (adValue > 700 && adValue < 900) {
return 3;
}
if (adValue > 900) {
return 4;
}
return 0;
}
byte buttonFromValue2(int adValue2) {
if (adValue2 > 300 && adValue2 < 500) {
return 1;
}
if (adValue2 > 500 && adValue2 < 700) {
return 2;
}
if (adValue2 > 700 && adValue2 < 900) {
return 3;
}
if (adValue2 > 900) {
return 4;
}
return 0;
}
controller.cpp
#include "Controller.h"
//Button (Pin Number, Command, Note Number, Channel, Debounce Time)
Button::Button(byte pin, byte kcbutton, byte command, byte cc, byte value, byte channel, byte debounce)
{
_pin = pin;
_kcbutton = kcbutton; // add this line
pinMode(_pin, INPUT); // add this line
_cc = cc;
_value = value;
_command = command;
_debounce = debounce;
_time = 0;
_busy = false;
_status = 0b00000010;
_last = 1;
Bcc = cc;
Bcommand = command;
Bvalue = value;
Bchannel = channel;
Btoggle = 0;
}
byte Button::buttonFromValue(int adValue) { // add this whole function
if (adValue > 300 && adValue < 500) {
return 1;
}
if (adValue > 500 && adValue < 700) {
return 2;
}
if (adValue > 700 && adValue < 900) {
return 3;
}
if (adValue > 900) {
return 4;
}
return 0;
}
byte Button::getValue()
{
// If BUSY bit not set - read button
if (bitRead(_status, 0) == false) { // If busy false
if ((buttonFromValue(analogRead(_pin)) == _kcbutton) == _last) return 2; // If same as last state - exit
} // i think it is like this
// If NEW Bit set - Key just pressed, record time
if (bitRead(_status, 1) == true) { // If new is true
bitSet(_status, 0); // Set busy TRUE
bitClear(_status, 1); // Set New FALSE
_time = millis();
return 255;
}
// Check if debounce time has passed - If no, exit
if (millis() - _time < _debounce) return 255;
// Debounce time has passed. Read pin to see if still set the same
// If it has changed back - assume false alarm
if ((buttonFromValue(analogRead(_pin)) == _kcbutton) == _last) {
bitClear(_status, 0); // Set busy false
bitSet(_status, 1); // Set new true
return 255;
}
// If this point is reached, event is valid. return event type
else {
bitClear(_status, 0); // Set busy false
bitSet(_status, 1); // Set new true
_last = ((~_last) & 0b00000001); // invert _last
return _last; // invert the logic here.
}
}
void Button::newValue(byte command, byte value, byte channel) {
Bvalue = value;
Bcommand = command;
Bchannel = channel;
}
controller.h
#ifndef Controller_h
#define Controller_h
#include <Arduino.h>
//Button (Pin Number, Command, Note Number, Channel, Debounce Time)
class Button
{
public:
Button(byte pin, byte kcbutton, byte command, byte cc, byte value, byte channel, byte debounce); // change this line
//Button(Mux mux, byte muxpin, byte command, byte value, byte channel, byte debounce);
byte getValue();
//void muxUpdate();
void newValue(byte command, byte value, byte channel);
byte Bcc;
byte Bcommand;
byte Bvalue;
byte Bchannel;
byte Btoggle;
private:
byte buttonFromValue(int adValue); // add this line
byte _previous;
byte _current;
unsigned long _time;
int _debounce;
byte _pin;
byte _kcbutton; // and this one
//byte _muxpin;
//byte _numMuxPins;
byte _cc;
byte _value;
byte _command;
bool _busy;
byte _status;
byte _last;
byte _enablepin;
};
#endif
It's for the BU2 with cc 1
Thank's a lot !