Microcontrollers and sensor input with Sonic PI

I’ve been playing around with using Arduino to send midi data into Sonic Pi. Here is something I did with 3 potentiometers to play with the parameters of the bitcrusher fx. There are so many possibilities that I can see using this for: fx parameters, changing notes, random seeds, moving through arrays, subdivisions…

(Can anyone tell me what I need to do to get the video from this tweet to appear here instead of having to click on the link?)

Continuing work with #Arduino and @Sonic_Pi now up to 3 potentiometers sending midi control change data to effect bits, cutoff & sample rate for bitcrusher fx. great way to get a feel for fx capabilities pic.twitter.com/gCeTCz0eLq

— Mister Bomb (@mrbombmusic) February 18, 2018

Here’s the code:

live_loop :cutz do
  use_real_time
  a, bb = sync "/midi/iac_driver_bus_1/0/3/control_change"
  set :cut, bb
end

live_loop :bitz do
  use_real_time
  c, dd = sync "/midi/iac_driver_bus_1/0/2/control_change"
  set :bit, dd
end

live_loop :samp do
  use_real_time
  e, ff = sync "/midi/iac_driver_bus_1/0/1/control_change"
  set :sam, ff
end

live_loop :octs do
  use_real_time
  x = get[:bit]
  y = get[:cut]
  z = get[:sam]
  puts y," " , x,"  ", z*1000
  
  with_fx :bitcrusher, bits: x, cutoff: y, sample_rate: z*1000 do
    use_synth :dsaw
    play (octs, :e1, 3).choose
    sleep 0.25
  end
end

If anyone wants to see the Arduino code, let me know and I’ll throw it up here too.

I’m interested to see if anyone else out there has played around with using sensor input to control various parameters in SPi. I realize that using a Raspberry Pi probably makes more sense, but I have more experience with Arduino and at this point, setting up my Raspberry Pi is a bit more trouble than it is worth as I have to put it away when I’m done to avoid little hands messing with it.
But I would love to see or hear about anything being down with microcontrollers.

5 Likes

Hello there,

If it’s possible, could you please include the Arduino code? I’m very interested in cross communication between Arduino and Sonic Pi.

Thank you!

I’m interested as well, this is right up my alley/

Here’s the Arduino code. I learned it from an course on Udemy called Making Music With Arduino. Worth checking out if your into that kind of things. Shows how to do a lot with sending MIDI using Arduino.

#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();

const int NPots = 3;
int potPin[NPots] = {A0, A1, A2};

int potCState[NPots] = {0};
int potPState[NPots] = {0};
int potVar = 0;

int midiCState[NPots] = {0};
int midiPState[NPots] = {0};

int timeout = 300;
int varThreshold = 3;
boolean potMoving = true;
unsigned long pTime[NPots] = {0};
unsigned long timer[NPots] = {0};

byte midiCh = 1;
byte note = 36;
byte cc = 1;

void setup() {
Serial.begin(115200);

}

void loop() {

for (int i = 0; i < NPots; i++) {
  potCState[i] = analogRead(potPin[i]);
  midiCState[0] = map(potCState[0], 0, 1023, 0 , 44); //Mapped to Sample rate
  midiCState[1] = map(potCState[1], 0, 1023, 0 , 16); // Mapped to Bits
  midiCState[2] = map(potCState[2], 0, 1023, 0 , 127); // Mapped to Cutoff

  potVar = abs(potCState[i] - potPState[i]);

  if(potVar > varThreshold) {
pTime[i] = millis();
  }
  timer[i] = millis() - pTime[i];

  if(timer[i] < timeout) {
potMoving = true;
  }
  else {
potMoving = false;
  }
  if(potMoving == true) {
    if(midiPState[i] != midiCState[i]) {
    //  Serial.println(midiCState);
    MIDI.sendControlChange(cc + i, midiCState[i], midiCh + i);
      potPState[i] = potCState[i];
      midiPState[i] = midiCState[i];
    }
  }
}
}

Be aware that in order to get the Arduino talking with Sonic Pi you need a midi serial bridge program called “Hairless Midi” : http://projectgus.github.io/hairless-midiserial/

3 Likes

Thanks for this, just the sort of thing I needed to get my nanokontrol tweaking values. My boys are gonna be psyched when they get back from school. :slight_smile:

1 Like

Ooh, this is lovely.

Useful to know in this context: Bare Conductive’s Touch Board is an off-the-shelf 12-channel capacitance/touch sensor, Arduino and MIDI controller board. I’ve previously used one to drive GarageBand, but interaction with Sonic Pi would be awesome.

2 Likes

interesting… well time to bring out my old arduino uno and try doing this

1 Like

Hi, could you please link the course that you’re referring to? I can’t find it on Udemy…

I couldn’t find it offered on Udemy anymore but here is a link to the course on the website of the person who developed and taught the course on Udemy: Making Music with Arduino – The Nerd Musician

I also see that he has a full tutorial on Youtube that seems to cover similar content but looks like it is using a different board than the Uno. Full disclosure: I have not watched this video so I can’t speak to any actual content covered in it, but it is the same guy who did the Udemy course and I found that course very helpful and informative.

I will also say that since I had made this post, I found the arduino MKRZero board which is much easier to set up in regards to being used to send MIDI data. Here’s a link to a post I did for a project I made using that board with Sonic Pi: Pringles Can Drums w/ Arduino

2 Likes

Thanks for replying! Really appreciate it.

Hey! I’m getting back to using arduino after long and and am facing issues using midi.h and wire.h libraries simultaneously. I need wire.h for I2C connection (sensor to track motion). Would love to hear your thoughts on how to proceed.