Hi,
I'm trying to show messages in my 16x2 LCD-display using LiquidCrystal library with Arduino Duemilanove and HD44780 as well as Python to send the data serially. Here's the code I'm using for testing purposes:
Arduino:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
char message[32] = "No input yet";
void setup() {
Serial.begin(9600);
lcd.print(message);
}
void loop() {
if ( Serial.available() > 0) {
//clear the message and LCD
for (int j = 0; j < 32; j++) message[j] = 0;
lcd.clear();
//Read string until line break
int i = 0;
char tempChar = Serial.read();
while ( tempChar != '\n' ) {
message[i] = tempChar;
i++;
//Wait for new char to become available (just in case)
while (Serial.available() <= 0) {}
tempChar = Serial.read();
}
//print the message
lcd.print(message);
}
}
Python:
import serial, sys, time
testString1 = "Test String"
testString2 = "It works!"
#Try to open serial
try:
ser = serial.Serial('/dev/tty.usbserial-A6008iPf', 9600)
except serial.SerialException:
print "no device connected"
sys.exit()
#Send sentences
time.sleep(2.0)
ser.write(testString1 + '\n')
time.sleep(2.0)
ser.write(testString2 + '\n')
#Close serial
ser.close()
When I upload the sketch, close Arduino software, reset the device and run serialtest.py, I get:
"No input yet" delay "Test string" delay "It works!"
...like I should. "It works!" also stays on the display, like it should. However, when I run serialtest.py again, I get the following:
"No input yet" small delay "Test string" delay "It works!"
What I would like to know is what causes the setup() to run again? I had understood that it would only run once when the device has been powered on or reset.
Also, am I doing something completely stupid here? I wrote those programs just as a test but I'm planning to use Python to parse some data and send the data serially to the LCD-display through Arduino by running the script every 5 minutes or so. Should I alter my approach? How?
I have only used Arduino for a couple of weeks now so forgive me if this is really something obvious.
And thanks in advance!
-Tomi