Code works alone but not in bigger project?

Hi. I have a simple project which is a MIDI pedal with 8 switches and 8 LED's.
This all works fine, no problems.

I want to add a Control Pedal to send MIDI control messages, so I found on Youtube an explanation and basic .ino to make it work.

The odd thing is, the Basic.ino compiles happily on its own, but when I copy it into the relevant places in the Main.ino it then fails, on things that are fine in the Basic version!

For example :
In the Basic.ino
potentiometers()
is fine.

But after copying into Main.ino I get an error
"error: 'potentiometers' cannot be used as a function potentiometers();"

And I have no idea why.
All the parts that work in Basic.ino are included in Main.ino in the correct place, so why does it not work?

I'd be very grateful for any help!

Please post your sketch that errors, using code tags when you do

Embarrassingly, you forgot to share them :wink:

Yes, sorry for not posting the files initially, I hadn't found the 'upload' button!
So here we go...
I'm probably missing one simple thing...

My original BassFX.ino (no pots) runs perfectly on Micro boards as my Bass pedalboard.
I'm simply trying to add a 'Midi CC' input pot on pin A1 - the POTS .ino file.

Here is the basic .ino I used as a CC template and it Verifies ok.
I didn't Upload to the board tho, but the code is happy.

MIDI_Controller_Micro.ino (5.4 KB)

This is the main .ino I copied the relevant bits of the above file into, which as I said works perfectly in its original 'No Pots' version as my Bass pedalboard, but now I'm trying to add a
Midi CC control pedal to it.

BassFXPedal_POTS.ino (13.4 KB)
And now it won't Verify, with the error in the first post, and I'm stumped.

Sorry I'm not a 'real' programmer, and the solution is probably really simple, so thanks in advance!

You need to post the code in code tags. above. Also the verbose error log.

Posts crossed....

Sorry! Is this what you meant?

/*
  Based on Sketch built by Gustavo Silveira (aka Music Nerd)
  Modified by Dolce Wang

  This code is only for Arduinos that use ATmega32u4 (like Micro, Pro Micro, Leonardo...)
  Remember to also assign the correct board in the IDE (like Tools / Boards / Sparkfun AVR / Pro Micro...)

*/

// Change any fields with //**


// LIBRARY

#include "MIDIUSB.h"  

/* BUTTONS
const int NButtons = 1; //***  total number of buttons
const int buttonPin[NButtons] = {2, 3, 5, 7}; //*** define Digital Pins connected from Buttons to Arduino; (ie {10, 16, 14, 15, 6, 7, 8, 9, 2, 3, 4, 5}; 12 buttons)
                                            
int buttonCState[NButtons] = {};        // stores the button current value
int buttonPState[NButtons] = {};        // stores the button previous value

      
// debounce
unsigned long lastDebounceTime[NButtons] = {0};  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    //** the debounce time; increase if the output flickers
*/

// POTENTIOMETERS
const int NPots = 1; //*** total number of pots (knobs and faders)
const int potPin[NPots] = {A1}; //*** define Analog Pins connected from Pots to Arduino; Leave nothing in the array if 0 pots {}
int potCState[NPots] = {0}; // Current state of the pot; delete 0 if 0 pots
int potPState[NPots] = {0}; // Previous state of the pot; delete 0 if 0 pots
int potVar = 0; // Difference between the current and previous state of the pot

int midiCState[NPots] = {0}; // Current state of the midi value; delete 0 if 0 pots
int midiPState[NPots] = {0}; // Previous state of the midi value; delete 0 if 0 pots

const int TIMEOUT = 300; //* Amount of time the potentiometer will be read after it exceeds the varThreshold
const int varThreshold = 10; //* Threshold for the potentiometer signal variation
boolean potMoving = true; // If the potentiometer is moving
unsigned long PTime[NPots] = {0}; // Previously stored time; delete 0 if 0 pots
unsigned long timer[NPots] = {0}; // Stores the time that has elapsed since the timer was reset; delete 0 if 0 pots


// MIDI Assignments 
byte midiCh = 1; //* MIDI channel to be used
byte note = 36; //* Lowest note to be used; 36 = C2; 60 = Middle C
byte cc = 1; //* Lowest MIDI CC to be used


// SETUP
void setup() {

  // Baud Rate
  // 31250 for MIDI class compliant | 115200 for Hairless MIDI

  // Buttons
  // Initialize buttons with pull up resistors
  /*
  for (int i = 0; i < NButtons; i++) {
    pinMode(buttonPin[i], INPUT_PULLUP);
  }
*/
}

