i want get three different int values from one input text (I will type "nn_nn_n").
for example. if i type "60_25_5", my code is seperating a recieved text into three part by using underbar(_) and setting 'null', and i think three char array (text1, text2, text3) will have each value (60, 25, 5) finally, to use these values as a integer, i used atoi() .
this is my code:
#include <String.h>
#include <stdlib.h>
#include <SoftwareSerial.h>
void setup {
Serial.begin(9600);
Serial.println("start");
}
void loop() {
if(Serial.available()){
char *data = Serial.read();// = nn_nn_n
data[2]='\0';
data[5]='\0';
char *str ;
str = data ;
char text1[4];
strcpy(text1,str);
str = data + 3;
char text2[4];
strcpy(text2, str);
str = data + 6;
char text3[4];
strcpy(text3, str);
int a= atoi(text1);
int b= atoi(text2);
int c= atoi(text3);
Serial.print(a);
Serial.print(b);
Serial.print(c);
}
}
but the resutl of this:
start
000000000000000000000000
i know that im a super noob, but i'm ready to learn what you teach me! I waiting your comment, thanks.
If you are want teach how things works, don't use arduino IDE, because there is not good tool for watch what MCU doing. I use Atmel studio to show you how debug some parts of your code. Here is tools named debugger (and simulator) which allow as make look inside whole MCU parts (look at attached picture).
Basically, you have three basic misunderstandings in your code :
Transferring one character over a serial line takes some time. For baud rate 9600 baud this time is
10*1/baudRate. This is approximately 1ms.During this time MCU is capable execute
loopfunction maybe thousand times. Of course, mostly doing nothing becauseSerial.available()returns false. When first characters of transmitted message arriveSerial.available()returns true and code in conditions is start execute. But whole code is executed very quickly. No others characters is transmitted yet. Next character arrive again after 1ms. So your code process only one characters and again waits to next arrived characters. Because message have 8 characters (6 + CR LF), your code eight times write values variablesa b cand you get this responds000 000 000 000 000 000 000 000Space is not sending, but it represents waiting for next chars inloopfunction. So after every chars what MCU receive print values000Let's find out why all variables was evaluated to 0 value.Serial.read()not return String or string as you are expected. You must read Arduino documentation and understand it. Function returns-1if no data available or first available byte from message. In other words you get number representing ASCII value of arrived characters. In your example you get first value 0x36 because ASCII value of digits six is 0x36 ('6'). This is reason because I change your commandchar *data = Serial.read();// = nn_nn_ntochar *data = '6';which represent situation after first chars arriving.What this command doing? (spoiler - very bad things in this case) This command create local variable what is a pointer to char. This variable is created on the stack and take two bytes. You can see it in watch window on first row. You can see that address of this variable is
0x08E6. This variable is automatically created inloopfunction and is here inserted value returned from callingSerial.read(). So in first character is there value0x36. But remember this is pointer to char It's mean that data pointer now point to address '0x36'. This is bad, really very bad. What is on address0x36. You can see this portion of MCU memory in window Memmory1. Unfortunately this is part of memory reserved for access to IO registers. This registers control HW of MCU like Timers, Converters, SPI, IIC, UARTS ... It is very bad idea write random values to this area.data[2]='\0';This will try write zero to0x0038registerdata[5]='\0'This will try write zero to0x003BregisterYou are lucky in this case as you can see all registers in this portion has zero values and you don't break any hw configuration. All others commands can be easy checked by stepping and it is easy understand that you are still play wits zero length strings. So at finally values variables
a b cbecame zero.(all this story line it's a deal in case that MCU is ATMEGA328)