Reputation: 1129
I am trying to interface with my Arduino via Pyserial on OS X. I am controlling LEDs with sending numbers from 0 to 9. The code as in
import serial
arduino = serial.Serial('/dev/tty.usbserial', 9600)
arduino.write('5')
works perfectly fine, but I am trying to have the 5 in this example as a changeable variable but something like
arduino.write('%d') % 5
won't work. I don't know how to format a variable that the output is equal to the working example?
Upvotes: 1
Views: 5297
Reputation: 12077
The string formatting must go inside the parentheses of the function call.
e.g. arduino.write("%d" % 5)
Here's some information on old-style string formatting
Upvotes: 2
Reputation: 7078
You were not formatting the string %d
but the function call:
arduino.write('%d' % 5)
The %
sign should go after the '
immediately.
This will work, but it is also preferable to use tuples with format strings:
arduino.write('%d' % (5,))
Because when you have multiple parameters, you will anyway have to use it this way:
arduino.write('%d.%d' % (2, 3))
Upvotes: 5