I can't correctly send data over serial from a Python script to an Arduino Uno. I'm using 9600 baud, and the Arduino correctly resets, but it does not read the char I'm sending from the Python script. I call time.sleep()
to ensure the reset on the Arduino does not interfere, and I'm using Windows 7. I should clarify by saying that my desktop is running the python script and connected via USB to the Serial of my Arduino Uno. I then have the RX & TX pins (pins 0 & 1) of my Uno connected to my Mega's Serial1 (pins 18 & 19). I then use the Serial Monitor in the Arduino IDE on my laptop (which uses the Mega's regular Serial) to peek at what the Uno is seeing. Here is the code for the Mega:
void setup() {
Serial1.begin(9600);
Serial.begin(9600);
Serial.println("Master Ready");
}
void loop() {
if(Serial1.available() > 0) {
char inByte = Serial1.read();
Serial.write(inByte);
Serial.write("\n");
}
}
Here is the code for the Uno:
void setup() {
Serial.begin(9600);
Serial.println("Slave Ready");
}
void loop() {
if(Serial.available() > 0) {
char inByte = Serial.read();
Serial.write(inByte);
}
}
And finally, here is the python script:
import sys
import serial
import time
ser = serial.Serial("COM23",9600)
n = int(sys.argv[1])
print n
time.sleep(10)
print ser
print n == 41
if (n == 70):
ser.write(chr(97))
print 'a'
elif n == 41:
ser.write('ggggggg')
print 'b'
elif n == 42:
ser.write('hello world')
print 'c'
elif n == 25:
ser.write(chr(100))
elif n == 26:
ser.write(chr(101))
elif n == 22:
ser.write(chr(102))
elif n == 10:
ser.write(chr(103))
elif n == 4:
ser.write(chr(104))
elif n == 14:
ser.write(chr(105))
elif n == 7:
ser.write(chr(106))
elif n == 11:
ser.write(chr(105))
elif n == 5:
ser.write(chr(106))
elif n == 17:
ser.write(chr(107))
# head - a - 70
# right bicep - b - 41
# right forearm - c - 42
# left bicep - d - 25
# left forearm - e - 26
# chest - f - 22
# right thigh - g - 10
# left thigh - h - 4
# right shin - i - 11 - 14
# left shin - j - 5 - 7
# waist - k - 17
In case it helps, I'm essentially trying to write the hit locations in Doom3 to an Arduino over serial so that the Arduino can turn a motor on in the proper location on your body. The game code is in C++, and I first tried using a serial library for C++, but that didn't work either.