Hello, I'm completely new at C++ and I'm trying to translate a MIDI pedalboard software by me in a more elegant one and to learn.
I know that this code is actually a mess but I'm stuck in the middle of the process.
maybe I have to replace some byte type in .cpp and .h files in char or unsigned char and I already tried but it didn't solve my problem.
By the way, this is the error I get:
Compiling 'myMIDIcontroller' for 'Arduino Uno'
cc1hOuIW.ltrans0.ltrans.o*: In function global constructors keyed to 65535_0_myMIDIcontroller.cpp.o.1915
Error linking for board Arduino Uno
(.text.startup+0x142): undefined reference to MIDIButton::MIDIButton(int, unsigned char, unsigned char, unsigned char)
(.text.startup+0x152): undefined reference to MIDIButton::MIDIButton(int, unsigned char, unsigned char, unsigned char)
Build failed for project 'myMIDIcontroller'
cc1hOuIW.ltrans0.ltrans.o*: In function loop
myMIDIcontroller.ino:29: undefined reference to MIDIButton listener()
myMIDIcontroller.ino:30: undefined reference to MIDIButton listener()
collect2.exe*: error: ld returned 1 exit status
myMIDIcontroller.ino
#include "MIDIButton.h"
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
// digital pin 2 has a pushbutton attached to it. Give it a name:
#define btn1pin 2
#define btn2pin 3
const int debounceTime = 5; //debounce time in milliseconds
bool btnState;
bool btnLast;
byte ccValue;
MIDIButton btn1(btn1pin, 0xB0, 0x14, 0);
MIDIButton btn2(btn1pin, 0xB0, 0x15, 0);
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
//Serial.begin(115200);
// Set MIDI baud rate:
MIDI.begin(1);
//Serial.begin(31250);
// make the pushbutton's pin an input:
}
// the loop routine runs over and over again forever:
void loop() {
btn1.listener();
btn2.listener();
/*
// read the input pin:
btnState = digitalRead(btn1pin);
if (btnState != btnLast) {
delay(debounceTime);
if (btnState == LOW && btnLast == HIGH) {
// print out the state of the button:
ccToggle(0x14, ccValue);
} else {
// print out the state of the button:
//Serial.println(btnState);
}
btnLast = btnState;
}
*/
}
MIDIButton.h
/*
MIDIButton.h - Library for button behavior.
Created by Luca Troncone, 13th May 2020.
Released into the public domain.
*/
#ifndef MIDIButton_h
#define MIDIButton_h
//#include <arduino.h>
#include <MIDI.h>
class MIDIButton
{
public:
MIDIButton(int pin, byte command, byte ccNumber, byte value);
void listener();
protected:
private:
const int _pin;
byte _ccNumber, _ccValue;
bool btnState, btnLast;
void ccToggle(byte _ccNumber, byte _ccValue);
};
#endif
MIDIButton.cpp
/*
MIDIButton.cpp - Library for button behavior.
Created by Luca Troncone, 13th May 2020.
Released into the public domain.
*/
//#include <arduino.h>
#include <MIDI.h>
#include "MIDIButton.h"
#define debounceTime 5
MIDI_CREATE_DEFAULT_INSTANCE();
MIDIButton::MIDIButton(int pin, byte command, byte ccNumber, byte value)
{
pinMode(pin, INPUT);
int _pin = pin;
byte _command = command;
byte _ccNumber = ccNumber;
byte _ccValue = value;
}
void MIDIButton::listener(){
btnState = digitalRead(_pin);
if (btnState != btnLast) {
delay(debounceTime);
if (btnState == LOW && btnLast == HIGH) {
// button press
ccToggle(_ccNumber, _ccValue);
} else {
// button release
}
btnLast = btnState;
}
}
void MIDIButton::ccToggle(byte ccNumber, byte ccValue)
{
if (ccValue == 0) {
ccValue = 127;
} else {
ccValue=0;
}
//MIDI.sendControlChange(this->_ccNumber, this->_ccValue)
}
Why this happens? At the moment I keep all the files in the same folder... this is my firs experience with an Arduino project with more than a file.
Please explain in the easiest possible way due to my poor english and C++ knowledge.
Every help will be appretiated!