I'm a bit new to Arduino so this may be stupid, but I'm trying to make a program that takes an input of characters from the serial port, uses it to create a string, and then writes that same string to the serial port. (I call it the parrot code for obvious reasons.) The main problem is that it has to take in any generic string so long as its 6 characters long and the first character is a. The code is below. My problem is that the code will only run once. That is to say that the first string will work perfectly, but upon inputting a second string the code won't do anything. Any possible fixes?
Code:
/*
The code is just not running twice.
Fix that and you should be good, because it works
perfectly for the first time.
*/
//Also, the code only accepts strings prefaced by 'a',
//otherwise it won't print it.
int txtMsgLength = 6;
String txtMsg = ""; // a string for incoming text
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect
}
// send an intro:
Serial.println("Code Initialized");
Serial.println();
}
void loop() {
if (Serial.available() > 0) {
for( int x = 0; x < 1; x++) {
char inChar = Serial.read();
txtMsg += inChar; // add any incoming characters to the String:
}
if (txtMsg.length() == txtMsgLength && txtMsg.startsWith("a") ) {
Serial.println(txtMsg);
}
}
}
Whoops my bad. But yes, please do look at it if you could.
*/
* The code is just not running twice.
*/
//Also, the code only accepts strings prefaced by 'a',
//otherwise it won't print it.
int txtMsgLength = 6;
String txtMsg = ""; // a string for incoming text
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect
}
// send an intro:
Serial.println("Code Initialized");
Serial.println();
}
void loop() {
if (Serial.available() > 0) {
for( int x = 0; x < 1; x++) {
char inChar = Serial.read();
txtMsg += inChar; // add any incoming characters to the String:
}
if (txtMsg.length() == txtMsgLength && txtMsg.startsWith("a") ) {
Serial.println(txtMsg);
txtMsg = "";
}
}
}
It is not a good idea to use Strings with Arduino. They cause memory problems.
Use character arrays instead, and post your code properly, so it looks like this similar program using character arrays;
char message[21]; //20 characters plus zero terminator
int x=0; //array index
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect
}
// send an intro:
Serial.println("Code Initialized");
}
void loop() {
if (Serial.available() > 0) {
message[x] = Serial.read(); // add incoming characters to the array
x++; //increment index
message[x]=0; //terminate character string
}
if ( strlen(message) == 20 ) { //if 20 characters have been read and stored, print the string
Serial.println(message);
x=0; //start over from beginning
message[x]=0; //empty message
}
}
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.
Add some extra Serial.print statements to see what your code is doing. Examining the intermediate results should make it clear why the code is failing.
char inChar = Serial.read();
Serial.print("inChar ");
Serial.print((int)inChar, DEC);
Serial.print(' ');
Serial.println(inChar);
txtMsg += inChar; // add any incoming characters to the String:
Serial.print("txtMsg "); Serial.println(txtMsg);
HINT 1: In the serial console window, set it to "No line ending". If do you do not, your program will receive invisible characters such as line feed or carriage return or both. This will not fix the problem but it reduces the complexity.
HINT 2: The for loop executes only 1 time so it can be removed. But this will not fix the problem.