I programmed my Duemilanove with the Communication->SerialEvent example and added code to blink an LED on pin 13 and sound a buzzer on pin 12. I am using a baud rate of 9600. The problem I am experiencing is sluggish serial communication. I have to enter a delay of 700 ms in a couple locations in my code or the Arduino does not respond. I will post my code this evening, but in the meantime am curious if this is a known issue.
Serial works, lets see your code...
I am using a baud rate of 9600.
There's one reason.
I have to enter a delay of 700 ms in a couple locations in my code or the Arduino does not respond.
The delay()s are never necessary. There's another reason. Whatever you are masking by calling delay() should be handled properly.
I will post my code this evening, but in the meantime am curious if this is a known issue.
With delay()s and low baud rates, yes. With no delay()s and a reasonable baud rate, no. But, of course, the code is essential.
I'll post my code this evening. I made some modifications and ran into an issue. For some reason, when setting String1 = String2, Serial.println(String1) is returning both strings appended. This doesn't seems right. For example, if String1 equals "BlinkSlow\n" and String2 equals "BlinkMed\n", after executing String1 = String2, String1 is equal to BlinkSlow\nBlinkMed\n. It is appending the strings :~.
It is appending the strings
Without seeing your code, all we can offer is sympathy. Which you probably don't need.
FYI, I replicated the code in Visual C++ 2010 and String1 = String2 worked as expected (String1 contents replaced with String2 contents). Standby for Arduino code.
Here is my Arduino code. It is actually working now. I made a couple tweaks this evening. I no longer need the long wait statements in my C# code. Regarding the String1 = String2 (currString = prevString in my code), I don't know exactly how I fixed it. I had several Serial.println commands throughout my code to check the value of currString for debugging purposes. Does executing a Serial.println() add a newline character to the string? I also changed my serial input code so that the newline character was no longer appended to currString.
#include <string>
String currString = ""; // a string to hold incoming data
String prevString = "";
boolean stringComplete = false; // whether the string is complete
int led = 13;
int buzzer = 12;
boolean flash = false;
boolean buzz = false;
void setup()
{
pinMode(led, OUTPUT);
pinMode(buzzer, OUTPUT);
Serial.begin(9600);
currString.reserve(15);
prevString.reserve(15);
}
void loop()
{
if (stringComplete == true)
{
if (currString == "BuzzOn")
{
buzz = true;
}
else if (currString == "BuzzOff")
{
buzz = false;
}
else if (currString == "LEDon")
{
digitalWrite(led, HIGH);
stringComplete = false;
}
else if (currString == "LEDoff")
{
digitalWrite(led, LOW);
stringComplete = false;
buzz = false;
}
else if (currString == "BlinkSlow")
{
prevString = "BlinkSlow";
if (flash == true)
{
digitalWrite(led, HIGH);
if (buzz == true)
{
digitalWrite(buzzer, HIGH);
}
delay(1000);
flash = false;
}
else
{
digitalWrite(led, LOW);
digitalWrite(buzzer, LOW);
delay(1000);
flash = true;
}
}
else if (currString == "BlinkMed")
{
prevString = "BlinkMed";
if (flash == true)
{
digitalWrite(led, HIGH);
if (buzz == true)
{
digitalWrite(buzzer, HIGH);
}
delay(500);
flash = false;
}
else
{
digitalWrite(led, LOW);
digitalWrite(buzzer, LOW);
delay(500);
flash = true;
}
}
else if (currString == "BlinkFast")
{
prevString = "BlinkFast";
if (flash == true)
{
digitalWrite(led, HIGH);
if (buzz == true)
{
digitalWrite(buzzer, HIGH);
}
delay(50);
flash = false;
}
else
{
digitalWrite(led, LOW);
digitalWrite(buzzer, LOW);
delay(50);
flash = true;
}
}
else
{
Serial.println("Invalid Command");
}
if (prevString != "")
{
currString = prevString;
//Serial.println(currString);
}
}
}
void serialEvent()
{
currString = "";
while (Serial.available()>0)
{
char inChar = (char)Serial.read();
delay(50); //Delay required or Arduino does not respond.
if (inChar != '\n')
{
currString += inChar;
}
else
{
stringComplete = true;
Serial.println(currString);
}
}
}
currString.reserve(15);
prevString.reserve(15);
If you know how big the strings that the String instance is going to wrap, you really don't need the wrapper, do you?
void serialEvent()
{
currString = "";
while (Serial.available()>0)
{
So, every time a character arrives, clear the current String? No!
delay(50); //Delay required or Arduino does not respond.
Bullshit. The delay just means that you stand around kicking the dirt for far longer than necessary, in the hope that the rest of the serial data arrives before you read the data.
You should get rid of the delay() AND the reset of currString. You should NOT expect that all the data will arrive during one call to serialEvent(). Whatever data does arrive should be appended to currString (or, better yet, stored in the next position(s) in the char array).
The time to reset currString is in loop, AFTER you determine that the packet is complete (because stringComplete is true) and use the value of currString.
If you know how big the strings that the String instance is going to wrap, you really don't need the wrapper, do you?
Because I didn't know any better. I was simply following the Serial Event example provided with the IDE.
So, every time a character arrives, clear the current String? No!
I initially cleared currString in my Loop but my while loop kept terminating for some reason. I think something else may have been going on, which I have now cleared up, so I'll put it back.
The delay just means that you stand around kicking the dirt for far longer than necessary, in the hope that the rest of the serial data arrives before you read the data.
Okay. I'll remove the delay then and see how it goes.
You should NOT expect that all the data will arrive during one call to serialEvent().
I wasn't aware that all the data may not be received in one serial event. Good to know.