An Arduino sends a string in the form of a sequence to another Arduino.
That data is in the form "1, 1, 4" or "1, 4" or "1, 2, 3, 4, 5" and so forth.
Say the string is "8, 9":
bool latch1 = true;
char* receivedChars = '0';
int leng = 0;
String sequence = "";
void setup()
{
Serial.begin(19200);
delay(500);
}
void loop()
{
receiveEvent();
Serial.println('\n');
delay(1000);
}
void receiveEvent()
{
if (latch1 == true)
{
latch1 == false;
sequence = "8, 9";
Serial.print("sequence: ");
Serial.print(sequence);
Serial.println();
leng = sequence.length();
Serial.print("leng: ");
Serial.print(leng);
Serial.println();
if (receivedChars == "")
delete [] receivedChars;
receivedChars = new char[leng];
}
for(int index = 0; index < leng; index += 3)
{
Serial.print("Index: ");
Serial.print(index);
Serial.println();
Serial.print("receivedChars: ");
Serial.print(receivedChars[index]);
Serial.println();
delay(3000);
// runFunctionForNum();
}
}
void runFunctionForNum()
{
Serial.println("Enter runFunctionForNum");
// switch statement for each number in sequence
delay(3000);
}
This prints
sequence: 8, 9
leng: 4
Index: 0
receivedChars:
How to print out the value for receivedChars? Right now it's either blank or a box character.
I am trying to access the array globally because the following part of a larger program (not minimal code) resets shape_sequence to blank. I want shape_sequence to retain the last string that was read (and not clear it out). Otherwise leng gets set to zero, and in turn the for-loop is never entered again. Instead, I want the for-loop to continually loop until new data is received.
bool latch1 = false;
String shape_sequence = "";
void receiveEvent()
{
if ( softSerial1.available() || latch1 == true)
{
// latch1 = true;
shape_sequence = softSerial1.readStringUntil('\n');
Serial.print("shape_sequence: ");
Serial.print(shape_sequence);
Serial.println();
leng = shape_sequence.length();
char receivedChars[leng];
shape_sequence.toCharArray(receivedChars, leng);
Serial.print("leng: ");
Serial.print(leng);
Serial.println();
for(int index = 0; index < leng; index += 3)
{
Serial.print("receivedChars, index: ");
Serial.print(index);
Serial.println();
Serial.print("receivedChars, shape: ");
Serial.print(receivedChars[index]);
Serial.println();
shape(receivedChars[index], A_0, iterate0, pace0);
}
Serial.println("--- --- --- Exit for-loop //////");
}
}
This prints
shape_sequence: 1, 4, 1
leng: 7
receivedChars, index: 0
receivedChars, shape: 1
Enter shape function, circle running...
receivedChars, index: 3
receivedChars, shape: 4
Enter shape function, clover running...
receivedChars, index: 6
receivedChars, shape:
Enter shape function, --- --- --- Exit for-loop //////
When the last line should, instead, print "receivedChars, shape: 1 "
Then the next line after, "Enter shape function, circle running..." before exiting for-loop.
Robin2
February 1, 2021, 8:40pm
3
It's not wise to use dynamic memory allocation in the small memory of an Arduino - it can easily cause memory corruption and a crash. Just create the array big enough for the largest message you will receive.
...R
Ok, I changed "char receivedChars[length];" to "char receivedChars[60];". How to get the sequence data globally or how to store the sequence data?
Use while( softSerial1.available() != "")
then the if statement about serial being available?
Ok, I think it works now just using if(shape_sequence != "")
Please post the latest complete code version for "I think it works now".
system
Closed
June 1, 2021, 11:13pm
7
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.