How declare char type array

Hi I want to know how can i fix my code .
I will put " 30a_10s_1Onoff " in the Serial.read()
then divide to [30a / 10s / 1Onoff ]by setting the 'Null ' on the underbar(_)

#include <String.h>
#include <SoftwareSerial.h>

void setup()
{
Serial.begin(9600);
Serial.println("start");
}

void loop() {
if(Serial.available()){
char *data = Serial.read(); // I will type "30a_10s_1Onoff"

  data[3]='\0'; // to make a division like 30a / 10s / 1Onoff
  data[7]='\0'; // to make a division like 30a / 10s / 1Onoff
  char *str; 
  str = data ;

  char ctrl[5];  
  strcpy(ctr1, str); 
  str= data+4;
  char ctr2[5];
  strcpy(ctr2, str);
  str= data+8;
  char ctr3[10];
  strcpy(ctr3,str);

}

this is my code

"""" 'ctr1' was not declared in this scope """

and error message

i dont know how declare char type array

please let me know

Post your code, not a picture of it. That way other folks can try compiling it.

Looks like you declared it as CTRL and tried to use it as CTR1. Change:
char ctrl[5];
to
char ctr1[5];

It's not a picture. The forum software mangled it because there are no code tags.

Why have you #include'd the String class?

oh i got it
actually it's my first question so i didn't know better way
thank you i

ok everybody
im not good at arduino and using this forum communitiy
i think first setback that i wanted to fix has been solved .
It was my mistake :sweat_smile:

thank you for your all comments

Not now, sure :wink:

1 Like

You forgot the "with code tags part"... FWIW. :slight_smile:

hi everyone :grinning:

i want to change char type array ctr1 /ctr2 /ctr 3 to int type and print int type number 30/ 10 /1

#include <String.h>
#include <SoftwareSerial.h> 
     
void setup() 
{         
   Serial.begin(9600);  
   Serial.println("start");
  }

void loop() { 
    if(Serial.available()){   
      char *data = Serial.read(); // I will type "30_10_1"   ///  30a_10s_1onoff
      data[2]='\0'; // to make a division like 30a / 10s / 1Onoff
      data[5]='\0'; // to make a division like 30a / 10s / 1Onoff
      char *str; 
      str = data ;

      char ctr1[3];  
      strcpy(ctr1, str);  //ctr1=30
      str= data+3;
      char ctr2[3];
      strcpy(ctr2, str); // ctr2 = 10
      str= data+6;
      char ctr3[3]; 
      strcpy(ctr3,str); // ctr3 = 1

}

everything is okay
i need your help. thanks

What does it mean in English?


That won’t work

Check the documentation to see what read will return

I would suggest to study Serial Input Basics to handle this

You can use atoi() or strtol() to convert a cString to an integer

i want to change char type array ctr1 /ctr2 /ctr 3 to int type and print int type number 30/ 10 /1

i wanted tell you that i have three char type array ctr1,ctr2,ctr3 and i need to change these char array into int type to use these values (30,10,1) as a number

Char type is for data transmission, int type for internal storage and use. See Serial input basics how to convert text into numbers.

No.

That is the worst crap I have seen for a long time.

Are the '_' always in the same place?
How is your input terminated?

@alex_hw

Your two topics on the same or similar subject have been merged.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

hi i'm new in the forum.
It's hard to me to convert data type properly

I need to get three int value a,b,c from Serial.read
(if i type 50_50_5 then int a=50 , int b=50 , int c= 5)

#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);
      
      }
    }

this is my error message:

{ invalid conversion from 'int' to 'char*' [-fpermissive] }

and it's result:
{
start
000000000000000000000000
}

i don't know why there are only so many 0 and how can i get rid of that error message

Of course I've searched a lot, but it's not easy to apply my code :joy: (i'm noob..)
so what should i do to get a better code
could you give an example code or good idea??
thanks.

Is there something at the end of the input text ? For example a Carriage Return or Line Feed or a timeout ? How do you know that everything is received ?

See:

Hi @alex_hw

This is the third post with the same code that you create in the last two days.
Please note that cross-posting is prohibited on the forum.

Previous thread