Controlling Arduino protocol through Bonsai

723 views
Skip to first unread message

Albit Caban

unread,
Jun 2, 2021, 5:23:04 PM6/2/21
to Bonsai Users
Hi!

I have a behavioral protocol which I run in an arduino: Essentially every 90 secs it opens a time window of 30s where if an IR beam is triggered (its a lickometer) it will send a pulse to a relay switch which will open and that will in turn allow voltage to flow to a solenoid that will deliver water. I'm trying to adapt this protocol to using it with Bonsai but I have a couple of doubts: 1) Is there a way of instead of having to create the entire sequence on Bonsai have Bonsai start a specific script on the arduino instead? (So that whatever script I load to the arduino is started with the start button in Bonsai. 2) If not, could you point me in a good direction for every 90s reading from the arduino for 30s and sending the TTL if the IR beam is broken? I cant seem to figure out how to make it time dependent in a way that would repeat the sequence every 90 seconds continuously. Thank you so much!

Best, 

Albit Caban

brunocruz

unread,
Jun 3, 2021, 4:45:04 AM6/3/21
to Bonsai Users
Hi Albit,

 1) Is there a way of instead of having to create the entire sequence on Bonsai have Bonsai start a specific script on the arduino instead? (So that whatever script I load to the arduino is started with the start button in Bonsai. 
You could use the the Serial protocol to send specific characters that result in specific behaviors in your Arduino code. If you just want to start the the Arduino from Bonsai:
In Arduino add the line at the end of the setup() function:
while (Serial.read() != 115) {
 }
Arduino will wait for a character whose ASCII code is 115 (which is an <s>). Once it reads this character from the buffer, it will proceed to the rest of the script.
From Bonsai side:
1. Add a CreateSerialPort and configure accordingly (probably just the COM port and a baudRate that matches the one you use in your Arduino script)
2. Add a String node and set its value to <s>. Connect this node to a SerialStringWrite that will send this character to your Arduino. To control the timing of the instruction simply connect something to the String node (e.g. KeyDown).

As a side note, you could make this as complicated as you want. For instance, you could have a Serial parser function in Arduino that receives arbitrary characters from Bonsai and performs distinct instructions.

2) If not, could you point me in a good direction for every 90s reading from the arduino for 30s and sending the TTL if the IR beam is broken? I cant seem to figure out how to make it time dependent in a way that would repeat the sequence every 90 seconds continuously. 
Bonsai interfaces with Arduino through Firmata (https://www.arduino.cc/en/reference/firmata and https://groups.google.com/g/bonsai-users/c/D2vMTY8KHcs/m/o4wNc_IiCgAJ), I will attach a second workflow that implements the logic you might be looking for. However, I would not use vanilla firmata to control your valves if you are looking for millisecond precision since there are some bottlenecks in the order of a couple dozen milliseconds. You could modify the StandardFirmata protocol and hardcore a routine for your valves too. I have never tried it, but it should be possible. 

Hope it helps,
B
exampleSerialStart.bonsai
giveReward.bonsai

Albit Caban

unread,
Jun 8, 2021, 7:14:36 PM6/8/21
to Bonsai Users
Hi Bruno,

I ended up going for the first option since it seemed a little more straightforward. It worked! Thank you so much!

Best,

Albit

Reply all
Reply to author
Forward
0 new messages