I am a bit new to both Arduino and Python. My aim is to send commands to Arduino to run a stepper motor. I need to send an array containing the parameters. After consulting many earlier posts like:
Send multiple int values from Python to Arduino using pySerial
I wrote my Arduino code:
long incoming[3];
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
Serial.println("Ready");
}
void loop() {
if(Serial.available()) {
for (int i = 0; i < 3; i++) {
incoming[i] = Serial.read();
}
Serial.println(incoming[0]);
Serial.println(incoming[0]);
Serial.println(incoming[2]);
}
}
And the Python(2.7) code:
from time import sleep
import serial
import struct
ser = serial.Serial('COM6', 9600)
while True:
ser.write(struct.pack('>iii',2000,10,220))
print ser.readline()
sleep(.5)
Now coming to the problem:
- The values printed on the Python shell are completely wrong, including negative values.
- If I use >BBB instead of >iii, I am getting the correct result, but I can't go beyond 255, the integer limit. There is a small catch here also, the initial two sets are giving minus values and then it becomes alright.
- Further, is there any way to interrupt a running void loop?
Any help is deeply appreciated.
incoming[0]
twice