Go Down

Topic: Using additional serial ports of 2560 for USB keyboard and MIDI control (Read 736 times) previous topic - next topic

cowasaki

I'm building a musical control device which I want to be able to connect to a computer so will be using the standard TX & RX pins for the normal USB comms so that the arduino can talk to a desktop program BUT I am also wanting to connect to a MIDI keyboard and control the computer as if a USB keyboard (to start/stop/rewind DAW etc)....

Now MIDI....   Looking at the MIDI example it seems very much a straight forward job, just change serial to serial1

so......

Code: [Select]
void setup() {
  //  Set MIDI baud rate:
  Serial1.begin(31250);
}

void loop() {
  // play notes from F#-0 (0x1E) to F#-5 (0x5A):
  for (int note = 0x1E; note < 0x5A; note ++) {
    //Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
    noteOn(0x90, note, 0x45);
    delay(100);
    //Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
    noteOn(0x90, note, 0x00);   
    delay(100);
  }
}

//  plays a MIDI note.  Doesn't check to see that
//  cmd is greater than 127, or that data values are  less than 127:
void noteOn(int cmd, int pitch, int velocity) {
  Serial1.write(cmd);
  Serial1.write(pitch);
  Serial1.write(velocity);
}




Now I need to control an application running on the computer using the arduino pretending to be a USB keyboard.....

now this the main issue at the moment......

Code: [Select]
const int buttonPin = 2;          // input pin for pushbutton
int previousButtonState = HIGH;   // for checking the state of a pushButton
int counter = 0;                  // button push counter

void setup() {
  // make the pushButton pin an input:
  pinMode(buttonPin, INPUT);
  // initialize control over the keyboard:
  Keyboard.begin();
}

void loop() {
  // read the pushbutton:
  int buttonState = digitalRead(buttonPin);
  // if the button state has changed,
  if ((buttonState != previousButtonState)
    // and it's currently pressed:
  && (buttonState == HIGH)) {
    // increment the button counter
    counter++;
    // type out a message
    Keyboard.print("You pressed the button ");
    Keyboard.print(counter);
    Keyboard.println(" times.");
  }
  // save the current button state for comparison next time:
  previousButtonState = buttonState;
}



How do I make Keyboard use Serial2 instead of serial0

PaulS

Quote
How do I make Keyboard use Serial2 instead of serial0

That would all depend on what Keyboard is, wouldn't it? That's why snippets don't cut it.

If Keyboard is a library, you'll need to modify it to use a different Serial instance. You'll also need to figure out how to connect that Serial instance to the device that is on the receiving end.

Only the Serial instance is connected to the USB to serial converter chip/USB port.

cowasaki


Quote
How do I make Keyboard use Serial2 instead of serial0

That would all depend on what Keyboard is, wouldn't it? That's why snippets don't cut it.

If Keyboard is a library, you'll need to modify it to use a different Serial instance. You'll also need to figure out how to connect that Serial instance to the device that is on the receiving end.

Only the Serial instance is connected to the USB to serial converter chip/USB port.


Paul,

I was going to connect another FTDI chip to serial port 2, just as the original is connected to port 0 or am I barking up the wrong tree?


DOH!

Just realised that the "Keyboard" command is not supported on the 2560...... reverting to plan B

PaulS


cowasaki


Quote
reverting to plan B

That being a Leonardo?


No plan B was using a USB keyboard and messing with the mapping but since typing "plan B" I have just starting on "plan C" and I am searching on ebay for a Leonardo :)

cowasaki

As this is a one off build for my own use and not to build for other people or several times I have decided on using a 2560 as the main controller which will be connected to the desktop computer, GLCD, SD card reader etc etc with a Leonardo as an add on board controlling the keyboard I/O..... Probably not the most elegant solution but at least it will work.

cowasaki

Hmmm....

What about plan D......

Active USB to PS2 keyboard adapter then emulate PS2 keyboard........  That is just serial data isn't it?  Will check library :D

Go Up