I have been looking for my error for a few days on this now. I hope you have not tired of my enquiries.
If you can imagine the same schematic as above, except with every switch having its own pull down resistor, that would save me drawing another schematic.
I have a button class and have created an array of button objects. The code also attempts to debounce the buttons but unfortunately i am getting nothing from it, no midi.
I have a feeling that the debounce function is not setting the message correctly.
Would you mind taking a quick look at it please?
One word of knowledge from the wise saves hours of wasted time for the ignorant.
#include "Controller.h"
//****************************************************************************************
//Button (Pin Number, Command, Note Number, Channel, Debounce Time)
Button::Button(byte pin, byte command, byte value, byte channel, byte debounce)
{
_pin = pin;
pinMode(_pin, INPUT);
_value = value;
_command = command;
_debounce = debounce;
_time = 0;
_busy = false;
_status = 0b00000010;
_last = 1;
Buttcommand = command;
Buttvalue = value;
Buttchannel = channel;
Butttoggle = 0;
}
byte Button::getValue()
{
// If BUSY bit not set - read button
if (bitRead(_status, 0) == false) { // If busy false
if (digitalRead(_pin) == _last) return 2; // If same as last state - exit
}
// 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 (digitalRead(_pin) == _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;
}
}
void Button::newValue(byte command, byte value, byte channel)
{
Buttvalue = value;
Buttcommand = command;
Buttchannel = channel;
}
//********************************************************************
#ifndef Controller_h
#define Controller_h
#include <Arduino.h>
//************************************************************************
//Button (Pin Number, Command, Note Number, Channel, Debounce )
class Button
{
public:
Button(byte pin, byte command, byte value, byte channel, byte debounce);
byte getValue();
void newValue(byte command, byte value, byte channel);
byte Buttcommand;
byte Buttvalue;
byte Buttchannel;
byte Butttoggle;
private:
byte _previous;
byte _current;
unsigned long _time;
int _debounce;
byte _pin;
byte _value;
byte _command;
bool _busy;
byte _status;
byte _last;
byte _enablepin;
};
//*************************************************************************
#endif
#include <MIDI.h>
#include "Controller.h"
MIDI_CREATE_DEFAULT_INSTANCE();
byte NUMBER_BUTTONS = 0;
Button butt1(2, 0x90, 48, 1, 5 );
Button butt2(3, 0x90, 49, 1, 5 );
Button butt3(4, 0x90, 50, 1, 5 );
Button butt4(5, 0x90, 51, 1, 5 );
Button *BUTTONS[] {&butt1, &butt2, &butt3, &butt4};
void setup() {
MIDI.begin(MIDI_CHANNEL_OFF);
}
void loop() {
updateButts();
}
//*****************************************************************
void updateButts() {
// Cycle through Button array
for (int i = 0; i < NUMBER_BUTTONS; i = i + 1) {
byte message = BUTTONS[i]->getValue();
// Button is pressed
if (message == 0) {
{
//note on
Serial.write(BUTTONS[i]->Buttcommand);
Serial.write(BUTTONS[i]->Buttchannel);
Serial.write(BUTTONS[i]->Buttvalue);
Serial.write(127); //note on
}
// Button is not pressed
if (message == 1) {
Serial.write(BUTTONS[i]->Buttcommand);
Serial.write(BUTTONS[i]->Buttchannel);
Serial.write(BUTTONS[i]->Buttvalue);
Serial.write(0); //note off or cc off
}
}
}
}