////
// LOOP
void loop() {

 // buttons();
  potentiometers();

}

/*
////
// BUTTONS
void buttons() {

  for (int i = 0; i < NButtons; i++) {

    buttonCState[i] = digitalRead(buttonPin[i]);  // read pins from arduino

    if ((millis() - lastDebounceTime[i]) > debounceDelay) {

      if (buttonPState[i] != buttonCState[i]) {
        lastDebounceTime[i] = millis();

        if (buttonCState[i] == LOW) {

          // Sends the MIDI note ON 
          
         // use if using with ATmega32U4 (micro, pro micro, leonardo...)
          noteOn(midiCh, note + i, 127);  // channel, note, velocity
          MidiUSB.flush();


        }
        else {

          // Sends the MIDI note OFF accordingly to the chosen board

          // use if using with ATmega32U4 (micro, pro micro, leonardo...)
          noteOn(midiCh, note + i, 0);  // channel, note, velocity
          MidiUSB.flush();

        }
        buttonPState[i] = buttonCState[i];
      }
    }
  }
}
*/


////
// POTENTIOMETERS
void potentiometers() {


  for (int i = 0; i < NPots; i++) { // Loops through all the potentiometers

    potCState[i] = analogRead(potPin[i]); // reads the pins from arduino

    midiCState[i] = map(potCState[i], 0, 1023, 0, 127); // Maps the reading of the potCState to a value usable in midi

    potVar = abs(potCState[i] - potPState[i]); // Calculates the absolute value between the difference between the current and previous state of the pot

    if (potVar > varThreshold) { // Opens the gate if the potentiometer variation is greater than the threshold
      PTime[i] = millis(); // Stores the previous time
    }

    timer[i] = millis() - PTime[i]; // Resets the timer 11000 - 11000 = 0ms

    if (timer[i] < TIMEOUT) { // If the timer is less than the maximum allowed time it means that the potentiometer is still moving
      potMoving = true;
    }
    else {
      potMoving = false;
    }

    if (potMoving == true) { // If the potentiometer is still moving, send the change control
      if (midiPState[i] != midiCState[i]) {

        // Sends  MIDI CC 
        // Use if using with ATmega32U4 (micro, pro micro, leonardo...)
        controlChange(midiCh, cc + i, midiCState[i]); //  (channel, CC number,  CC value)
        MidiUSB.flush();

        potPState[i] = potCState[i]; // Stores the current reading of the potentiometer to compare with the next
        midiPState[i] = midiCState[i];
      }
    }
  }
}


// if using with ATmega32U4 (micro, pro micro, leonardo...)


// Arduino MIDI functions MIDIUSB Library
/*
void noteOn(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOn);
}

void noteOff(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOff);
}
*/

void controlChange(byte channel, byte control, byte value) {
  midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
  MidiUSB.sendMIDI(event);
}

and the main .ino

// Copyright Louie "Wake The Tiger" 2023 
// with edits by Will Hitchings
// Sketch for a Midi Pedal to control FX in a PC
//  8 SWITCHES
//  8 LED's
//  1 POTENTIOMETER
//  = = = CHECK 'boards.txt' ! ! !
// 
// 

#include "MIDIUSB.h"

// MIDI Notes to Control LEDs
#define LED1_NOTE 41 // 
#define LED2_NOTE 42 // 
#define LED3_NOTE 43 // 
#define LED4_NOTE 44 //
#define LED5_NOTE 45 // 
#define LED6_NOTE 46 // 
#define LED7_NOTE 47 // 
#define LED8_NOTE 48 //



// Buttons/Switches to MIDI Notes
#define BTN1_NOTE 51 //
#define BTN2_NOTE 52 // 
#define BTN3_NOTE 53 // 
#define BTN4_NOTE 54 // 
#define BTN5_NOTE 55 //
#define BTN6_NOTE 56 // 
#define BTN7_NOTE 57 // 
#define BTN8_NOTE 58 // 

const int ButtonPins[8] = {1,2,3,4,5,6,7,8}; // 
const int LEDPins[8] = {9,10,11,12,17,14,15,16}; // 
const int PotPins[1] = {A1}; 

#define deviceChannel 0 // This is the MIDI channel number 0-15
#define buttonDebounceTime 150 // Debounce time for each button in milliseconds (prevents multiple triggers due to chatter)


