Combine StandardPyfirmata with custom code


In my project I need have to connect an incremental rotary encoder, a solenoid valve and a servo motor. all input and outputs are controlled by a python script. I control the solenoid valve and the servo motor with the standardFirmata with simple read() and write() functions on an Arduino Uno. The the rotary encoder uses a custom script on an ArduinoMega. I have no issues controlling both with Python, however now that I tried to combine them in one script I realized that only one serial port at once can be opened. So I can either read the rotary encoder, but not controll the valve or the servo and vice versa.

I was wondering if it is possible to to add the custom code of the rotary encoder to the code in the Firmata file to be able to control everything with just one Arduino, but I am not sure if that would create issues in the Firmata protocol if I just add somewhere.

I hope someone can help me with this situation or mention any alternatives.


Rotary Encoder Code:

by Ben-Tommy Eriksen

// Encoder connect to digitalpin 2 and 3 on the Arduino.

int counter = 0;  //This variable will increase or decrease depending on the rotation of encoder

void setup() {
  Serial.begin (4600);
  //Setting up interrupt
  //A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on moust Arduino.
  attachInterrupt(0, ai0, RISING);
  //B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr 3 on moust Arduino.
  attachInterrupt(1, ai1, RISING);

void loop() {
  // Send the value of counter
  Serial.println (counter);

void ai0() {
  // ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
  // Check pin 3 to determine the direction
  if(digitalRead(3)==LOW) {

void ai1() {
  // ai0 is activated if DigitalPin nr 3 is going from LOW to HIGH
  // Check with pin 2 to determine the direction
  if(digitalRead(2)==LOW) {

When you installed StandardPyFirmata, you made the Arduino a moron. It can ONLY respond to pull requests.

The rotary encoder sketch is designed to (uselessly) push data.

The two sketches can not possibly be combined.

Why do you want the rotary encoder sketch to spam the receiver with the same data over and over?

the whole thing is used to drive a psychophysical experiment.
I use the rotary encoder to detect the running of a mouse(animal) on a treadmill. The data it gives me is used in my python code to move an cursor in a psychopy window (kind of like a scrolling wheel the animal controls) to give select something.

the pyFirmata is used to controll the valve for the water reward delivery. If the mouse stops the rotary at a certain position, the valve should open to give the reward.

(The servo is rather optional at the moment. It just moves the wheel for the mouse to teach it what to do. I could to that manually as well if necessary.)

So, periodically checking the value of the rotary encoder could work, if you check often enough.

You'll need to dig into the Firmata documentation in order to learn how to develop a SysEx message, and have python send the appropriate request, and have the Arduino respond to that message properly.

Or, ditch Firmata and develop your own protocol.