Dear colleagues.
Hope you are doing fine!
I'm doing a program for getting comands from serial comunication sent from bluethooth.
We have following comands that arduino will reiceve from bluethooth.
(command,value)
cycles,200
timer1,500
timer2,500
I need split and save command in variable as command "name and value" for use as below.
void loop() {
var = 0;
while (var < cycles) {
digitalWrite(12, HIGH);
delay(timer1);
digitalWrite(12, LOW);
delay(timer2);
var++;
}
Someone has an example for it ?
Have a nice day!
Thanks
Robin2
July 20, 2019, 1:51pm
2
Have a look at the parse example in Serial Input Basics .
If you can reduce the commands to a single character it will make the job of writing the Arduino program much easier. For example 'C' instead of "cycles" and "1" instead of "timer1"
Another way of simplifying things would be to send all three values as a single message, for example <200,500,500>. Then the Arduino would know which is which by the position in the message.
...R
You would be far better off understanding and using the code from Robin2's tutorial. All of those String concatenations will eventually lead to memory fragmentation and random bugs/resets. Here is a page that explains why not to use Strings and some ways to use null terminated character arrays (strings) instead.
Read the how to use this forum-please read stick y to see how to post code and some advice on how to ask a good question.
What Arduino board are you using?
Hi Colleagues.
I try to implement a simple code for test the example.
The main idea is head HelloWorld from serial and execute a blink port number 13 with delay time as serial comand.
<HelloWorld, 1000, 24.7>
if (messageFromPC == 'HelloWorld'){
var = 0;
while (var < 10) {
digitalWrite(led, HIGH);
delay(integerFromPC);
digitalWrite(led, LOW);
delay(integerFromPC);
var++;
}
}
Below you can find my code
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];
// variables to hold the parsed data
char messageFromPC[numChars] = {0};
int integerFromPC = 0;
float floatFromPC = 0.0;
int var = 0;
boolean newData = false;
#define led 13
//============
void setup() {
Serial.begin(9600);
Serial.println("Enter data in this style <HelloWorld, 12, 24.7> ");
Serial.println();
pinMode(led, OUTPUT);
}
//============
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
parseData();
showParsedData();
newData = false;
if (messageFromPC == 'HelloWorld'){
var = 0;
while (var < 10) {
digitalWrite(led, HIGH);
delay(integerFromPC);
digitalWrite(led, LOW);
delay(integerFromPC);
var++;
}
}
}
}
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars,","); // get the first part - the string
strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ",");
floatFromPC = atof(strtokIndx); // convert this part to a float
}
//============
void showParsedData() {
Serial.print("Message ");
Serial.println(messageFromPC);
Serial.print("Integer ");
Serial.println(integerFromPC);
Serial.print("Float ");
Serial.println(floatFromPC);
}
if (messageFromPC =='HelloWorld'){
There are a couple things wrong with that statement. You cannot compare strings like that. Use strcmp() . The strcmp() function is one of the functions in string.h , a library for searching, comparing and concatenating strings.
'HelloWorld'
Single quotes for single characters ('H'), double quotes for strings (string literals):
"HelloWorld"