oF - ARDUINO communication

Hi everyone!

I have an application with three buttons made using openFrameworks that selects a sequence in the arduino to be run, whenever any of the buttons is selected it sets a variable (b1Push, b2Push or b3Push) then a message is sent to the ARDUINO:

This is written in the update() function

  
  
  
        if (b1Push){  
            prog=1;  
        }  
  
        if (b2Push){  
            prog=2;  
        }  
          
        if (b3Push){  
            prog=3;  
        }  
          
      
        arduinoCom.writeByte(prog);  
  

it is running at 9600 bps

on the other hand I have the ARDUINO receiving that message, also at 9600 bps, it compares the button that was pushed before and the sequence that it activates (eg. if button 1 was pushed before, activo = 1) with the new message received (programa), if activo = programa, it does nothing and continues to run the code where interrupcion() was called. if activo != programa, then it resets the arduino and does the new sequence:

  
  
void interrupcion(){  
  programa = Serial.read();  
  if(programa != activo){  
      resetFunc();  //call reset  
  }  
}  
  

the problem I have is that when a sequence is running, and I push a button in my oF application, it takes at least 5 cycles in the Arduino before it receive what oF is sending, I think that they are not synchronized and when oF is “talking” Arduino is not listening, and when Arduino is listening, oF is not talking, eventually, they will synchronize (x number of cycles after I pushed the button) and the sequence activates,

How can I solve this problem? if that’s not the problem, then what is it?

THANK YOU! and sorry if any of my english is wrong… not a native!

from what you say and the bit of code you posted i would say calling reset on the arduino is what creates your delay. i assume resetFunc(); calls a function that then forces an actual reset of the arduino.

when you reset an arduino (manually or via code in your arduino sketch) it takes a few seconds for the arduino’s boot loader to be done and to start the actual sketch.

in my experience OF sends serial messages pretty fast to my arduino.

s.

sendByte() is an ofArduino method, which uses Firmata. If you’re writing lots of custom code for your Arduino you might want to just use ofSerial to communicate with your Arduino as it can be much faster, particularly if your OF App -> Arduino communication is simple.