Noob needs help combining 2 small programs

Hey all, Im totally new to programming, so i assume there is some small formatting error i am committing, but i cant figure out what it is.

I am taking 2 working programs and trying to combine in to 1.

I am trying to set up a Recording Light that will turn on when I start recording audio in Logic Pro on my mac. The goal is to make a device that will listen to a MIDI channel and turn on the light when the tone is sent, this part works with this method:

and the code for the MIDI input test program found here:

that works fairly well so far, but now i want to add a button to just turn the light on manually when i want as well.. Like a manual override switch. so i am trying to add the program from the examples; 02.digital: Button

I have tried an number of ways but i can get past the error message:

'MyHandleNoteOn' was not declared in this scope

but i dont think i have changed anything too cause this to no longer work its seems like just a formating thing.

Here is my most recent attempt to combine these, i would really appreciate if someone could tell me where i am going wrong.

#include <MIDI.h>  // Add Midi Library

int LED = 13;    // Arduino Board LED is on Pin 13
int buttonPin = 2;
int buttonState = 0;

//Create an instance of the library with default name, serial port and settings
MIDI_CREATE_DEFAULT_INSTANCE();



void setup() {
  pinMode (LED, OUTPUT); // Set Arduino board pin 13 to output
  pinMode (buttonPin, INPUT);
  
  MIDI.begin(2); // Initialize the Midi Library.
  // OMNI sets it to listen to all channels.. MIDI.begin(2) would set it 
  // to respond to notes on channel 2 only.
  MIDI.setHandleNoteOn(MyHandleNoteOn); // This is important!! This command
  // tells the Midi Library which function you want to call when a NOTE ON command
  // is received. In this case it's "MyHandleNoteOn".
  MIDI.setHandleNoteOff (MyHandleNoteOff); // This command tells the Midi Library 
  // to call "MyHandleNoteOff" when a NOTE OFF command is received.

}


void loop() 
{ // Main loop
 MIDI.read(); // Continuously check if Midi data has been received.
  
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin); 

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(LED, HIGH);
  } else {

  
// MyHandleNoteON is the function that will be called by the Midi Library
// when a MIDI NOTE ON message is received.
// It will be passed bytes for Channel, Pitch, and Velocity
void MyHandleNoteOn(byte channel, byte pitch, byte velocity) { 
  digitalWrite(LED,HIGH);  //Turn LED on
}

// MyHandleNoteOFF is the function that will be called by the Midi Library
// when a MIDI NOTE OFF message is received.
// * A NOTE ON message with Velocity = 0 will be treated as a NOTE OFF message *
// It will be passed bytes for Channel, Pitch, and Velocity
void MyHandleNoteOff(byte channel, byte pitch, byte velocity) { 
  digitalWrite(LED,LOW);  //Turn LED off
}

Here is the starting MIDI codes that works on its own:

#include <MIDI.h>  // Add Midi Library

#define LED 13    // Arduino Board LED is on Pin 13



//Create an instance of the library with default name, serial port and settings
MIDI_CREATE_DEFAULT_INSTANCE();

void setup() {
  pinMode (LED, OUTPUT); // Set Arduino board pin 13 to output
  MIDI.begin(2); // Initialize the Midi Library.
  // OMNI sets it to listen to all channels.. MIDI.begin(2) would set it 
  // to respond to notes on channel 2 only.
  MIDI.setHandleNoteOn(MyHandleNoteOn); // This is important!! This command
  // tells the Midi Library which function you want to call when a NOTE ON command
  // is received. In this case it's "MyHandleNoteOn".
  MIDI.setHandleNoteOff(MyHandleNoteOff); // This command tells the Midi Library 
  // to call "MyHandleNoteOff" when a NOTE OFF command is received.
}

void loop() { // Main loop
  MIDI.read(); // Continuously check if Midi data has been received.
}

// MyHandleNoteON is the function that will be called by the Midi Library
// when a MIDI NOTE ON message is received.
// It will be passed bytes for Channel, Pitch, and Velocity
void MyHandleNoteOn(byte channel, byte pitch, byte velocity) { 
  digitalWrite(LED,HIGH);  //Turn LED on
}

// MyHandleNoteOFF is the function that will be called by the Midi Library
// when a MIDI NOTE OFF message is received.
// * A NOTE ON message with Velocity = 0 will be treated as a NOTE OFF message *
// It will be passed bytes for Channel, Pitch, and Velocity
void MyHandleNoteOff(byte channel, byte pitch, byte velocity) { 
  digitalWrite(LED,LOW);  //Turn LED off
}

And here is the button code that works:

/*
  Button

 Turns on and off a light emitting diode(LED) connected to digital
 pin 13, when pressing a pushbutton attached to pin 2.


 The circuit:
 * LED attached from pin 13 to ground
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground

 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.


 created 2005
 by DojoDave <http://www.0j0.org>
 modified 30 Aug 2011
 by Tom Igoe

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/Button
 */

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

You can not define a function within another function (loop in this case)

You will have to take this out.

void loop()
{
  ...
  ...

    void MyHandleNoteOn(byte channel, byte pitch, byte velocity) {
      digitalWrite(LED, HIGH); //Turn LED on
    }

  ...
  ...
}

And place it on it's own

void loop()
{
  ...
  ...
}

void MyHandleNoteOn(byte channel, byte pitch, byte velocity)
{
  digitalWrite(LED, HIGH); //Turn LED on
}

Same applies to MyHandleNoteOff.

And next call it in loop()

void loop()
{ // Main loop
  MIDI.read(); // Continuously check if Midi data has been received.

  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(LED, HIGH);
  } else {
    MyHandleNoteOn(channelVariable, pitchVariable, velocityVariable);
    ...
  }
}

