MIDI Shield buttons

I have this: SparkFun MIDI Shield - DEV-12898 - SparkFun Electronics? midi shield connected to my arduino. I decided to start simple with just a button or two. All of the simple button examples show a button sending a digital input high, but the shield I'm using connects the digital pin to ground when the switch is closed which confused me. So I looked at the example code and tried to pull out the bits that are pertinent to just utilizing two of the three buttons.

Here are the pertinent portions of the setup code:

#define BUTTON1  2
#define BUTTON2  3


#define STAT1  7
#define STAT2  6



void setup() {

  Serial.begin(9600);
  pinMode(STAT1,OUTPUT);   
  pinMode(STAT2,OUTPUT);

  pinMode(BUTTON1,INPUT);
  pinMode(BUTTON2,INPUT);
 

  digitalWrite(BUTTON1,HIGH);
  digitalWrite(BUTTON2,HIGH);
  

  for(int i = 0;i < 10;i++) // flash MIDI Sheild LED's on startup
  {
    digitalWrite(STAT1,HIGH);  
    digitalWrite(STAT2,LOW);
    delay(30);
    digitalWrite(STAT1,LOW);  
    digitalWrite(STAT2,HIGH);
    delay(30);
  }
  digitalWrite(STAT1,HIGH);   
  digitalWrite(STAT2,HIGH);
  
}

In the main loop the example code uses the buttons like this:

if(button(BUTTON1) || button(BUTTON2) || button(BUTTON3))
  {  
    Midi_Send(0x90,note,0x45);
    while(button(BUTTON1) || button(BUTTON2) || button(BUTTON3));
  }

Can someone explain to me what is going on with the buttons in the main loop? This is the part that I don't get. I think that If I understood this I could get my sketch to react to a button push.

Thanks for stopping by.

Loren

The button() is a function that return 'true' if the button is pressed. That function is located at the bottom of the example file.

if ( onething || anotherthing || thirdcondition) is a test to see if any of the conditions is true.

You can send a Serial.writeln("button pressed"); if your button is pressed. If that is okay, you can implement that in the button() function.

I think that the example file is for buttonss that connect the input to ground if the button is pressed. So if that is like your buttons, you don't have to change it.

Fantastic! thanks for the explanation. I tried this:

  if (button(BUTTON1)) {

      Serial.println("on");

    }

That didn't print any results. So I decided to try and read the digital pin and print that out. When I did this:

  buttonState = digitalRead(BUTTON1);
Serial.println(buttonState);

the serial monitor reads a continuous string of 1s
I would think that it would change to zero if I pushed the button.

Thoughts?

I would think that it would change to zero if I pushed the button.

So press the button and see what it does!

marco_c:

I would think that it would change to zero if I pushed the button.

So press the button and see what it does!

I did nothing happend

Check the button with a multimeter. Some switches have 4 pins, and I always use the wrong pins. Check the input of the Arduino with a multimeter, does it change from (almost) 5V to 0V ?
Check if you have selected the right pin number for the button.

It can only be something very simple.
If you don't have a multimeter, try a resistor and led to the 5V at the input. Pressing the button should turn on the led.

It turned out to be the ground was cross patch. Ugg

Thanks for all of the help!

Loren