converting arduino sketches to run in Pd for Bela
hello! this question intersects with Pd even if it is not about Pd specifically...
I currently have several (installation) pieces in which the sound is run from Pd and then I have motors or sensors being run off other things.
This means that in one case I am using a raspberry pi for the Pd part and an Arduino mega for the motor part
In another case I have Pd running on a Bela/BBB and then an Arduino doing the motor stuff.
The reason for this inefficiency is that my knowledge is fairly limited on these platforms. However, it is annoying to have to use the extra gear when I don't have to. Especially because I know that both the raspberry and the Bela have pins that could be used in place of the arduino for this purpose. Since the Bela is a simpler case and the pins can be addressed directly from Pure Data, I will ask about that and leave the Raspberry behind for now (as I think I would end up running Pd AND Arduino on that board anyway, whereas with Bela, one can easily address the pins from inside Pd without any other programs or libraries).
My question is this: does anyone know how the standard mappings of pins Analog/Digital/and PWM pins on the Arduino lines up on the Bela? Does the Bela have have PWM pins like, for example pins 8,9, 10,11 on Arduino Mega or Dueminanove? And if so, what is the best way to address them from Pure Data? Basically, I'd like to convert my already very simple Arduino sketches over to Pd, so that both motors and Audio can run from the same patch.
I know this isn't specifically a Pd issue but I have asked a related question on the Bela forum but didn't get an answer so I hoped someone might know or be able to suggest a resource over here...
I have traversed the Bela documentation already of course....and will continue with that meanwhile.
Jorge
Comport in browser?
Hi! I'm stuck in a room with a Chromebook and an Arduino. I've got purrdata.glitch.me/ from @cuinjune working in one Chrome tab and Arduino IDE in another tab monitoring the serial input. What would be needed to get comport to read the serial port from the usb/arduino?
I guess the ultimate goal is: what is the lowest latency setup from an Arduino sensor into PD on a chromebook or browser only?
I don't necessarily need the Arduino environment running for my uses, it's just that it looks like it's running an agent that allows the site to access the usb. Thanks for any thoughts!
How to loop/reset an audio file to the beginning
I'm not at the computer now, but I would proceed by steps.
Sensor = 1 play/resume, sensor 0 = stop. Get this very simple specification working first.
Then start the timer on sensor = 0 and be sure that the timer is counting correctly.
Btw I think you can simplify this using the pd [timer] object. Why? Because you need to know the elapsed time only when the sensor was 0 but becomes 1. So there is no need to poll the time continuously. Sensor 0, bang to timer's left inlet, sensor 1 to timer's right inlet.
Then:
- If the sensor goes to 0, the playback rate should change to 0 (pause).
- If the sensor goes to 1, the playback rate should change to 1 (play again).
- Note that these two behaviors do not depend on the timer at all!
- Then, also at the moment when the sensor becomes 1, if the timer shows a long enough period, then you would also send "all" to the left inlet.
hjh
How to loop/reset an audio file to the beginning
I will check it late on my mac computer. for the moment I'm not on my computer, so I don't want to waste much time on it now as I think it will work on my computer.
For the moment I'm using your help file patch which is great. Your abstraction is so much more easy to use then from my patch I shared in the beginning.
what I'm trying to do is the following -
I will have 8 different sound files. Each sound file should be in a individual sf-play2~ object.
I will have data from 8 diffrent sensors I'm reading via arduino. Each sensor will report 0 or 1 (if sensor is pressed or not) alongside with analog data of numbers between 0 - 1023.
What I would like to do is that when I'm reading 1 the audio file will play, when reading 0 the audio file will pause . if 1 is held and the audio file arrived to it ends it will continue from the beginning. (loop 1) . I will also have a clocker to report how much time has elpased since sensor is 0. If more then x seconds the audio file will go back to beginnnig so next time sensor is held (1) it will start from the beginnnig.
here is my try with your abstraction.
the green part is to imitate the on off from the sensor. the blue is the analog data from sensor.
the orange part is my try to make a clocker that will start count when sensor is off. The yellow part is for small envelop to avoide clicks when audio is pause and resume.
How can I make the audio file go to the beginning when the orange part is reporting a bang (that time was elapsed) ?
Also - I'm not sure if the yellow section is working. Meaning if it is indeed making an envelope on every pause and resume ? Is that possible?
Thanks!
edit: the scale of the sensor should be inverted. 0 1023 0 1
edit 2: how can I make the sf-play2~ go back to the beginning of the audio file without starting it immediately?
edit3: how can I load the file to the sf-play2~ object without starting it? if I don't press "all" message it won't load the file. I would lik the file to be loaded to the beginnnig, but to start and play only when receiving data from sensor on off. for the moment if I'm not pressing all message the file won't play.
Arduino->Pd with Open Sound Control
@jameslo Thanks a lot!
I can send messages locally between Pd, Processing and Unity. But not Arduino.
oscProcessing.pd
oscUnity.pd
oscP5sendReceive.pde
These works but I can still not for example replace Processing with Arduino.
@jameslo said in [Arduino->Pd with Open Sound Control] do you need bidirectional messaging? What data are you passing (if any)? What Arduino library would you prefer to use (I see there are more now than when I started)? is it over WiFi or ethernet or something else?
-bidirectional is good. Type of data, analog float, and digital. I have no preference for what library I use. I just want to make it work locally on a single computer.
I would like to use the Arduino as a controller for Pd through OSC.
For example, 5 analog ins on the Arduino that I after som treatment (smoothing and more) send locally to Pd, with custom osc addresses: /myArduino/analog-0
Having lots of switches into Pd
Here is the working Arduino code, (which is basically a straight copy from "project 8", but I wrote it while reading the tutorial)
// setup for 6 analog in och 12 digital in (pullup to be added).
//intended as experimental template. use pd patch
//made with tutorial Arduino for Pd'ers, project 8
byte myArray[25];
void setup()
{
for(int i = 2; i < 14; i++)
pinMode(i, INPUT);
Serial.begin(9600);
}
void loop()
{
myArray[0] = 0xc0;
int index = 1;
for(int i = 0; i < 6; i++){
unsigned int knob = analogRead (i);
myArray[index++] = knob & 0x007f;
myArray[index++] = knob >> 7;
}
for(int i = 2; i < 14; i++)
myArray[index++] = digitalRead(i);
Serial.write(myArray, 25);
}
Then I tried to incorporate project 1 (blink):
// set a variable to hold the ledddddddz pin number
int led = 13;
void setup()
{
// st pin 13 as output, to light up the LED
// whenever it is told so from Pd
pinMode(led, OUTPUT);
// start the serial communication o the Arduino
// and Pd can communicate with each other
Serial.begin(9600);
}
void loop()
{
while(Serial.available()){
byte ledState = Serial.read() - '0';
digitalWrite(led, ledState);
}
}
but when I tried to edit "blink" to work with several outputs and merge this in the project 8 code, I decided that I do not have enough understanding of the arduino code to pull it through. What is needed is, I think, another "for()" combined with digitalWrite() inside void loop()
Note: My plan now is to (still) use several Arduinos but with your code and abstractions (instead of the Pdunio). I will wait with the matrix switch setup since I have to take this in steps in order to have control of it.
This means that I am up and running, unless I want to use digital input and output from the same Arduino at the same time. I figure it would be great to have that sorted out beforehand so it works when I need it.
Thanks a lot.
Android processing and Purr data communication
Hi!
I have a new problem,
Everthing is working but not when I want to use the accelerometer of the phone, that's really weird.
Processing is receiving but not PD
Processing code:
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import oscP5.;
import netP5.;
import controlP5.*;
ControlP5 controlP5;
OscP5 oscP5;
NetAddress myRemoteLocation;
Context context;
SensorManager manager;
Sensor sensor;
AccelerometerListener listener;
float ax, ay, az;
void setup() {
fullScreen();
frameRate(25);
// create a new instance of oscP5.
// 12000 is the port number you are listening for incoming osc messages.
oscP5 = new OscP5(this, 12000);
myRemoteLocation = new NetAddress("192.168.137.1", 12011);
controlP5 = new ControlP5(this);
context = getActivity();
manager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
sensor = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
listener = new AccelerometerListener();
manager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME);
textFont(createFont("SansSerif", 30 * displayDensity));
}
void draw() {
background(0);
text("X: " + ax + "\nY: " + ay + "\nZ: " + az, 0, 0, width, height);
}
class AccelerometerListener implements SensorEventListener {
public void onSensorChanged(SensorEvent event) {
ax = event.values[0];
ay = event.values[1];
az = event.values[2];
print(ax);
print(ay);
print(az);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
void mousePressed() {
/* in the following different ways of creating osc messages are shown by example */
//OscMessage myMessage = new OscMessage(int(knobValue));
//myMessage.add();
OscMessage myMessage = new OscMessage("/test");
myMessage.add(ax);
myMessage.add(ay);
myMessage.add(az);
/* send the message */
oscP5.send(myMessage, myRemoteLocation);
}
PD code:
[import mr peach] -----> [udpreceive 127.0.0.1 12000]--------->[unpackOSC]------->[routeOSC /test]---->[number]
I've already try like that too :
[import mr peach] -----> [udpreceive 127.0.0.1 12000]--------->[unpackOSC]------->[routeOSC /test]----> [unpack f f f]-------->[number]
Thanks in advance
pduino for Vanilla or how to control arduino outputs from PD
[arduino] is the Pduino abstraction that communicates with Arduino via Firmata, and I think it's vanilla. Any way, if all you want is to control Arduino pins from Pd, you just need to send the correct bytes to Arduino through [comport].
I've written a tutorial on the communication between Pd and Arduino which you can find here, under "Tutorials", it's called "Arduino for Pd'ers".
After I wrote the tutorial I also wrote some abstractions to facilitate this communication, which are not used in the tutorial. You can get them here. Though, these are meant to receive data from the Arduino, not send. Sending data is covered in the tutorial.
Build a MIDI controller with the Arduino, Firmata and Pure Data
Time to start contributing some knowledge back to the wonderful world that is the internet; today, a step by step nice and easy tutorial on getting started to building your own MIDI controllers with the arduino.
When researching for my ableton controller project, I didn’t find much out there about using firmata on an arduino to send data to software. The standard approach just seemed to be create the code in the arduino language, upload it to your board and hack one of those MIDI to USB cables as a bodge job way of getting the MIDI out of the arduino.
So why firmata and pure data? Well the whole idea of firmata is that you flash it to your arduino, and it throws out serial about whats going on with the arduino inputs and outputs, then you decide how the software treats the readings coming in and going out.
Theory out the way, lets build some controllers. You’ll need a few things…
HARDWARE:
An arduino and something to wire into it (for this i’ll be using a pot)
A USB cable for your arduino
SOFTWARE:
Arduino – http://arduino.cc/en/Main/Software
Pure Data – http://puredata.info/downloads
Firmata – http://at.or.at/hans/pd/objects.html#pduino
Something to patch your new controller into; like Reason or Ableton Live
- SETTING UP FIRMATA AND PURE DATA
Install Pure Data and create a folder to store all your patches somewhere. Unzip Firmata and add the files ‘arduino.pd’, ‘arduino-test.pd’ and ‘arduino-help.pd’ to your new Pure Data folder. The ‘arduino.pd’ file is the object that we use in PD for opening up communication with your arduino and routing it to PD. Done? Awesome, your software is almost set up.
- FLASHING FIRMATA TO YOUR ARDUINO
Install the latest version of arduino and open it up. Connect your arduino with the USB cable to your laptop (i’m using a macbook for this by the way). In the example patches, open up “Standard Firmata”, select your board (im using an arduino mega), and your serial port (look for tty.usbserial for use with a USB cable). Then compile and hit the upload button and your arduino is now ready to use firmata and communicate with Pure Data!
- WIRING UP A POT
Potentiometers are cool, and theres a great arduino tutorial of how to wire one up here: http://www.arduino.cc/en/Tutorial/Potentiometer
Basically, all you need to know is that there are three pins; your two outer pins govern voltage flow across the pot, meaning one has to be 5V and the other has to be ground. It doesn’t matter which, but your 5v pin is going to be where your pot reads maximum, so convention dictates this should be the right hand pin. The center pin needs to be connected to an analog in on the arduino and will read the value of the pot as it sweeps from ground (0v) to 5v.
All wired up? Plug it into your laptop and open Pure Data, we’re ready to get things talking.
- SETTING UP OUR PATCH
Open the example “arduino-test.pd” Pure Data patch you copied over earlier. It should look like this one…
The test patch has everything we need to open a connection and enable pins. Firstly, lets delete a bunch of stuff and make our window a bit bigger. Hit Command + E to enter edit mode in Pure Data.
Ok a quick explaination; the key component here is the ‘arduino’ object. This is being drawn from the file you copied in earlier, and is what communicated with your arduino. Here we can do everything to control the arduino from opening a connection, to receiving data.
The large grid allows us to set the mode of each pin on the arduino. Remember pins 0 and 1 are reserved for Rx and Tx. I’m using analog pin 4 for this demo, so I’ve set my pin mode for pin 4 to ‘analog’.
Now we can plug our arduino in and get a reading from the potentiometer.
- ARDUINO INTO PURE DATA
With your arduino plugged in, hit command and E to bring us out of edit mode. In our patch, click on ‘Devices’ above the arduino object and open up the pure data terminal. (That other thing that loads with PD that has all the scary code in)
The “Devices” message connected to the arduino object pings your computer to find what devices are connected and on what serial ports. Since we’re using a USB cable to connect our arduino, we’re looking for something with ‘usbserial’ in it, in this case; port 2.
Select the relevent port in the green box at the top (remember the first box is ‘0’, second is ‘1’ and so forth) and hit ‘Open’ to establish a connection. Check the terminal to see if the connection was sucessful.
Now lets check we’re getting something in. Create a number box (Command + 3) and connect it to the relevent pin on the ‘Route analog’ box at the bottom. In this case, pin 4.
One more thing; if you’re not getting any readings in, you’ll need to click on ‘pd old analog/digital controls’ and enable your pins here too. What I tend to do in my patches is just not include the large grid but make my own ‘old pd’ controls custom to what i’m enabling/disabling to save space.
Here’s what the ‘old analog/digital controls’ subpatch looks like (pin 4 enabled)…
Come out of edit mode and check that you’ve got readings. If so congratulations! If not, troubleshoot, start with making sure your usb connection is opened, make sure all the correct pins are enabled (remember you’re counting from 0 not 1 on most of these buttons in PD, it’s just the way computers work).
- SCALING READINGS TO MIDI
So we’ve got a reading and chances are it’s to 3 decimal places between 0 to 1. No problem, create a new object (Command + 1) and type “autoscale 0 127”. This allows us to scale the input to a min and max value, in this case 0 to 127 of MIDI. Next, lets get things looking nice, create a new object and type “knob”. Connect this AFTER the autoscale object. (the knob is default set to read inputs from 0 to 127. Then create another number to display the scaled MIDI data coming out, and finally a new object and type “ctlout 1”.
It should look something like this…
The second box should be outputing values from 0 – 127 now, and the knob giving a visual representation of your potentiometer.
Now lets patch it into ableton…
- PURE DATA TO ABLETON LIVE
Firstly, you’ll need to set up your macs IAC driver if you’ve not done this. Basically you’ll need to go into Audio/MIDI preferences and enable your IAC driver. Then create a new input and output. One for input to DAW and one for output from DAW. Google around for a tutorial on this, its really simple, a 30 second job.
After you’ve set up your IAC driver, go back to PD and go to preferences > MIDI Settings, and connect your IAC driver.
Open ableton and go to its MIDI preferences. Create a device listing for your IAC driver and enable its ins and outs into ableton like so…
And thats it! Create an instrument and try to assign something! I’ve got it controlling the brightness of a bass sound here.
Shout out for Facu who requested this tutorial. Hopefully it’ll help some of you looking to get into this stuff and start building things but with no idea where to start.
HC-SR04 Ultrasonic Sensor with Pd (How long does a bang last?)
Hi,
I'm trying to use an HC-SR04 Ultrasonic sensor through arduino with Pure Data using Pduino and Standard Firmata. Pduino works perfectly and I am able to get data from many other sensors. This particular sensor is slightly different because it needs me to send a pulse of length 30 micro seconds at a time interval of more than 60 milliseconds to the trigger pin of the sensor, which triggers the ultrasonic waves from the transmitter. The receiver of the sensor measures the ultrasonic waves as they bounce back after reflecting from an external object to measure the distance.
My problem is that I am trying to figure out how to give a 30 microsecond pulse as an output from Pd, which leads me to ask, how long does a 'Bang' last? Is there another way to set up a pulsing mechanism where I can control the time interval of the pulse? Something like the pulseIn() function in Arduino IDE?