// POTS VERSION 2
//
// const int NPots = 1; //*** total number of pots (knobs and faders)
const int PotPins[NPots] = {A1}; //*** define Analog Pins connected from Pots to Arduino; Leave nothing in the array if 0 pots {}
int potCState[NPots] = {0}; // Current state of the pot; delete 0 if 0 pots
int potPState[NPots] = {0}; // Previous state of the pot; delete 0 if 0 pots
int potVar = 0; // Difference between the current and previous state of the pot

int midiCState[NPots] = {0}; // Current state of the midi value; delete 0 if 0 pots
int midiPState[NPots] = {0}; // Previous state of the midi value; delete 0 if 0 pots

const int TIMEOUT = 300; //* Amount of time the potentiometer will be read after it exceeds the varThreshold
const int varThreshold = 10; //* Threshold for the potentiometer signal variation
boolean potMoving = true; // If the potentiometer is moving
unsigned long PTime[NPots] = {0}; // Previously stored time; delete 0 if 0 pots
unsigned long timer[NPots] = {0}; // Stores the time that has elapsed since the timer was reset; delete 0 if 0 pots

int potentiometers;

// MIDI Assignments 
byte midiCh = 1; //* MIDI channel to be used
byte note = 36; //* Lowest note to be used; 36 = C2; 60 = Middle C
byte cc = 1; //* Lowest MIDI CC to be used


// --------------------------------------------------------

bool InternalLED_state = LOW;

bool buttonState[8] = {0,0,0,0,0,0,0,0};
unsigned long buttonToggleTime[8] = {0,0,0,0,0,0,0,0};

//-----------------------------------------------------

void setup() {




  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, InternalLED_state); // Turn LED On
  
  for (int i=0; i<8; i++){ 
       pinMode(ButtonPins[i], INPUT_PULLUP);// For All buttons
       pinMode(LEDPins[i], OUTPUT);// For All LED buttons
  }

   
  for (int i=0; i<8; i++){ 
      digitalWrite(LEDPins[i], HIGH); // Turn LED On
      delay(100); // Do this one-by-one
  }
      delay(300); // Wait
  for (int i=0; i<8; i++){ 
      digitalWrite(LEDPins[i], LOW); // Turn LED Off
      delay(100); // Do this one-by-one
  }
}

//-----------------------------------------------------

