In [1]:
from pyiduino import *

Set up

In []:
# Hardware Serial
# PORT = '/dev/tty.usbserial-A900ce0g'
# a = Iduino(PORT)

# FTDI (Software Serial)
# PORT = '/dev/tty.usbserial-FTH9JLQX'
# SPEED = 38400
# a = Iduino(PORT, SPEED)

# BlueTooth
PORT = '/dev/tty.HC-05-DevB'
SPEED = 38400
a = Iduino(PORT, SPEED)
In [3]:
for i in range(10):
    a.digitalWrite(13, HIGH)
    time.sleep(1)
    a.digitalWrite(13, LOW)
    time.sleep(0.3)

Servo Random Sweep

In [4]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from IPython import display

f, ax = plt.subplots()
ax.set_title("Servo Control")
ax.set_xlabel('t')
ax.set_ylabel('position')
f.set_size_inches(18, 6)

myServo = Servo(a)
myServo.attach(9) # attach on pin 9

x = []
y = []
tmax = 60
t_delta = 0.5

for n in range(1,tmax):
    pos = np.random.randint(180)
    myServo.write(pos) # move to position
    
    x.append(n*t_delta)
    y.append(pos)
    
    ax.plot(x, y, 'r-')
    display.clear_output(wait=True)
    display.display(f)
    time.sleep(t_delta)

# close the figure at the end, so we don't get a duplicate
# of the last plot
plt.close()

Analog Inputs

Read

In [9]:
import numpy as np

n = 1000
n_pins = 6
dt = 50
pins = np.arange(n_pins)
Y = np.zeros( (n, n_pins) )

for i in range(n):
    for pin in pins:
        Y[i, pin] = a.analogRead(pin)
    a.delay(dt)

Analyze

In [57]:
%matplotlib inline
import matplotlib.pyplot as plt
x = np.linspace(0, float(dt)/1000*n, n)

plt.plot(x, Y)
plt.legend(['Pin %d' % i for i in pins])
plt.show()

y = np.average(Y,1)
# y = Y[:,0]

nn, bins, patches = plt.hist(y, 50, normed=1, histtype='stepfilled')
In [3]:
prog = '''

pinMode(13, OUTPUT);
for(;;) {
    digitalWrite(13, HIGH);
    delay(400);
    digitalWrite(13, LOW);
    delay(200);
}
'''
a.upload(prog)
Out[3]:
'for(;;) {\r\n'
In []: