Go Down

Topic: Print "Hello" on Serial connection (Read 784 times) previous topic - next topic

rosak

Hi,
How to make Arduino to print "Hello" on serial connection but only once, without delay, and loop and then continue to execute the whole loop? I need the program to be running all the time since boot up, just to print "Hello" when I connect to it (or also print "I'm alive" every some period of time while being connected), just so I know that I'm connected.
Sometimes I'm connected but the communication is broken, probably because of my laptop BlueTooth.
Thanks

JaBa

Anything that you only want to execute once, at Startup, should be placed in the Setup Routine.

If you want something to only execute once within the Loop, you Need to have a flag

void lood()
{ boolean flag = true;
   if (flag)
   {  Serial.print("Hello");
       flag = false;}
   }

If you want it to repeat every so often Change the flag to an int and save the millis().
If (millis() < (flag_millis + repeat_time))
{  flag_millis = millis();
    Serial.print("Hello again");}

GypsumFantastic

1. Put a Serial.println("Hello"); right at the end of setup()

2. Somewhere sensible in loop() put something like this:
Code: [Select]

if (millis() > lastAlive + checkPeriod)  {
Serial.println("I'm alive");
lastAlive = millis();
}


lastAlive would need to be an unsigned long
checkPeriod is the time between checks in milliseconds (make it an appropriate type)

rosak

Thanks, but you both didn't understand the question.
Let me ask in different way. How to "detect" that the serial connection has been established?

I think I found already, "if (Serial)". Will try it out later.

patduino

Hi rosak,

Alternately, you can wait for it to establish immediately after the begin statement:

while (!Serial) ;

Pat
There are 10 types of people in the world, those who understand binary, and those that don't.

rosak

Thanks Pat but I can't wait, I said the program has to run regardless of serial connection.

patduino

Try the 'if' and let us know how it works!  I think you're right.
There are 10 types of people in the world, those who understand binary, and those that don't.

sterretje

if(Serial) only checks if a Serial object is created. And will always be true with the exception of a few boards. See If(Serial)
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Robin2

Let me ask in different way. How to "detect" that the serial connection has been established?
That is very very different from what your Title implies.

The if (Serial) only tell you if the serial port exists within the Arduino - it is intended for the Arduinos that use the 32U4 MCU which does not have a separate TTL to USB device.

With an Uno or Mega the Arduino should reset when the Serial port on the PC is opened. That means that the code in setup() will run.

With a Leonardo or Micro (which use the 32U4 MCU) there will not be an automatic reset although you can arrange for a PC program to cause a reset by briefly opening the serial port at 1200 baud and closing it again before opening it at the working baud rate.

The simplest way to know if there is a serial connection is for the PC program and the Arduino program to operate a startup sequence in which one of them sends a message to the other and waits for a response.

Have a look at the code in this Python - Arduino demo

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

rosak

That is very very different from what your Title implies.

You're right, but that's my lack of English language.

I see now. I should have figured out myself that's impossible. This is why I use a STATE pin of my HC-05 to detect connection (but I can't use it for this purpose as it's already connected to reset for programming over BT).

Ok while typing now I figured out what to do.
I will connect HC-05 STATE pin to one of digital inputs so Atmega knows when I'm connected (and can print hello, waiting for input etc.)
Then I'm going to add a piece of code for reset when I want to upload sketch over BT. Something like:
bool reset = 0;
   if (reset)
      wait for STATE to go HIGH and then LOW again (at this stage STATE pin is LOW as I'm connected)
   digitalWrite(resetpin, LOW) // digital pin connected to reset through a capacitor.

I just have to find out how to detect this state change to HIGH and LOW again but that should be easy.

Go Up