A bit obscure but I've reached a wall.
I am trying to send data from a Raspberry pi across a serial connection to an Arduino uno. For the simple test I am sending a random integer on the pi to play a midi note on the Arduino. I set up a simple random python script to test. I managed to encode and send bytes across the serial with this helpful tutorial with newline breaks successfully at 9600 baud. Serial.read and Arduino: Here's the advanced stuff you should know
All this looks fine in the serial monitor. Each number displays on a new line clearly.
Now the issue is some kind of conflict with the Midi library. (#include <MIDI.h)MIDI Library - Arduino Reference
It seems to change the baud rate to 31250 automatically although you can’t see it.
I am able to convert the data over the serial to an integer (thisPitch) to put in MIDI.sendNoteOn(thisPitch,127,1);
but the midi library seems to corrupt the data coming across the serial.
I’ve tried to update the python script and the Arduino to 31250 baud but still no luck. It all seems simple in theory, but maybe I can’t use the midi library with other serial communications. Are there other ones people have tried this?
Does anyone know of any way to monitor output at 31250 baud on an Arduino uno
The serial monitor doesn’t have that preset.
I monitor the breadboard 8 pin midi output to USB using Ableton live. Other note outputs from the midi library work fine.
Here is the python code on the Pi which simply sends a random number across the serial connection.
import random
import threading
import time
import serial
def infiniteloop1():
while True:
pnote = (random.randint(0,255))
print(pnote)
ser = serial.Serial('/dev/ttyACM0',9600, timeout=1)
ser.flush()
ser.write(b"pnote\n")
print('pnote sent')
time.sleep(5)
thread1 = threading.Thread(target=infiniteloop1)
thread1.start()
Here is the Code on the Arduino that takes the random number via serial and converts it to midi using the library and turns on a light when looping:
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
#define blueLed 10
void setup() {
// put your setup code here, to run once:
//Serial.begin(9600);
MIDI.begin();
pinMode(blueLed, OUTPUT);
}
void loop() {
// read serial
int sensorReading = analogRead(A0);
Serial.println(sensorReading);
int thisPitch = map(sensorReading, 400,1000,120,1500);
tone(9, thisPitch, 10);
digitalWrite(blueLed, HIGH);
// send midi
MIDI.sendNoteOn(thisPitch,127,1); //turn midi note on variable, velocity 127, midi channel 1.
delay(1000);
MIDI.sendNoteOff(thisPitch,127,1); //turn midi note off variable, velocity 127, midi channel 1.
digitalWrite(blueLed, LOW);
delay(100 );
//delay(1);
}