Pages: [1]   Go Down
Author Topic: What causes Setup() to run again?  (Read 777 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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:
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
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: June 13, 2009, 05:53:31 am by Nappi » Logged

Palmetto, FL USA
Offline Offline
Full Member
***
Karma: 0
Posts: 138
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

London, England
Offline Offline
Edison Member
*
Karma: 4
Posts: 1026
Go! Go! Arduinoooo !!!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: