Detect different buttons from Arduino


#1

I recently just figured out how to send/receive button presses from my Arduino to my oF program, but what do I do if I connect multiple buttons and want to do different commands depending on which button gets pressed? Note: I am restricted to not using Firmata, or any add-ons


#2

What I did was to make the Arduino print a different thing of my multiple inputs and then parse them in my oF app.

One example, say you have 4 buttons want to track, you print a string like 1001 or 1110 where each digit is either pressed or not pressed for each button. Then in your oF app, you split the string and then you can check for each value. You might want to send them as comma separated values too, or with spaces, whatever you feel is the best way for you to parse them.


#3

This might be useful:

https://openframeworks.cc/documentation/utils/ofUtils/#show_ofSplitString


#4

How would I send a string from the Arduin to the oF app? Right now I am able to detect different buttons on my Arduino program but I’m not sure how to, if we take your example, send a string to from Arduino to oF


#5

how are you doing that?


#6

I’m using Serial.write(), and currently I I’m only getting my oF program to register that a button/byte has been pushed/sent. I’m basing everything off of the oF serialExample program and the Arduino built-in Button example


#7

Oh yeah… the ofSerial can only read a single byte at a time I think? One of the reasons why I used ofxSerial.

Well, you have two options,

  1. Send a unique byte for each buttons on/off state from the Arduino. And a bunch of if/else or switch statements inside oF depending on the value of each byte.
  2. Bookend a string with two unique bytes. Send the string as bytes between the two other bytes from the Arduino. In the oF app, construct a string from the bytes received in between those two unique bytes. And then parse it and do whatever with it.

#8

How do I know what the value of a received byte from the Arduino to oF is?


#9

That should be in the oF serial example. I thought you had that working.


#10

I’m not able to see the value byte for some reason, I can only detect that a byte is being sent. Even the serialExample code itself does the same, it displays nothing for the actual value of the byte, but it does know that a byte is being sent


#11

I know you said you can’t use Firmata but you may as well emulate how it works. What @ayruos is describing is almost exactly what firmata does only the protocol is a bit more complicated. For how to parse through the bytes being sent by the arduino you can look here:

also ofSerial can read more than one byte at a time

Regarding the most recent post are you trying to print the byte being sent? That will likely not work because its likely casting it to ascii character codes and so if you are sending 1 or something as the byte its not a displayable character. I would compare the received character against a hex value like so:

if(charReceived == 0x01) {
    do stuff
}

#12

Could you share some code, what exactly are you sending from the Arduino, and what’s the loop that’s reading the bytes from the serial port?


#13

I figured my problem out, in the Arduino code where I was testing the states of the buttons, if they were LOW, I would send that state out, which would be zero (LOW) no matter which button, hence oF received the same zero, and the zero value is not printable on screen, as it is a numeric zero. In the ASCII system, value 48 is zero, where value 0 is a NULL character. Thank you so much for everyone’s help!