void loop() {

// POTS VERSION 1
//
// Serial.println(potValue); 
// Serial.println(potState);
// delay(potDelayTime);

// potValue = analogRead(potPin);
// potValue = map(potValue, 0, 1023, 0, 276);

// POTS VERSION 2
//

potentiometers();

  midiEventPacket_t rx;
  rx = MidiUSB.read(); // Check for Message
  if (rx.header != 0);  // If message Received
      // printMessage(rx); 
      // toggleOnBoardLED(); // FOR DEBUGGING -  CAN BE REMOVED - WH added ;



// POTS VERSION 1
//
// for (int i = 0; i < N_POTS; i++);
//  potValue = analogRead(potPin);

// POTS VERSION 2
//
////
// POTENTIOMETERS
void potentiometers() 


  for (int i = 0; i < NPots; i++) { // Loops through all the potentiometers

    potCState[i] = analogRead(potPin[i]); // reads the pins from arduino

    midiCState[i] = map(potCState[i], 0, 1023, 0, 127); // Maps the reading of the potCState to a value usable in midi

    potVar = abs(potCState[i] - potPState[i]); // Calculates the absolute value between the difference between the current and previous state of the pot

    if (potVar > varThreshold) { // Opens the gate if the potentiometer variation is greater than the threshold
      PTime[i] = millis(); // Stores the previous time
    }

    timer[i] = millis() - PTime[i]; // Resets the timer 11000 - 11000 = 0ms

    if (timer[i] < TIMEOUT) { // If the timer is less than the maximum allowed time it means that the potentiometer is still moving
      potMoving = true;
    }
    else {
      potMoving = false;
    }

    if (potMoving == true) { // If the potentiometer is still moving, send the change control
      if (midiPState[i] != midiCState[i]) {

        // Sends  MIDI CC 
        // Use if using with ATmega32U4 (micro, pro micro, leonardo...)
        controlChange(midiCh, cc + i, midiCState[i]); //  (channel, CC number,  CC value)
        MidiUSB.flush();

        potPState[i] = potCState[i]; // Stores the current reading of the potentiometer to compare with the next
        midiPState[i] = midiCState[i];
      }
    }
  } // FOR END


  

      //------------------------- TURN LED's ON
      //-------------------------
      if (rx.byte1 >> 4 == 0x9){ // If it's a note ON message
        switch(rx.byte2){  // Check which Note is Played
          case LED1_NOTE:
                digitalWrite(LEDPins[0], HIGH); // Turn LED On
                break;
          case LED2_NOTE:
                digitalWrite(LEDPins[1], HIGH); // Turn LED On
                break;
          case LED3_NOTE:
                digitalWrite(LEDPins[2], HIGH); // Turn LED On
                break;
          case LED4_NOTE:
                digitalWrite(LEDPins[3], HIGH); // Turn LED On
                break;
          case LED5_NOTE:
                digitalWrite(LEDPins[4], HIGH); // Turn LED On
                break;
          case LED6_NOTE:
                digitalWrite(LEDPins[5], HIGH); // Turn LED On
                break;
          case LED7_NOTE:
                digitalWrite(LEDPins[6], HIGH); // Turn LED On
                break;
          case LED8_NOTE:
                digitalWrite(LEDPins[7], HIGH); // Turn LED On
                break;
          
        }
      }
      //------------------------- TURN LED's OFF
      //------------------------- 
      if (rx.byte1 >> 4 == 0x8){ // If it's a note OFF message
                switch(rx.byte2){  // Check which Note is Played
          case LED1_NOTE:
                delay(1000);
                digitalWrite(LEDPins[0], LOW); // Turn LED Off
                break;
          case LED2_NOTE:
                delay(1000);
                digitalWrite(LEDPins[1], LOW); // Turn LED Off
                break;
          case LED3_NOTE:
                delay(1000);
                digitalWrite(LEDPins[2], LOW); // Turn LED Off
                break;
          case LED4_NOTE:
                delay(1000);
                digitalWrite(LEDPins[3], LOW); // Turn LED Off
                break;
          case LED5_NOTE:
                delay(1000);
                digitalWrite(LEDPins[4], LOW); // Turn LED Off
                break;
          case LED6_NOTE:
                delay(1000);
                digitalWrite(LEDPins[5], LOW); // Turn LED Off
                break;
          case LED7_NOTE:
                delay(1000);
                digitalWrite(LEDPins[6], LOW); // Turn LED Off
                break;
          case LED8_NOTE:
                delay(1000);
                digitalWrite(LEDPins[7], LOW); // Turn LED Off
                break;
        }
      }
      // END If message Received



  //-------------------------
   // CHECK BUTTONS
  for (int i=0; i<8; i++){ // For all Eight buttons
      if (millis() < buttonToggleTime[i] + buttonDebounceTime){ // If it was recently changed
        continue;                                               // Skip this button. Do next one
      }
    
      if (buttonState[i] == 0){ // If Button is Not Pressed Already 
          if (digitalRead(ButtonPins[i]) == 0){ // Pin is Low, button is pressed
              // toggleOnBoardLED(); // FOR DEBUGGING -  CAN BE REMOVED - WH added ;
              buttonState[i] = 1; // Mark as pressed
              buttonToggleTime[i] = millis(); // Update Event Time
              switch(i){  // Pick which Note it is (from button ID)
                case 0: 
                    noteOn(deviceChannel, BTN1_NOTE, 127); // Channel, Note, Velocity
                    break;
                case 1: 
                    noteOn(deviceChannel, BTN2_NOTE, 127); // Channel, Note, Velocity
                    break;
                case 2: 
                    noteOn(deviceChannel, BTN3_NOTE, 127); // Channel, Note, Velocity
                    break;
                case 3: 
                    noteOn(deviceChannel, BTN4_NOTE, 127); // Channel, Note, Velocity
                    break;
                 case 4: 
                    noteOn(deviceChannel, BTN5_NOTE, 127); // Channel, Note, Velocity
                    break;
                case 5: 
                    noteOn(deviceChannel, BTN6_NOTE, 127); // Channel, Note, Velocity
                    break;
                case 6: 
                    noteOn(deviceChannel, BTN7_NOTE, 127); // Channel, Note, Velocity
                    break;
                case 7: 
                    noteOn(deviceChannel, BTN8_NOTE, 127); // Channel, Note, Velocity
                    break;
                 
              } // End Switch
          } // End Button Read
      } // End IF (Button Not Already Pressed)
      
      else{ // Button Already Pressed (Button State == 1)
      
          if (digitalRead(ButtonPins[i]) == 1){ // Pin is High, button is released              
                //toggleOnBoardLED(); // FOR DEBUGGING -  CAN BE REMOVED - WH added ;
                buttonState[i] = 0; // Mark as released
                buttonToggleTime[i] = millis(); // Update Event Time

                // MIDI NOTE OFF MESSAGES FOR BUTTONS
                switch(i){  // Pick which Note it is (from button ID)
                    case 0: 
                        noteOff(deviceChannel, BTN1_NOTE, 0); // Channel, Note, Velocity
                        break;
                    case 1: 
                        noteOff(deviceChannel, BTN2_NOTE, 0); // Channel, Note, Velocity
                        break;
                    case 2: 
                        noteOff(deviceChannel, BTN3_NOTE, 0); // Channel, Note, Velocity
                        break;
                    case 3: 
                        noteOff(deviceChannel, BTN4_NOTE, 0); // Channel, Note, Velocity
                        break;
                    case 4: 
                        noteOff(deviceChannel, BTN5_NOTE, 0); // Channel, Note, Velocity
                        break;
                    case 5: 
                        noteOff(deviceChannel, BTN6_NOTE, 0); // Channel, Note, Velocity
                        break;
                    case 6: 
                        noteOff(deviceChannel, BTN7_NOTE, 0); // Channel, Note, Velocity
                        break;
                    case 7: 
                        noteOff(deviceChannel, BTN8_NOTE, 0); // Channel, Note, Velocity
                        break;
               
                } // End Switch
              
          }// End Button Read
      } // End Else (Button Already Pressed)

      
  }// End IF all eight buttons
  
 
} // END LOOP 

