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