You can either use variables or constants in the call above.

This Simple Merge Demo may help.

...R

thanks for the help guys, needed some help understanding the answers, but i am learning and getting closer to the functions i want.

my current problem i belive to be an order of operations issue, I have tried a few ways but none seem to give the desired results.

depending, on what i do i either get the button to work but not the midi,
or the midi works, and the button turns on, but not off.

is there a way i can make it so the button only turns it off if the midi has not turned it on?

I appreciate any help. and dont be afraid to over simplify.

here is the current code with a couple variations // greyed out.. with this one the midi works, and the button turns on but not off.

#include <MIDI.h>  // Add Midi Library

int LED = 13;    // Arduino Board LED is on Pin 13
int buttonPin = 2;
int buttonState = 0;

//Create an instance of the library with default name, serial port and settings
MIDI_CREATE_DEFAULT_INSTANCE();

void MyHandleNoteOn(byte channel, byte pitch, byte velocity) { 

  switch (pitch) {
  
 case 25:
  digitalWrite(LED,HIGH);  //Turn LED on
}

}
void MyHandleNoteOff(byte channel, byte pitch, byte velocity) { 
 switch (pitch) {
  
 case 25:
  
  digitalWrite(LED,LOW);  //Turn LED off
}
}

void setup() {
  pinMode (LED, OUTPUT); // Set Arduino board pin 13 to output
  pinMode (buttonPin, INPUT);
  
  MIDI.begin(2); // Initialize the Midi Library.
  // OMNI sets it to listen to all channels.. MIDI.begin(2) would set it 
  // to respond to notes on channel 2 only.
  MIDI.setHandleNoteOn(MyHandleNoteOn); // This is important!! This command
  // tells the Midi Library which function you want to call when a NOTE ON command
  // is received. In this case it's "MyHandleNoteOn".
  MIDI.setHandleNoteOff (MyHandleNoteOff); // This command tells the Midi Library 
  // to call "MyHandleNoteOff" when a NOTE OFF command is received.

}

void loop() 
{ // Main loop
 //MIDI.read(); 
           // Continuously check if Midi data has been received.  
          // ?moved to "IF" statment?
 
  buttonState = digitalRead(buttonPin); 
  if (buttonState == HIGH) {
    digitalWrite(LED, HIGH);
  }

//if (buttonState == LOW) {
  //digitalWrite(LED, LOW);
 // }

          // when enabled, disabled midi triggering

  if (buttonState == LOW) {
  MIDI.read();
  }


}

You want the LED on pin 13 to be lit when the midi signal is detected and you want it the LED to be off if no midi signal is detected. Additionally, if the LED is not already on, you want a press of the button to switch it on. If the LED is on and was previously switched on by a button press, you want the next button press to switch the LED off.
Is that it ?

6v6gt:
You want the LED on pin 13 to be lit when the midi signal is detected and you want it the LED to be off if no midi signal is detected. Additionally, if the LED is not already on, you want a press of the button to switch it on. If the LED is on and was previously switched on by a button press, you want the next button press to switch the LED off.
Is that it ?

yes this is essentially correct. but the button i am using is more like a switch so it locks on (depressed in) or lock off (out). so not looking to press it once, for off and again for on, it is either flipped on or off.

so when the switch is on i want the light on, when the switch is off i want it to default to the last midi command ( either NoteOn or NoteOff)

You can try this (untested) :

#include <MIDI.h>  // Add Midi Library

#define LED 13    // Arduino Board LED is on Pin 13

boolean midiOn = false ;
int buttonPin = 2;
int buttonState = LOW ;



//Create an instance of the library with default name, serial port and settings
MIDI_CREATE_DEFAULT_INSTANCE();

void setup() {
  pinMode (LED, OUTPUT); // Set Arduino board pin 13 to output
  MIDI.begin(2); // Initialize the Midi Library.
  // OMNI sets it to listen to all channels.. MIDI.begin(2) would set it
  // to respond to notes on channel 2 only.
  MIDI.setHandleNoteOn(MyHandleNoteOn); // This is important!! This command
  // tells the Midi Library which function you want to call when a NOTE ON command
  // is received. In this case it's "MyHandleNoteOn".
  MIDI.setHandleNoteOff(MyHandleNoteOff); // This command tells the Midi Library
  // to call "MyHandleNoteOff" when a NOTE OFF command is received.
}

void loop() { // Main loop
  MIDI.read(); // Continuously check if Midi data has been received.
  buttonState = digitalRead(buttonPin);

  if ( midiOn == true || buttonState == HIGH ) {
     digitalWrite(LED, HIGH);
  }
  else {
     digitalWrite(LED, LOW);
  }
}

// MyHandleNoteON is the function that will be called by the Midi Library
// when a MIDI NOTE ON message is received.
// It will be passed bytes for Channel, Pitch, and Velocity
void MyHandleNoteOn(byte channel, byte pitch, byte velocity) {
  // digitalWrite(LED,HIGH);  //Turn LED on
  midiOn = true; 
}

// MyHandleNoteOFF is the function that will be called by the Midi Library
// when a MIDI NOTE OFF message is received.
// * A NOTE ON message with Velocity = 0 will be treated as a NOTE OFF message *
// It will be passed bytes for Channel, Pitch, and Velocity
void MyHandleNoteOff(byte channel, byte pitch, byte velocity) {
  // digitalWrite(LED,LOW);  //Turn LED off
  midiOn = false ;
}

Oh nice thanks, that seems to do it, will just take me a wile for me to understand why/how...

In principle, it works by holding the current status of the midi signal in a variable midiOn.
If midiOn is true or the switch is on, the LED is on otherwise the LED is off.

current version works, here is a video and the most recent code:

code:

#include <MIDI.h>                      // Add Midi Library

int LED = 13;                         // Arduino Board LED is on Pin 13
int acRelay = 12;                    //AC realy switch is on pin 12
int buttonPin = 2;                   // power button in to pin 2 
int buttonState = 0;                // a varable to hold the power button state-last read starts set at low or 0
int resetButtonPin = 3;             // reset button in to pin 3
int resetState = 0;                // a varable to hold the reset button state-last read starts set at low or 0
boolean resetIsOn = false;         // create vareable to hold reset state 
boolean switchIsOn = false;         // create vareable to hold power switch state 
boolean recIsOn = false;            // create vareable to hold midi record state
boolean trackIsOn = false;          // create vareable to hold midi track enabled state

int redLED = 9;                    // red led on pin 9
int yellowLED = 10;               // yellow LED on pin 10
int greenLED = 11;                // green led on pin 11



//Create an instance of the library with default name, serial port and settings
MIDI_CREATE_DEFAULT_INSTANCE();

void MyHandleNoteOn(byte channel, byte pitch, byte velocity) { 
  switch (pitch) {                // when midi note on signal sent
      case 25:                    // logic record midi signal sent with pitch 25
          recIsOn = true;         // when logic send (note on pitch 25) recIsOn var set to true
          break;                  // required for switch instruction
    
      case 24:                    // logic track rec enabled midi signal sent with pitch 24
          trackIsOn = true;       // when logic send (note on pitch 24) trackIsOn var set to true
          break;                   // required for switch instruction
      default:                     
          break;                   // required for switch instruction
  }
}

void MyHandleNoteOff(byte channel, byte pitch, byte velocity) {
    switch (pitch) {               // when midi note off signal sent
        case 25:                  // logic end record midi signal sent with pitch 25
            recIsOn = false;       // when logic send (note off pitch 25) recIsOn var set to false
            break;                // required for switch instruction

                
       case 24:                     // logic track stop rec enabled midi signal sent with pitch 24
            trackIsOn = false;       // when logic send (note off pitch 24) trackIsOn var set to false
            break;                   // required for switch instruction
       default:
            break;                  // required for switch instruction

            
    }
}

void setup() {

  Serial.begin(9600);               //start serial monitor
  
  pinMode (LED, OUTPUT);              // Set Arduino board pin 13 to output
  pinMode (acRelay, OUTPUT);          // Set Arduino board pin 12 to output
  pinMode (buttonPin, INPUT);         // Set Arduino board pin 2 to input
  pinMode (resetButtonPin, INPUT);    // Set Arduino board pin 3 to input
  pinMode (redLED, OUTPUT);           // Set Arduino board pin 09 to output
  pinMode (yellowLED, OUTPUT);        // Set Arduino board pin 10 to output
  pinMode (greenLED, OUTPUT);         // Set Arduino board pin 11 to output

  
  MIDI.begin(2);                       // Initialize the Midi Library.
                                      // OMNI sets it to listen to all channels.. MIDI.begin(2) would set it 
                                      // to respond to notes on channel 2 only.
  MIDI.setHandleNoteOn(MyHandleNoteOn);   
                                      // This is important!! This command
                                      // tells the Midi Library which function you want to call when a NOTE ON command
                                       // is received. In this case it's "MyHandleNoteOn".
  MIDI.setHandleNoteOff (MyHandleNoteOff); 
                                       // This command tells the Midi Library 
                                      // to call "MyHandleNoteOff" when a NOTE OFF command is received.

}

void loop() 
{
    MIDI.read();                      // read midi signal

    
    buttonState = digitalRead(buttonPin); 
                                     // read powre button pin 2 set state as button pin         
    
    if (buttonState == HIGH) {      
        switchIsOn = true;            // when power button pressed switchIsOn is set to true
    } else if (buttonState == LOW) {
        switchIsOn = false;            // when power button released switchIsOn is set to false
    }    
    
    if (recIsOn || switchIsOn)        // if midi rec ligt is on or the powere switch is on do the folowing 
        {
            digitalWrite(LED, HIGH);    //turn on pin 13 led
            digitalWrite(acRelay, LOW);   //turn on ac relay switch
            digitalWrite(redLED, LOW);    // turn off red led
            digitalWrite(greenLED, HIGH); // turn on green LED
        }
    else                                    // if midi record is off and power switch is off do the folowing
        {
            digitalWrite(LED,LOW);          // turn off pin 13 led
            digitalWrite(acRelay,HIGH);     // turn off ac realy switch 
            digitalWrite(redLED,HIGH);      // turn red led on
            digitalWrite(greenLED, LOW);    // turn green led off
        }



      if (trackIsOn == true)                // if ligic track is record enabled, do this
      {
          digitalWrite(yellowLED, HIGH);    // turn on yellow led
          digitalWrite(redLED, HIGH);       // turn on red led
       
      }

      if (trackIsOn == false)               //if NO ligic track is record enabled, do this
      {
          digitalWrite(yellowLED, LOW);      // turn off yellow led
          digitalWrite(redLED, LOW);         // turn off red led
       
      }

    resetState = digitalRead(resetButtonPin);    //red pin 3, set as "resetState"
      if (resetState == HIGH) {                 // if reset button bressed do this
      trackIsOn = false;                        // set trackIsOn var to false, 
        
      }


     
}