[RESOLVED] Concatenate a Char array

Hi,
I’m using an arduino Uno with IDE 1.8.13. Here’s my problem:

I receive a data via the serial port, for example V90 (so V or H for which motor to activate then a angle value varying from -90 to 90) I receive the data correctly and the data transform in a string correctly. But I can’t put it back to the original value I wanted : I push the value 60 but the final value I get is 58.
This code is just a test code to put into another code after.

I had a way for it to work , the example 2 of the Serial Input Basics by Robin 2. But thats not how my superior want it. So I’m trying to do it this way and it could resolve some problem with my other code.

Have a good day! :slight_smile:

char moteurDesire;
char incomingByte;
char DegreeByteH[3];
char DegreeByteV[3];
String Degree;
int DegreeI;

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    incomingByte = Serial.read();
    Serial.println("data received");
    Serial.println (incomingByte);

    if (incomingByte == 'H') {
      for (int i = 0; i < 3; i++)
        DegreeByteH[i] = Serial.read();
      moteurDesire == 'H';
    }
    else if (incomingByte == 'V') {
      for (int i = 0; i < 3; i++)
        DegreeByteV[i] = Serial.read();
        moteurDesire == 'V';
        Degree.concat(DegreeByteV[1] + DegreeByteV[2] + DegreeByteV[3] + DegreeByteV[4]);
        DegreeI = Degree.toInt();
        long myNum = atol(DegreeI);
        Serial.print("angle value");
    }
  }
  Serial.print("moteur desire = ");
  Serial.println(moteurDesire);
  Serial.println (Degree);
  Serial.print("asked H angle = ");
  for (int i = 0; i < 3; i++) {
    Serial.println(DegreeByteH[i]);
  }
  Serial.print("asked V angle = ");
  for (int i = 0; i < 3; i++) {
    Serial.println(DegreeByteV[i]);
  }
  delay (1000);
}

moteurDesire == 'H';Oops

What's the problem with that line? I want to use that value later on to activate the asked motor. in this code it's not useful but I just want to make sure it works in this code so I can use it later on.So the value would be either V or H. Is there something wrong with writing it like that?

It’s a pointless comparison - you don’t do anything with the result.

Oh I didn't mean it to be a comparison , I wanted to put it as a value so just one =. Thanks for that help for that error.

gablack567:
What's the problem with that line? I want to use that value later on to activate the asked motor. in this code it's not useful but I just want to make sure it works in this code so I can use it later on.So the value would be either V or H. Is there something wrong with writing it like that?

The "statement has no effect" warning should have been a big clue. Read and understand your warning messages. If you didn't get any, go to Preferences... and set "Compiler warnings:" to "All".

Also, you are passing "DegreeI" (an int) to a function the takes a character pointer. You already converted the String to a number. No need to treat that number as a string and try to convert it again! I guess it's OK because you don't use the result anywhere. Just get rid of that line.

/Users/john/Documents/Arduino/sketch_sep06a/sketch_sep06a.ino: In function 'void loop()':
/Users/john/Documents/Arduino/sketch_sep06a/sketch_sep06a.ino:26:20: warning: statement has no effect [-Wunused-value]
       moteurDesire == 'H';
       ~~~~~~~~~~~~~^~~~~~
/Users/john/Documents/Arduino/sketch_sep06a/sketch_sep06a.ino:32:20: warning: statement has no effect [-Wunused-value]
       moteurDesire == 'V';
       ~~~~~~~~~~~~~^~~~~~
/Users/john/Documents/Arduino/sketch_sep06a/sketch_sep06a.ino:35:32: warning: invalid conversion from 'int' to 'const char*' [-fpermissive]
       long myNum = atol(DegreeI);
                                ^
In file included from /Users/john/Library/Arduino15/packages/arduino/hardware/avr/1.8.3/cores/arduino/Arduino.h:23:0,
                 from sketch/sketch_sep06a.ino.cpp:1:
/Users/john/Library/Arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/avr/include/stdlib.h:264:13: note:   initializing argument 1 of 'long int atol(const char*)'
 extern long atol(const char *__s) __ATTR_PURE__;
             ^~~~
/Users/john/Documents/Arduino/sketch_sep06a/sketch_sep06a.ino:35:12: warning: unused variable 'myNum' [-Wunused-variable]
       long myNum = atol(DegreeI);
            ^~~~~
Sketch uses 3634 bytes (11%) of program storage space. Maximum is 32256 bytes.
Global variables use 287 bytes (14%) of dynamic memory, leaving 1761 bytes for local variables. Maximum is 2048 bytes.

Note: You should not read characters before you know that they are in the buffer. If you see an ‘H’ or ‘V’ you are immediately reading three more characters. I would recommend using:

  DegreeI = Serial.parseInt();

instead of:

      for (int i = 0; i < 3; i++)
        DegreeByteH[i] = Serial.read();

The .parseInt() function will wait for characters to arrive until it finds the end of the number or no characters arrive in one second (timeout is adjustable).

Thank you very much john!! It's now working as intended , I didn't know a single function could resolve all of my problems.

Here's the code for anyone wondering after seeing the post

I didnt know you could put warning in the compiler either john so thanks for that too!

char moteurDesire;
char incomingByte;
int DegreeH;
int DegreeV;



void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    incomingByte = Serial.read();
    Serial.println("data received");
    Serial.println (incomingByte);

    if (incomingByte == 'H') {
      DegreeH = Serial.parseInt();
      moteurDesire = 'H';
    }
    else if (incomingByte == 'V') {
      DegreeV = Serial.parseInt();
        moteurDesire = 'V';
        
        Serial.print("angle valueV");
        
    }
  }
  Serial.print("moteur desire = ");
  Serial.println(moteurDesire);

  Serial.print("asked H angle = ");
Serial.println(DegreeH);
  Serial.print("asked V angle = ");
 Serial.println(DegreeV);
  delay (1000);
}