Hi
If I have a number this way
x= 22,123456789
How to make
y= 123456789
Note that the value (y) is taken after the comma (,) .
Thank you
Hi
If I have a number this way
x= 22,123456789
How to make
y= 123456789
Note that the value (y) is taken after the comma (,) .
Thank you
Have a look at the parse example in Serial Input Basics
...R
Seems easy,
while it is probably not what you wanted, or will be able to achieve on 8 bit Arduinos.
long x, y;
void setup() {
Serial.begin(250000);
x = (22,123456789);
y = x;
Serial.println(y);
}
void loop() {}
123456789
If the comma should denote a decimal point, you will need a system that really supports double
and obviously a different strategy.
#include <string.h>
#include <stdlib.h>
void(setup)
{
char* x= "22,123456789";
// Get first split
strtok(x,",");
char *y= strtok(NULL, ",");
Serial.begin(115200);
// Prints "123456789"
Serial.println(y);
// If you want it as a number
long yValue = atol(y);
}
Have fun!
Might want to use a long int for yValue and atol() instead of this:
// Prints "123456789"
Serial.println(y);
// If you want it as a number
int yValue = atoi(y);
Do you have a number or a text representation of a number?
thank you all
I really benefited from this information.
Thank you from my heart ....
Robin2:
Have a look at the parse example in Serial Input Basics...R
This sounds good
But in Example (5) in Serial Input Basics
// Example 5 - Receive with start- and end-markers combined with parsing
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
char messageFromPC[numChars] = {0};
int integerFromPC = 0;
float floatFromPC = 0.0;
boolean newData = false;
//============
void setup() {
Serial.begin(9600);
Serial.println("This demo expects 3 pieces of data - text, an integer and a floating point value");
Serial.println("Enter data in this style <HelloWorld, 12, 24.7> ");
Serial.println();
}
//============
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
showParsedData();
newData = false;
}
}
//============
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("x ");
Serial.println(messageFromPC);
Serial.print("Integer ");
Serial.print("y ");
Serial.println(floatFromPC);
}
I tried this code works great but only receives from a user
How can be controlled so that it receives from (Serial.println(G)) and analyzes them in the same way
G= <84515,5452.874>
Serial.println(G)
x= 84515
y=5452.874
I hope that the thing I want is clear .... Thank you
Did you see posts #3 and #4?
How can be controlled so that it receives from (Serial.println(G))
The output from Serial.println() is the number of bytes that were written to the outgoing serial buffer. Hardly useful input to a string parsing function.
Now, passing G to that function would be a completely different story.