import serial
import numpy
import matplotlib.pyplot as plt
from drawnow import *
values = []

plt.ion()
cnt=0

serialArduino = serial.Serial('/dev/cu.usbmodem1421', 9600)

def plotValues():
    plt.title('Serial value from Arduino')
    plt.grid(True)
    plt.ylabel('Temperature Deg C')
    plt.plot(values, 'rx-',label='values')
    plt.legend(loc='upper right')

for i in range (0,600):
    values.append(0)

while True:
    while (serialArduino.inWaiting()==0):
        pass
    valueRead = serialArduino.readline()

    try:
        valueInInt=int(valueRead)
        print (valueInInt)
        if valueInInt <= 1024:
            if valueInInt>=0:
                values.append(valueInInt)
                values.pop(0)
                drawnow(plotValues)
            else:
                print "Invalid! negative number"
        else:
            print "invalid too large"
    except ValueError:
        print "Invalid! cannot cast"
        