//void toggleOnBoardLED(void){
//    InternalLED_state = !InternalLED_state; // Toggle LED state - DEBUGGING
//    digitalWrite(LED_BUILTIN, InternalLED_state); // set internal LED - DEBUGGING
//}

//-----------------------------------------------------


void printMessage (midiEventPacket_t data){ // Not USED - For testing - Won't work with only one USB port (i.e. Micro)
      Serial.print("Received: ");
      Serial.print(data.header, HEX); //First parameter is the event type (0x09 = note on, 0x08 = note off).combined with the channel.
      Serial.print("-");
      Serial.print(data.byte1, HEX);
      Serial.print("-");
      Serial.print(data.byte2, HEX);
      Serial.print("-");
      Serial.println(data.byte3, HEX);
}



// --------------------------------------------------------

// First parameter is the event type (0x09 = note on, 0x08 = note off).
// Second parameter is note-on/note-off, combined with the channel.
// Channel can be anything between 0-15. Typically reported to the user as 1-16.
// Third parameter is the note number (48 = middle C).
// Fourth parameter is the velocity (64 = normal, 127 = fastest).


// POTS VERSION 2
//
void controlChange(byte channel, byte control, byte value) {
  midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
  MidiUSB.sendMIDI(event);
  }
///

void noteOn(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOn);
  MidiUSB.flush();// Send note now
  }
//

void noteOff(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOff);
  MidiUSB.flush(); // Send note now
  }
//

Hope this works ok!

Yes, that puts the code inline and presents it in a format that can be easily read by anyone wanting to contribute. Thank you for doing that.

I actually think its the opposite, you have something in excess....

You first have a global variable declared like so:

int potentiometers;

Then, further down you have a function declared (also in the global context) with the same name like so:

void potentiometers()

It can't be both at the same time, so the compiler first seen the int and then complains when it finds a function of the same name in the same context. I think you need to change the name of one of those entities. It would have been helpful to see the error output as per sonofcy to confirm this.

So with a bit of stabbing in the dark it finally verified :

// Copyright Louie "Wake The Tiger" 2023 
// with edits by Will Hitchings
// Sketch for a Midi Pedal to control FX in a PC
//  8 SWITCHES
//  8 LED's
//  1 POTENTIOMETER
//  = = = CHECK 'boards.txt' ! ! !
// 
// 

#include "MIDIUSB.h"

// MIDI Notes to Control LEDs
#define LED1_NOTE 41 // 
#define LED2_NOTE 42 // 
#define LED3_NOTE 43 // 
#define LED4_NOTE 44 //
#define LED5_NOTE 45 // 
#define LED6_NOTE 46 // 
#define LED7_NOTE 47 // 
#define LED8_NOTE 48 //



// Buttons/Switches to MIDI Notes
#define BTN1_NOTE 51 //
#define BTN2_NOTE 52 // 
#define BTN3_NOTE 53 // 
#define BTN4_NOTE 54 // 
#define BTN5_NOTE 55 //
#define BTN6_NOTE 56 // 
#define BTN7_NOTE 57 // 
#define BTN8_NOTE 58 // 

const int ButtonPins[8] = {1,2,3,4,5,6,7,8}; // 
const int LEDPins[8] = {9,10,11,12,17,14,15,16}; // 

#define deviceChannel 0 // This is the MIDI channel number 0-15
#define buttonDebounceTime 150 // Debounce time for each button in milliseconds (prevents multiple triggers due to chatter)


// POTS VERSION 2
//
const int NPots = 1; //*** total number of pots (knobs and faders)
const int potPin[NPots] = {A1}; //*** define Analog Pins connected from Pots to Arduino; Leave nothing in the array if 0 pots {}
int potCState[NPots] = {0}; // Current state of the pot; delete 0 if 0 pots
int potPState[NPots] = {0}; // Previous state of the pot; delete 0 if 0 pots
int potVar = 0; // Difference between the current and previous state of the pot

int midiCState[NPots] = {0}; // Current state of the midi value; delete 0 if 0 pots
int midiPState[NPots] = {0}; // Previous state of the midi value; delete 0 if 0 pots

const int TIMEOUT = 300; //* Amount of time the potentiometer will be read after it exceeds the varThreshold
const int varThreshold = 10; //* Threshold for the potentiometer signal variation
boolean potMoving = true; // If the potentiometer is moving
unsigned long PTime[NPots] = {0}; // Previously stored time; delete 0 if 0 pots
unsigned long timer[NPots] = {0}; // Stores the time that has elapsed since the timer was reset; delete 0 if 0 pots

// int potentiometers();

// MIDI Assignments 
byte midiCh = 1; //* MIDI channel to be used
byte note = 36; //* Lowest note to be used; 36 = C2; 60 = Middle C
byte cc = 1; //* Lowest MIDI CC to be used


// --------------------------------------------------------

bool InternalLED_state = LOW;

bool buttonState[8] = {0,0,0,0,0,0,0,0};
unsigned long buttonToggleTime[8] = {0,0,0,0,0,0,0,0};

//-----------------------------------------------------
//
//
// START of SETUP
//

void setup() {




  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, InternalLED_state); // Turn LED On
  
  for (int i=0; i<8; i++){ 
       pinMode(ButtonPins[i], INPUT_PULLUP);// For All buttons
       pinMode(LEDPins[i], OUTPUT);// For All LED buttons
  }

   
  for (int i=0; i<8; i++){ 
      digitalWrite(LEDPins[i], HIGH); // Turn LED On
      delay(100); // Do this one-by-one
  }
      delay(300); // Wait
  for (int i=0; i<8; i++){ 
      digitalWrite(LEDPins[i], LOW); // Turn LED Off
      delay(100); // Do this one-by-one
  }


}
//
//
// END of SETUP
//


//-----------------------------------------------------
//
//
// START of VOID LOOP
//
//

void loop() {

// POTS VERSION 1
//
// Serial.println(midiCState); 
// Serial.println(potState);
// delay(potDelayTime);
// potValue = analogRead(potPin);
// potValue = map(potValue, 0, 1023, 0, 127);




// POTS VERSION 2
//

potentiometers();

  midiEventPacket_t rx;
  rx = MidiUSB.read(); // Check for Message
  if (rx.header != 0);  // If message Received
      // printMessage(rx); 
      // toggleOnBoardLED(); // FOR DEBUGGING -  CAN BE REMOVED - WH added ;



// POTS VERSION 1
//
// for (int i = 0; i < N_POTS; i++);
//  potValue = analogRead(potPin);

// POTS VERSION 2
//
////
// POTENTIOMETERS

{
{
      //------------------------- TURN LED's ON
      //-------------------------
      if (rx.byte1 >> 4 == 0x9){ // If it's a note ON message
        switch(rx.byte2){  // Check which Note is Played
          case LED1_NOTE:
                digitalWrite(LEDPins[0], HIGH); // Turn LED On
                break;
          case LED2_NOTE:
                digitalWrite(LEDPins[1], HIGH); // Turn LED On
                break;
          case LED3_NOTE:
                digitalWrite(LEDPins[2], HIGH); // Turn LED On
                break;
          case LED4_NOTE:
                digitalWrite(LEDPins[3], HIGH); // Turn LED On
                break;
          case LED5_NOTE:
                digitalWrite(LEDPins[4], HIGH); // Turn LED On
                break;
          case LED6_NOTE:
                digitalWrite(LEDPins[5], HIGH); // Turn LED On
                break;
          case LED7_NOTE:
                digitalWrite(LEDPins[6], HIGH); // Turn LED On
                break;
          case LED8_NOTE:
                digitalWrite(LEDPins[7], HIGH); // Turn LED On
                break;
          
        }
      }
      //------------------------- TURN LED's OFF
      //------------------------- 
      if (rx.byte1 >> 4 == 0x8){ // If it's a note OFF message
                switch(rx.byte2){  // Check which Note is Played
          case LED1_NOTE:
                delay(1000);
                digitalWrite(LEDPins[0], LOW); // Turn LED Off
                break;
          case LED2_NOTE:
                delay(1000);
                digitalWrite(LEDPins[1], LOW); // Turn LED Off
                break;
          case LED3_NOTE:
                delay(1000);
                digitalWrite(LEDPins[2], LOW); // Turn LED Off
                break;
          case LED4_NOTE:
                delay(1000);
                digitalWrite(LEDPins[3], LOW); // Turn LED Off
                break;
          case LED5_NOTE:
                delay(1000);
                digitalWrite(LEDPins[4], LOW); // Turn LED Off
                break;
          case LED6_NOTE:
                delay(1000);
                digitalWrite(LEDPins[5], LOW); // Turn LED Off
                break;
          case LED7_NOTE:
                delay(1000);
                digitalWrite(LEDPins[6], LOW); // Turn LED Off
                break;
          case LED8_NOTE:
                delay(1000);
                digitalWrite(LEDPins[7], LOW); // Turn LED Off
                break;
        }
      }
      // END If message Received



  //-------------------------
   // CHECK BUTTONS
  for (int i=0; i<8; i++){ // For all Eight buttons
      if (millis() < buttonToggleTime[i] + buttonDebounceTime){ // If it was recently changed
        continue;                                               // Skip this button. Do next one
      }
    
      if (buttonState[i] == 0){ // If Button is Not Pressed Already 
          if (digitalRead(ButtonPins[i]) == 0){ // Pin is Low, button is pressed
              // toggleOnBoardLED(); // FOR DEBUGGING -  CAN BE REMOVED - WH added ;
              buttonState[i] = 1; // Mark as pressed
              buttonToggleTime[i] = millis(); // Update Event Time
              switch(i){  // Pick which Note it is (from button ID)
                case 0: 
                    noteOn(deviceChannel, BTN1_NOTE, 127); // Channel, Note, Velocity
                    break;
                case 1: 
                    noteOn(deviceChannel, BTN2_NOTE, 127); // Channel, Note, Velocity
                    break;
                case 2: 
                    noteOn(deviceChannel, BTN3_NOTE, 127); // Channel, Note, Velocity
                    break;
                case 3: 
                    noteOn(deviceChannel, BTN4_NOTE, 127); // Channel, Note, Velocity
                    break;
                 case 4: 
                    noteOn(deviceChannel, BTN5_NOTE, 127); // Channel, Note, Velocity
                    break;
                case 5: 
                    noteOn(deviceChannel, BTN6_NOTE, 127); // Channel, Note, Velocity
                    break;
                case 6: 
                    noteOn(deviceChannel, BTN7_NOTE, 127); // Channel, Note, Velocity
                    break;
                case 7: 
                    noteOn(deviceChannel, BTN8_NOTE, 127); // Channel, Note, Velocity
                    break;
                 
              } // End Switch
          } // End Button Read
      } // End IF (Button Not Already Pressed)
      
      else{ // Button Already Pressed (Button State == 1)
      
          if (digitalRead(ButtonPins[i]) == 1){ // Pin is High, button is released              
                //toggleOnBoardLED(); // FOR DEBUGGING -  CAN BE REMOVED - WH added ;
                buttonState[i] = 0; // Mark as released
                buttonToggleTime[i] = millis(); // Update Event Time

                // MIDI NOTE OFF MESSAGES FOR BUTTONS
                switch(i){  // Pick which Note it is (from button ID)
                    case 0: 
                        noteOff(deviceChannel, BTN1_NOTE, 0); // Channel, Note, Velocity
                        break;
                    case 1: 
                        noteOff(deviceChannel, BTN2_NOTE, 0); // Channel, Note, Velocity
                        break;
                    case 2: 
                        noteOff(deviceChannel, BTN3_NOTE, 0); // Channel, Note, Velocity
                        break;
                    case 3: 
                        noteOff(deviceChannel, BTN4_NOTE, 0); // Channel, Note, Velocity
                        break;
                    case 4: 
                        noteOff(deviceChannel, BTN5_NOTE, 0); // Channel, Note, Velocity
                        break;
                    case 5: 
                        noteOff(deviceChannel, BTN6_NOTE, 0); // Channel, Note, Velocity
                        break;
                    case 6: 
                        noteOff(deviceChannel, BTN7_NOTE, 0); // Channel, Note, Velocity
                        break;
                    case 7: 
                        noteOff(deviceChannel, BTN8_NOTE, 0); // Channel, Note, Velocity
                        break;
               
                } // End Switch
              
          }// End Button Read
      } // End Else (Button Already Pressed)

      
  }// End IF all eight buttons
  
 
}
}
//
//
//
} // END of VOID LOOP 
//



