communicating button presses via Serial

start, reset, left, right, bottom, up buttons are connected to an Arduino. The program is coded in Python. I do Serial.print() to send the state from the Arduino via USB to the "PC".

I check the button state in Arduino like this:

void loop(){
  val = digitalRead(pin_index);  // read input value
  if (val == HIGH) {         // check if the input is HIGH (button released)
    Serial.println('something')
  } else {
    Serial.println('something_else')
  }
}

Then in Python I do Serial.Serial.readline(). THis works when testing with one button.

What about when connecting all the buttons? Should I use a separator character like "/" or "|" and then separate the string in Python with that character as separator (string.split('/')) . Or is there a nicer way to do this? The string Python gets via serial will look something like this "1-1/2-0/3-1/4-1/5-1/6-0" (button name or index followed by its state).

My first suggestion would be to send the data only when the button [u]becomes[/u] pressed and not when it [u]is[/u] pressed. This will avoid flooding the PC with unnecessary data. Look at the StateChangeDetection example in the IDE.

As to using separators in the data, you need them in order to differentiate the different parts of the data stream.

UKHeliBob: As to using separators in the data, you need them in order to differentiate the different parts of the data stream.

I just thought theres a moreelegant way to do it, is all.

Comma Separated Values (CSV) is a well established and widely used system.

Have a look at the 3rd example in serial input basics. Personally I would use a similar system for sending data to a Python program.

This Python-Arduino demo may also be useful - it was written before serial input basics

...R

ghosttrain: I just thought theres a moreelegant way to do it, is all.

Think about how you would separate the string of data when presented with it written down on paper. It is tempting to send just 112031415160, parse the string by separating it into individual characters and decode them as index and state pairs but if you ever lose a character in the serial data you will not be able to resynchronise the data stream.

Think too what would happen with no delimiters if someone sees your project and asks "can you do the same with 16 buttons ?" or "can you change the number of states from 2 to 16 ?". Without delimiters you would not be able to do it in the same way.

I wasnt thinking of not using delimeters, but sending the data some other way. I dont know how, I'm asking. I'm new to Serial, binary data would take less space and speed to send, thats one thing that comes to my mind.

ghosttrain: binary data would take less space and speed to send,

However it is more troublesome to debug so I would use ascii code unless speed is a problem. An Arduino can easily work at 500,000 baud - it's just the Serial Monitor that is limited to 115200 baud.

Here is a Python binary code demo

...R

Yes, understood.

A related question, If i have 6 buttons and want to write strings for their state like this "1-0", theres 18 bytes plyus newline. Im not very familiar with "baud rate", what values should I use?

ghosttrain: Im not very familiar with "baud rate", what values should I use?

The highest one that works reliably. The only essential is that the sender and receiver use the same baud rate.

The baud rate is the number of bits (loose language, but sufficient) per second and a byte requires about 10 bits so at 115200 baud you get about 11,000 characters per second.

...R

If you wire all your 6 buttons on the same port then you can read all buttons states at once, in a single byte, ready to be transmitted with Serial.write.. :smiley_cat: