0

I have a DIY midi-foot-pedal for my home studio, with two buttons programmed to send out signals to the midi output, which my DAW then receives and interprets as a MIDI controller.

Everything is working as it is now, but now all of a sudden Arduino IDE began finding problems in my code, and because of that I can't either make any edits to the code to add more functionality.

This is the code (that works flawlessly in the Arduino Uno by the way):

// define pins
const int switchPin1 = 2;
const int switchPin2 = 3;

// midi notes
char note1 = 60; //Middle C
char note2 = 62; //D

// variables
int switchState1 = LOW;
int switchState2 = LOW;
int currentSwitchState1 = LOW;
int currentSwitchState2 = LOW;

void setup() {
  // set the states of the I/O pins:
  pinMode(switchPin1, INPUT);
  pinMode(switchPin2, INPUT);

  // set MIDI baud rate :
  Serial.begin(31250);
}

void loop() {
  currentSwitchState1 = digitalRead(switchPin1);
  currentSwitchState2 = digitalRead(switchPin2);

  // button 1 push
  if (currentSwitchState1 == HIGH && switchState1 == LOW) {
    noteOn(0x94, note1, 0x45);
    delay(100);
    noteOn(0x94, note1, 0x00);
    switchState1 = HIGH;
  }
  // button 1 release
  if (currentSwitchState1 == LOW && switchState1 == HIGH) {
    switchState1 = LOW;
  }

  // button 2 push
  if (currentSwitchState2 == HIGH && switchState2 == LOW) {
    noteOn(0x94, note2, 0x45);
    delay(100);
    noteOn(0x94, note2, 0x00);
    switchState2 = HIGH;
  }
  // button 2 release
  if (currentSwitchState2 == LOW && switchState2 == HIGH) {
    switchState2 = LOW;
  }

  void noteOn(char cmd, char data1, char data2) {
    Serial.print(cmd);
    Serial.print(data1); Serial.print(data2);
  }
}

And this is the error log I get:

Arduino: 1.8.15 (Windows Store 1.8.49.0) (Windows 10), Board: "Arduino Uno"



C:\Users\CrazyCringeComplex\Desktop\MIDI Pedal\midipedal\midipedal.ino: In function 'void loop()':

midipedal:46:5: error: 'noteOn' was not declared in this scope

     noteOn(0x94, note1, 0x45);

     ^~~~~~

C:\Users\CrazyCringeComplex\Desktop\MIDI Pedal\midipedal\midipedal.ino:46:5: note: suggested alternative: 'note2'

     noteOn(0x94, note1, 0x45);

     ^~~~~~

     note2

midipedal:58:5: error: 'noteOn' was not declared in this scope

     noteOn(0x94, note2, 0x45);

     ^~~~~~

C:\Users\CrazyCringeComplex\Desktop\MIDI Pedal\midipedal\midipedal.ino:58:5: note: suggested alternative: 'note2'

     noteOn(0x94, note2, 0x45);

     ^~~~~~

     note2

midipedal:68:49: error: a function-definition is not allowed here before '{' token

   void noteOn(char cmd, char data1, char data2) {

                                                 ^

exit status 1

'noteOn' was not declared in this scope

Of what I can tell all of these errors can be traced to that the "function definition is not allowed". From researching the problem on the internet I have found out that this is usually due to improper indentation. Homever, I can't find any missing brackets or similar in my code, and multiple times have I auto-formatted everything.

Can anyone help me with this? Thank you (:

1 Answers1

0

The problem in your code is that you declared the void noteOn() function inside the void loop() function. You should never ever declare a function inside another function. You need to declare the void noteOn() function either before void setup(), or after void loop(). I have fixed it. Here is the working code:

const int switchPin1 = 2;
const int switchPin2 = 3;

// midi notes
char note1 = 60; //Middle C
char note2 = 62; //D

// variables
int switchState1 = LOW;
int switchState2 = LOW;
int currentSwitchState1 = LOW;
int currentSwitchState2 = LOW;


void noteOn(char cmd, char data1, char data2) { // Always declare it before void setup
  Serial.print(cmd);
  Serial.print(data1); Serial.print(data2);
}

void setup() {
  // set the states of the I/O pins:
  pinMode(switchPin1, INPUT);
  pinMode(switchPin2, INPUT);

  // set MIDI baud rate :
  Serial.begin(31250);
}

void loop() {
  currentSwitchState1 = digitalRead(switchPin1);
  currentSwitchState2 = digitalRead(switchPin2);

  // button 1 push
  if (currentSwitchState1 == HIGH && switchState1 == LOW) {
    noteOn(0x94, note1, 0x45);
    delay(100);
    noteOn(0x94, note1, 0x00);
    switchState1 = HIGH;
  }
  // button 1 release
  if (currentSwitchState1 == LOW && switchState1 == HIGH) {
    switchState1 = LOW;
  }

  // button 2 push
  if (currentSwitchState2 == HIGH && switchState2 == LOW) {
    noteOn(0x94, note2, 0x45);
    delay(100);
    noteOn(0x94, note2, 0x00);
    switchState2 = HIGH;
  }
  // button 2 release
  if (currentSwitchState2 == LOW && switchState2 == HIGH) {
    switchState2 = LOW;
  }


}