Pages: [1]   Go Down
Author Topic: MIDI Shield buttons  (Read 287 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 142
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have this: https://www.sparkfun.com/products/9595? 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:

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:

Code:
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
Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1010
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 142
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Fantastic! thanks for the explanation.  I tried this:

Code:
  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: 
Code:
  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?
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1188
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

So press the button and see what it does!
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

0
Offline Offline
Full Member
***
Karma: 0
Posts: 142
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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
Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1010
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: January 22, 2013, 12:23:28 am by Krodal » Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 142
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Thanks for all of the help!

Loren
Logged

Pages: [1]   Go Up
Jump to: