Hi
I'm trying to teach me to write function that will count the number of characters in a string
My code looks like this and it works:
void setup() {
// initialize the digital pin as an output:
Serial.begin(9600);
Serial.println("Read: ");
}
void loop() {
char x= "example";
int resultat = (strlen(x));
Serial.println(resultat);
delay(1000);
}
but if I want to see how many characters there are in an INT, I do not know how to use it
Here is the code I have
void setup() {
// initialize the digital pin as an output:
Serial.begin(9600);
Serial.println("Read: ");
}
void loop() {
int i = 123456;
char x= i;
int resultat = (strlen(x));
Serial.println(resultat);
delay(1000);
}
I got this
In function 'void loop()':
error: invalid conversion from 'char' to 'const char*
How convert a int to string and use the LEN function
Can anyone help me whit this, this will resolve my problem to continue with my other project.
A string is an array of char (bytes), where the last byte is a zero (an actual zero, not an ASCII '0').
"strlen" simply examines each character and if the byte isn't zero, it increments a count.
If the character is a zero, it returns the value of the count.
So, the string "example" needs at least eight bytes to store it - one byte extra for the terminating zero.
An "int" is not an array of "char"s, so "strlen" won't work.
Perhaps you need "sizeof"?
Or are you trying to determine how many decimal digits you need to represent a given value, in which case you probably need "log".
This is my problem:
I will join two Arduino. first Arduino are linked together with nunchuck and temp sensor and I get three values
accel_x_axis
accel_y_axis
3 temp
These values can be different number of characters (one or three).
For example 5 or 56 or -46
Value I get in my function is INT.
These, I would like to send to other Arduino, but I would have fixed number of characters.
For example, with:
char x_axis = (strlen(accel_x_axis))// this is my problem, can not solve this
switch (x_axis) { //depending on the number of characters
case 1:
Serial.print(accel_x_axis);
Serial.print(" ");// add four spaces char
break;
case 2:
Serial.print(accel_x_axis);
Serial.print(" ");//add three spaces char
break;
case 3:
Serial.print(accel_x_axis);
Serial.print(" ");//add two spaces char
break;
This code would help me to have 5 char´s
When I'm done with all the values I'll get string with 15 characters.
Code for reading in second arduino is
Serial.flush(); // clear the serial buffer before reading new data
char timeString[13]; // create a string to hold the time value when it's read
memset(timeString,'\0',13); // initialize that string to all NULL characters
boolean timeStringValid = false; // declare and initialize a variable to track whether the string has all valid characters
Serial.print("GET");
byte inByte = '\0'; // declare and initialize a byte to read in serial data
long startTime = millis();//makes the start time = to now
int timeout = 1000; // timeout after one second
while(millis() - startTime < timeout && inByte != '*') {
inByte = Serial.read(); // read data and wait for an asterisk character
}
if (inByte == '*') { // if we got the correct start character (instead of a timeout)
timeStringValid = true; // declare and initialize a variable to track whether the string has all valid characters
long startTime = millis();//makes the start time = to now
int timeout = 1000; // timeout after one second
while(millis() - startTime < timeout && Serial.available() <12) {
; //wait for enough data to be available (13 characters of time string), while doing nothing else
}
for (int i=0; i < 12; i++) {
timeString[i] = Serial.read(); // reach each time string character into a character array
}
}
if (timeStringValid == true) {
char Xosa[5]; // create a string to hold the Xosa part of the string
memset(Xosa,'\0',5); // initialize that string to all NULL characters
strncpy( Xosa, timeString, 4); // copy the first four characters of timeString into the Xosa string
Serial.println(Xosa);
char Yosa[5]; // create a string to hold the Yosa part of the string
memset(Yosa,'\0',5); // initialize that string to all NULL characters
strncpy( Yosa, timeString+4, 4); // skip four characters, then copy the next two of timeString into the Yosa string
Serial.println(Yosa);
char Tempvalue[5];
memset(Tempvalue,'\0',5);
strncpy( Tempvalue, timeString+8, 4);
Serial.println(Tempvalue);
Code for second arduino need to be changed
This code must I change to load the correct number of characters and this piece of code I can manage without any problems.
My problem is how to send the correct number of characters
Thanks in advance
If the value is greater than or equal 10,000, it needs 5 characters.
Else, if the value is greater than or equal 1,000, it needs 4 characters.
Else, if the value is greater than or equal 100, it needs 3 characters.
Else, if the value is greater than or equal 10, it needs 2 characters.
Else, if the value is greater than or equal 1, it needs 1 characters.
Even easier, though, is to use sprintf:
char buffer[6];
sprintf(buffer, "%5d", val);
The array, buffer, now contains a string representation of val that is 5 characters long. Send the buffer to the serial port.
Thanks for the reply
but, unfortunately I have difficulty understanding what you have written.
Where or how to use your example?
please note that I am a beginner
char x_axis = (strlen(accel_x_axis))// this is my problem, can not solve this
accel_x_axis is what kind of variable? Int? float?
The strlen function takes a string as an argument. A string is a NULL-terminated array of characters. In other words, it's an array of characters where one of the array positions contains a NULL.
accel_x_axis is not a suitable argument.
The output from strlen is an int, not a char, so storing the value in x_axis is wrong.
If accel_x_axis is an int, and you want to output a 5 character string representation of that int: