0
votes

I wrote this sample sketch to test serial communication with Arduino Leonardo (using Arduino IDE 1.0.5 on Windows 7):

int delayTime = 10000;
long lastExec = 0;

void setup()
{
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
}

void loop()
{
  long t = millis();
  if (t - lastExec >= delayTime) {
    if(Serial.available() > 0){
      Serial.println('Hello world');
    }  
    lastExec = t;
  }
}

The serial port selected seems working because the sketch uploads correctly.

However I didn't get anything in the serial monitor window. Why?

2
It should work if you send something to it. But then every time Serial.available() will be non-zero -- which may not be what you want.bobwki

2 Answers

6
votes

You first need to send a character to the Arduino.

Because you have if(Serial.available() > 0), the Arduino won't Serial.println("Hello World"); unless there is something in the Serial buffer.

You may also want to reduce the delayTime which is very long.

In conclusion, you can try this by typing something in your Serial Monitor:

void setup()
{
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
}

void loop()
{
  if(Serial.available() > 0){
    Serial.println('Hello world');
  }  
}

Hope it helps! :)

-3
votes

Does help to have "Hello world", not 'Hello world'.