I'm trying to set up a serial connection between my Duemilanove (ATmega328) board and a Python program running on my PC (Windows XP). I thought a good test of my serial connection would be to make the Python program light an LED on the board. My attempt to emulate the great examples in these forums have yielded mixed results.
When I run the program the light does not blink. However, if I type ser.write("Hello World") in the Python console after the program closes the light
does blink. Why doesn't it blink when the program runs?
Here's my Arduino sketch:
// Open a serial connection and flash LED when input is received
int ledRed = 4; // LED connected to digital pin 4
void setup(){
// Open serial connection.
Serial.begin(9600);
pinMode(ledRed, OUTPUT);
}
void loop(){
if(Serial.available() > 0){ // if data present, blink
digitalWrite(ledRed, HIGH);
delay(1000);
digitalWrite(ledRed, LOW);
delay(1000);
Serial.flush();
}
}
Here's my Python program:
## Open a serial connection with Arduino.
import serial
ser = serial.Serial("COM8", 9600) # open serial port that Arduino is using
print ser # print serial config
print "Sending serial data"
ser.write("Hello World")
# Reminder to close the connection when finished
if(ser.isOpen()):
print "Serial connection is still open."