//void toggleOnBoardLED(void){
//    InternalLED_state = !InternalLED_state; // Toggle LED state - DEBUGGING
//    digitalWrite(LED_BUILTIN, InternalLED_state); // set internal LED - DEBUGGING
//}


//-----------------------------------------------------

//
//
//
// START of THE FINAL VOID
//

void potentiometers() 

{
  for (int i = 0; i < NPots; i++) { // Loops through all the potentiometers

    potCState[i] = analogRead(potPin[i]); // reads the pins from arduino

    midiCState[i] = map(potCState[i], 0, 1023, 0, 127); // Maps the reading of the potCState to a value usable in midi

    potVar = abs(potCState[i] - potPState[i]); // Calculates the absolute value between the difference between the current and previous state of the pot

    if (potVar > varThreshold) { // Opens the gate if the potentiometer variation is greater than the threshold
      PTime[i] = millis(); // Stores the previous time
    }

    timer[i] = millis() - PTime[i]; // Resets the timer 11000 - 11000 = 0ms

    if (timer[i] < TIMEOUT) { // If the timer is less than the maximum allowed time it means that the potentiometer is still moving
      potMoving = true;
    }
    else {
      potMoving = false;
    }

    if (potMoving == true) { // If the potentiometer is still moving, send the change control
      if (midiPState[i] != midiCState[i]) {

        // Sends  MIDI CC 
        // Use if using with ATmega32U4 (micro, pro micro, leonardo...)
        controlChange(midiCh, cc + i, midiCState[i]); //  (channel, CC number,  CC value)
        MidiUSB.flush();

        potPState[i] = potCState[i]; // Stores the current reading of the potentiometer to compare with the next
        midiPState[i] = midiCState[i];
      }
    }
  } 
  }// FOR END


  


void printMessage (midiEventPacket_t data){ // Not USED - For testing - Won't work with only one USB port (i.e. Micro)
      Serial.print("Received: ");
      Serial.print(data.header, HEX); //First parameter is the event type (0x09 = note on, 0x08 = note off).combined with the channel.
      Serial.print("-");
      Serial.print(data.byte1, HEX);
      Serial.print("-");
      Serial.print(data.byte2, HEX);
      Serial.print("-");
      Serial.println(data.byte3, HEX);
}



// --------------------------------------------------------

// First parameter is the event type (0x09 = note on, 0x08 = note off).
// Second parameter is note-on/note-off, combined with the channel.
// Channel can be anything between 0-15. Typically reported to the user as 1-16.
// Third parameter is the note number (48 = middle C).
// Fourth parameter is the velocity (64 = normal, 127 = fastest).


// POTS VERSION 2
//
void controlChange(byte channel, byte control, byte value) {
  midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
  MidiUSB.sendMIDI(event);
  }
///

void noteOn(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOn);
  MidiUSB.flush();// Send note now
  }
//

void noteOff(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOff);
  MidiUSB.flush(); // Send note now
  }


//
//
// END of THE FINAL VOID
//

Next is upload and test it.

Thanks for the pointers folks!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.