Go Down

Topic: Sending a Midi Note (Read 2270 times) previous topic - next topic

Hello, I have just bought the arduino Uno and I am trying to send a midi note and play with it in Reason (a software to make music) all I have is:
-Serial-Midi Converter Program
-Midi Yoko
-Arduino Uno
-1 button
-Protoboard

The only thing I want is that when I press the button, arduino send a midi note.

I have connected 5V to the button, and the other part of the button (switch) is connected to the digital port 2.

I would really appreciate how can I do that (the code).

Thanks and sorry for my English

marco_c

#1
Nov 03, 2012, 03:26 am Last Edit: Nov 05, 2012, 08:43 am by marco_c Reason: 1
You should break the problem down into component parts. There are clearly 2 things that you need to do:

1. Read a button and register that this has changed from OFF to ON. There is plenty of example code on how to do this. You should consider debouncing the switch so that you do not read false presses (sounds like you have not from the description).

2. Sending the note to the MIDI device. Again, lots of examples on how to do this (look for MIDI in the Playground) using various libraries trhat will do most of the work for you.

The easiest way to learn is to try writing some simple code from existing examples and then asking for more help when you get stuck.

Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

Thanks for the reply, this is my code:

const int button1 = 2;     // the number of the pushbutton pin

int buttonState = 0;       

void setup() {
  pinMode(button1, INPUT);     
  Serial.begin(31250);
}

void loop(){
  buttonState = digitalRead(button1);
  if (buttonState == HIGH) {
    noteOn(0x90, 0x1E, 0x45);
    delay(100);
  }
}


void noteOn(int cmd, int pitch, int velocity) {
  Serial.write(cmd);
  Serial.write(pitch);
  Serial.write(velocity);
}



In the Serial-Midi Converter, I can choose different speed rates: 31250, 38400, 57600, 115200. Which should I use?

Thanks

marco_c

The MIDI standard is 31250.
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

Okey but the code isn't working

Nantonos


I have connected 5V to the button, and the other part of the button (switch) is connected to the digital port 2.


where is your pulldown resistor?

marco_c

#6
Nov 04, 2012, 06:10 am Last Edit: Nov 04, 2012, 10:46 am by marco_c Reason: 1
When you have a switch wired to an input up you need to have a definite electrical signal for both when it is off and when it is on. When you switch the switch on, you are connecting it to 5V and your get a HIGH in the code. If you simply disconnect the switch and do not pull it down to 0V with through a resistor, then you get a floating input that can flip all over the place. Look up the principle for the "pull down" resistor circuit either on this forum or using google.

The same applies for a circuit that is normally at 5V and is switched to low. In this case the resistor is called a pull-up resistor, but the reasoning behind it is the same.
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

robtillaart


you can use the internal pull-down resistor this way

Code: [Select]

void setup()
{
  pinMode(button1, INPUT);     
  digitalWrite(button1, HIGH);
  Serial.begin(31250);
}


Please use the # button to apply [ code] tags to code to make it more readable
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

It was my fault, it was working. By the way I was using a 4.7k resistor.

Other question (As robtillaart said):

void setup()
{
  pinMode(button1, INPUT);     
  digitalWrite(button1, HIGH);
  Serial.begin(31250);
}


if button1 is an input, what does "  digitalWrite(button1, HIGH);" do? If somebody can explain it.

Thanks everybody

marco_c

It turns on an internal pull down resistor when used that way.
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

Grag38


It was my fault, it was working. By the way I was using a 4.7k resistor.

Other question (As robtillaart said):

void setup()
{
  pinMode(button1, INPUT);     
  digitalWrite(button1, HIGH);
  Serial.begin(31250);
}


if button1 is an input, what does "  digitalWrite(button1, HIGH);" do? If somebody can explain it.

Thanks everybody


This a kind of inverse logic.

Doing by this way, you don't need to use an external resistor between the button and the ground.

In that case, if the button is pressed, the value returned by digitalRead value will be LOW. When the button is depressed, the value returned by digitalRead will be HIGH.

hedi

Hi everybody,
i'm aiming to do the same thing as Readytodie and i added the pull down thing.
Yet ,the midi message sent out is incomplete(a status message ?) .Check the picture to see the message received at midi ox.
Thanks.

Go Up