Go Down

Topic: What causes Setup() to run again? (Read 876 times) previous topic - next topic

Nappi

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:
Code: [Select]
#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:
Code: [Select]
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

Nappi

#1
Jun 13, 2009, 12:53 pm Last Edit: Jun 13, 2009, 12:53 pm by Nappi Reason: 1
Oh well.. I guess that would be the Serial.begin() statement in the setup(). Is that correct?

Which method would you recommend if I was to send data to Arduino, say, every 5 minutes: opening the serial connection at the beginning of transmission and closing it afterwards (like in the method above) or keeping the serial connection (and the python script) open all the time while still only sending data every 5 minutes?

Robert Lee

I believe that reopening the serial connection from the PC/MAC is causing this (since the Arduino is resetting with the new serial connection). I would try by leaving the connection open the entire time.

Mike Mc

When you re-open the seral line it causes the Arduino to reset. That is correct and normal. Not sure if it can be overridden in software or if you'd need to modify the fuses in the bootloader though.

Nappi

Thanks for the replies.

Arrived at the same conclusion a while after posting the thread. I guess it is the easiest to just keep the connection open anyway.

Go Up