How to change the function of a button, with a press of another button?

Hey guys,

Hope you can help me with this one.

Say I have BUTTON_1 set up to send MIDI noteOn messages when pressed and MIDI noteOff messages when released. That's all good and straight forward.

My question is; when I press BUTTON_2, this will change the function of BUTTON_1.

Say BUTTON_1 will now send MIDI CC messages when pressed.

Is this possible? If you guys could kindly point me in the right direction so I may read on it.

Thank you very much.

Sure. Say you had an array that you read, which stored a value that was read out and sent over MIDI for every message. You could have a duplicate array other values that was read instead if button2 was pressed. if (digitalRead(button2) == 0){ // read from array 1 } else { // read from array 2 }

or read button 2 and save the value, then:

if (button2State == 0){ // read from array 1 } else { // read from array 2 }

Lot of ways to accomplish this.

bdumaguina: My question is; when I press BUTTON_2, this will change the function of BUTTON_1.

Say BUTTON_1 will now send MIDI CC messages when pressed.

Is this possible?

Define a variable which indicates what BUTTON_1 is supposed to do. Update that variable as needed in response to inputs from BUTTON_2. When handling BUTTON_1, use the value of that variable to control what action is taken.

Thank you very much Sir for your swift response. I guess I’ll have to read on arrays. Couldn’t wrap my head around that one.
I’ve gone this far though.

#include <MIDI.h>
#include <midi_Defs.h>
#include <midi_Namespace.h>
#include <midi_Settings.h>

//Variables for Button 1 - sending MIDI noteOn/noteOff
const int buttonPin = 14;
int buttonState = 0;
int lastButtonState = 0;
int ledPin = 9;

//Variables for Button 2 - sending MIDI CC
const int buttonPin2 = 15;
int buttonState2 = 0;
int midiCCselect2 = 23;
int ledPin2 = 8;
boolean lastButtonState2 = LOW;
boolean currentButton2 = LOW;
boolean ledOn = false;

//Variables for Button 3 - supposed switching program button
const int buttonPin3 = 16;

void setup() {
  pinMode(ledPin,OUTPUT);
  pinMode(ledPin2,OUTPUT);
  
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin2, INPUT);

  analogWrite(9,64);

  MIDI.begin(); 
  Serial.begin(38400);  
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(buttonPin2);
  if(last!=current)
  {
    delay(5);
    current = digitalRead(buttonPin2);
  }
  return current;
}

void loop()
{
//buttonPin sends MIDI noteOn and noteOff messages
  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState)
  {
    if (buttonState == HIGH)
    {
      MIDImessage(144,36,120);//turn note on
      analogWrite(9,255);  
    }
    else
    {
      MIDImessage(128,36,0);//turn note off
      analogWrite(9,64);
    }
  }
  lastButtonState = buttonState;

//buttonPin2 sends MIDI CC messages
  currentButton2 = debounce(lastButtonState2);
  if (lastButtonState2 == LOW && currentButton2 == HIGH)
  {
    ledOn = !ledOn;
    midiCC(0xB0,midiCCselect2,127);//send/trigger MIDI CC 23 on channel 1
  }
  lastButtonState2 = currentButton2; 
  digitalWrite(ledPin2,ledOn);
  
}

//send MIDI CC
void midiCC(char CC_data, char c_num, char c_val)
{ 
  Serial.write(CC_data); // send MIDI channel
  Serial.write(c_num); // send MIDI CC number
  Serial.write(c_val); //send analog value
}
  
  
//send MIDI message
void MIDImessage(int command, int MIDInote, int MIDIvelocity) 
{
  Serial.write(command);//send note on or note off command 
  Serial.write(MIDInote);//send pitch data
  Serial.write(MIDIvelocity);//send velocity data
}

The code works fine. Edited code already available on the internet. Aside from arrays, what would be another way to achieve this?

Will update this thread should I run into some trouble with arrays. Thank you Sir.

Define a variable which indicates what BUTTON_1 is supposed to do. Update that variable as needed in response to inputs from BUTTON_2. When handling BUTTON_1, use the value of that variable to control what action is taken. [/quote]

Good Sir, Thank you for your suggestion. I will also look into defining variable. But if you could point me out to an example where I may learn more about this, I would greatly appreciate it.

Thank you.

Define a set of int const values representing the different things you want the button to do. Define an int variable to indicate what the current function is. Change the function by assigning a different value to the variable. To execute the current function use a switch/case statement to execute the code associated with the current value of the variable. See the Arduino online reference for an introduction to these terms, and look for C++ tutorials on the internet if you want more comprehensive tutorials.

Thank you for your help. I think I have it figured out already.