Newby Needs Some Help

Hey all

Need some help here please, im looking to make a midi foot controller for my effectas box, i have an arduino mega so pins will not be a problem. I have learnt to do the individual processes i.e Press a button, Program change command`sent, light an led & debounce so the led lights when the button is pressed and doesnt miss. i can do this with cc messages to allow extra effects to be togged on while im in a specific program.

i cant seem to get my project to have a pc button and a cc button to work together in the same sketch. i think once i have mastered this the rest will be easier.....

the total project i am trying to undertake is

3 banks - bank one will be for one bands sound, 6 program changes giving me the type of sound and then 18 instant access (cc) switches to toggle in effects and tuner etc bank two will be the same as bank one but with diffrent settings and bank three will be a scrolling and direct select bank, this will allow me to select individual patches using three button presses on switches labled 1 -10 or just scrolling through the presets bank 1- press up - bank 2 etz.

i will be looking a using shift registers to light the led's (again something i have done with one button n one led but nothing on a more than one scale. wither that or multiplexing them so the use fewer pins than they would normally and this will allow me to have an cd disply than is needed also. the answers just a push in the right direction



any help would be gr8 - i have checked out some other projects but dont understad how the work compared to mine.

i do't want tot be spoon fed

What is a pc and a cc switch? I think you mean the pc is a program change switch and the cc switches will change effects within a program. Is that correct?

Start by getting one of the 'banks' to work. The one with scrolling selection is going to be the most difficult so I would start with the more basic. But before you get to that, you seem to need to be able to have more than one switch input to your Mega. So start by using one swtich to turn on your led and one switch to turn it off. Once you have mastered that you wil lbe ready to move on.

Since that is where you are initially stuck, I am assuming you used the basic tutoirals to get the single switch debounce/LED to work. Add a second switch to a second pin and make sure you define that pin as an input. Since I'm not sure how you coded your debounce, you will need to either post it or try to work it out but in general you need to look at each switch each loop. What I would suggest is a series of YouTube video tutorials.I can't do YouTube at work so I can't do a direct link, but if you do a search for sciguy14, he has a series of 14 excellent tutorials on the arduino. In one of the the first couple he covers software debouncing.

If you use this method, you will simply want to add the buttonpin you want to debounce into the call. In other words, I beleive in his tutorial, he simply calls his single button buttonpin. So in his debounce routine, he simply does a digitalRead(buttonpin);

To debounce more than one button, you will need to pass which button you wish to debounce.

He uses two further variables currentbutton and lastbutton. You will need to add to that your second button states to make this work.

So in your debounce function, you will need to add something like void debounce(int buttonpin);

When you look at each button in your loop, you will need to call the debounce and pass it which pin like so:

currentccbutton = debounce(ccbuttonpin); currentpcbutton = debounce(pcbuttonpin);

When you return the state to currentccbutton or currentpcbutton, it will put them either HIGH or LOW. Compare these to the lastccbutton and lastpcbutton respectively to either do something or do nothing.

This will allow you to debounce two buttons.

Initially, I would just have them turn on and off two seperate LED's. sciguy14 has tutorials that will help you along.

I did this from memory so I hope I am making sense.

Once you understand how to make your two buttons work, then its time to move to the next level.


ok so i have been through the tutorials and have written

Moderator: but haven’t figured out how to post code yet

/* version 005 - 4 led's and 4 buttons debouncing independantly.
change of pins to group led's and switches together, this works with my new sketch

next idea is to have 2 cc switches and 2 pc switches awhen the 2 pc switches are pressed all other LED's go off
except for the botton i am pressing. 


#include <MIDI.h>
#include <NewSoftSerial.h>

const int midi_channel = 1; 
int S1 = 50;
int L1 = 46;
int S2 = 51;
int L2 = 47;
int S3 = 52;
int L3 = 44;
int S4 = 53;
int L4 = 45;

boolean lastButton1 = LOW;
boolean currentButton1 = LOW;
boolean lastButton2 = LOW;
boolean currentButton2 = LOW;
boolean lastButton3 = LOW;
boolean currentButton3 = LOW;
boolean lastButton4 = LOW;
boolean currentButton4 = LOW;

boolean L1On = false;
boolean L2On = false;
boolean L3On = false;
boolean L4On = false;

void setup(){
for(int i = 44; i <= 47; i++)//Set up the LED pins and make them float high right off the bat
    pinMode(i, OUTPUT);
    digitalWrite(i, HIGH);
    pinMode(S1, INPUT);
    pinMode(S2, INPUT);
    pinMode(S3, INPUT);
    pinMode(S4, INPUT);

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

boolean debounce1(boolean last) // switch 1
  boolean current1 = digitalRead(S1);
  if (last != current1)
    current1 = digitalRead(S1);
  return current1;

boolean debounce2(boolean last) // Switch 2
  boolean current2 = digitalRead(S2);
  if (last != current2)
    = digitalRead(S2);
  return current2;
boolean debounce3(boolean last) // Switch 3
  boolean current3 = digitalRead(S3);
  if (last != current3)
    current3 = digitalRead(S3);
  return current3;

boolean debounce4(boolean last) // Switch 4
  boolean current4 = digitalRead(S4);
  if (last != current4)
    current4 = digitalRead(S4);
  return current4;

void loop()
  currentButton1 = debounce1(lastButton1);
  if (lastButton1 == LOW && currentButton1 == HIGH)
    L1On = !L1On;
  lastButton1 = currentButton1; // SWITCH 1
  digitalWrite(L1, L1On);
  if (digitalRead(S1) == LOW)
  Serial.print(0xC0);//Program Change
  currentButton2 = debounce2(lastButton2);
  if (lastButton2 == LOW && currentButton2 == HIGH)
    L2On = !L2On;
  lastButton2 = currentButton2; // SWITCH 2
  digitalWrite(L2, L2On);
  currentButton3 = debounce3(lastButton3);
  if (lastButton3 == LOW && currentButton3 == HIGH)
    L3On = !L3On;
  lastButton3 = currentButton3; // SWITCH 3
  digitalWrite(L3, L3On);
  currentButton4 = debounce4(lastButton4);
  if (lastButton4 == LOW && currentButton4 == HIGH)
    L4On = !L4On;
  lastButton4 = currentButton4; // SWITCH 4
  digitalWrite(L4, L4On);

Moderator: CODE TAGS added

so i tried to get it to send a pc message n that work o, i want now when say button one is pressed (thats going to be my pc buttom - all the lileds go out excpt the button im steppig on… i have to do a very simalar thing to about 18 more buttons, is there any way of stream lining the code

thanks for you help

A quick couple of things to clean up your code.

You need to take your input pin assignments out of your for loop.

You don't need separate Debounce functions for your switches. Since you are passing the the data to the function, pass the pin to read as well.

In other words sonething like this:

boolean debounce(boolean last, int pin)  //You are passing the last to the function.

   boolean current1 = digitalRead(pin);
  if (last != current1)
    current1 = digitalRead(pin);
  return current1;

//And in your loop:

currentButton = debounce(lastButton1,S1);


currentButton2 = debounce(lastButton2,S2);