Serial.println() very slow?

Hey all,

I'm a Cococa developer, new on Arduino platform.
My problem is very simple (a noob one). I'm searching a way to communicate with my computer, to send some orders, execute commands or programs from my Arduino.

So, I upload a simple program to my card:

const int buttonPin = 2;

void setup()   
{                
  Serial.begin(115200);
  pinMode(buttonPin, INPUT); 
}

bool run_once = false;

void loop()                     
{
  int reading = digitalRead(buttonPin);
  
  if(reading == HIGH && !run_once)
  {
    Serial.println("A");
    run_once = true;
  }
  delay(500);
}

This code loop the button and wait until it pressed.
When pressed, a serial proxy (Xcode project based on the Arduino Serial Example sample code found on the website) read incoming text (here an "A") to launch trigger commands.

And it works, bravo!

But it's very slow, not valid for a real-time use. The serial console receive my char hardly less a second. I reduced all latency, sleep, delay values found in the proxy code but it's still too slow.

Is this behavior is normal?
If not, from where this latency come?
And finally, exists it another way to execute commands on computers?

Regards.

delay(500);

Could be your problem?

No, obviously, I have the same behavior when I comment this delay.
Strange isn't it?

But what about "run_once" - where does it get reset?

Reset? I don't understand really. (I'm sorry about my poor english?)

run_once block the loop to execute once the println().

Do you think my code is the issue?

In your sketch, if the button is HIGH (I assume you've got a pull-down wired to it) and "run_once" is false (as it will be at the start of your program), then you'll print an "A".
Now, you set "run_once" true, so you can never print another "A", regardless of the state of the button.

Hmmm, but it works. :-/
Each time a press my button, an "A" appears in the serial console, but with a latency of a second.

When I press the button, the Arduino send a char. That's all, but quickly?
How I can code this differently?

I don't suppose you're talking about the small push button built onto the Arduino board are you?

Andrew

I don't suppose you're talking about the small push button built onto the Arduino board are you?

:o

Yes, I use the little push button built onto the board.
Why?

That's the reset button

What a noob I am. :-?

Thanks for that and sorry for disturbing. :slight_smile:

No worries. :smiley:

The simplest way to put a switch onto the Arduino is to use the built-in pull-ups, so:

const int buttonPin = 2;
void setup () 
{
  Serial.begin(115200);
  pinMode(buttonPin, INPUT);
  digitalWrite (buttonPin, HIGH); // enable the pull-up resistor.
}

Now, connect the switch between pin 2 and ground.
When the switch is open, the input pin will read HIGH, and when the switch is closed, it will read LOW.

HTH

Or here's how to do it with a physical (external) pull-down resistor: http://arduino.cc/en/Tutorial/Button

Don't worry about the mistake - everyone has to start somewhere.

Andrew

exists it another way to execute commands on computers?

Take a look at this application (Gobetwino). It could save you a lot of coding on the PC side of things.