I am currently trying to pass multiple values from a python script to an arduino. The values are passed over the serial port. The python script is here:
import sys
import serial
import time
import urllib2
import json
from pprint import pprint
######################################################################
#READS JSON FILE FROM URL
response = urllib2.urlopen('http://ift.tt/1OAuRfJ')
data = json.load(response)
#pprint(data)
#####################################################################
# FIRST CUBE STUFF#
data0= data[0]['x']+", "+data[0]['y']+", "+data[0]['z']+", "+data[0]['colour']
#print data0
data0x = str(data[0]['x'])
data0y = str(data[0]['y'])
data0z = str(data[0]['z'])
data0c = str(data[0]['colour'])
print 'DATA ONE'
print data0x
print data0y
print data0z
print data0c+" light blue"
print '\n'
######################################################################
#SECOND CUBE STUFF#
data1 = data[1]['x']+", "+data[1]['y']+", "+data[1]['z']+", "+data[1]['colour']
#print data1
data1x = str(data[1]['x'])
data1y = str(data[1]['y'])
data1z = str(data[1]['z'])
data1c = str(data[1]['colour'])
print 'DATA TWO'
print data1x
print data1y
print data1z
print data1c+" red"
print '\n'
######################################################################
#THIRD CUBE STUFF#
data2 = data[2]['x']+", "+data[2]['y']+", "+data[2]['z']+", "+data[2]['colour']
#print data3
data2x = str(data[2]['x'])
data2y = str(data[2]['y'])
data2z = str(data[2]['z'])
data2c = str(data[2]['colour'])
print 'DATA THREE'
print data2x
print data2y
print data2z
print data2c+" purple"
print '\n'
#####################################################################
#FOURTH CUBE STUFF
data3 = data[3]['x']+", "+data[3]['y']+", "+data[3]['z']+", "+data[3]['colour']
#print data4
data3x = str(data[3]['x'])
data3y = str(data[3]['y'])
data3z = str(data[3]['z'])
data3c = str(data[3]['colour'])
print 'DATA FOUR'
print data3x
print data3y
print data3z
print data3c+" yellow"
print '\n'
##################################################################
#MAIN
port = 'COM7' #check this is right every time
def main(val):
ser = serial.Serial(port) #gets serial port
ser.write('#')
time.sleep(1.5) #delays 1.5s for duino
ser.write(val)
ser.write('~')
written = ser.write(val) #writes value over serial
ser.close() #close the port
print "BYTES WRITTEN TO PORT: ", written
print "VALUE WRITTEN TO PORT: %s "%val
##################################################################
#MENU#
def menu():
#menu for testing input to duino
print 'CUBES AVAILABLE: '
print '1: CUBE 1'
print '2: CUBE 2'
print '3: CUBE 3'
print '4: CUBE 4'
print '5: QUIT'
return input("ENTER ONE OF THESE OPTIONS: ")
loop=True
choice=0
while loop:
choice=menu()
if choice == 1:
main(data1) #pass the values as one string
elif choice == 2:
main(data1x) #pass each individual value
main(data1y)
main(data1z)
main(data1c)
elif choice == 3:
main(data2x)
main(data2y)
main(data2z)
main(data2c)
elif choice == 4:
main(data3x)
main(data3y)
main(data3z)
main(data3c)
elif choice == 5:
print 'QUITTING'
break
In the code, we want to be able to send the Arduino 4 values, x, y, z and a hex colour. Is there anyway of getting the Arduino to be able to take each value we send, and store it in either in an array or just a variable. Or could I send it as a string and then split the string up on the arduino, then set substring to x, y, z and colour.
Here is the code on the arduino, very basic at the moment.
/*
Rainbowduino v3.0 Library examples: Cube1
Sets pixels on 3D plane (4x4x4 cube)
*/
#include <string.h>
#include <Rainbowduino.h>
#include<SoftwareSerial.h>
SoftwareSerial portOne(10,11);
SoftwareSerial portTwo(8, 9);
char str;
void setup()
{
Serial.begin(9600);
portOne.begin(9600);
Rb.init(); //initialize Rainbowduino driver
}
void loop()
{
while (Serial.available())
{
str = Serial.readStringUntil('~');
//need to set substring to x y z and colour
//Set (Z,X,Y,Colour)
//Rb.setPixelZXY(Z,X,Y,colour);
}
}
Aucun commentaire:
Enregistrer un commentaire