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