Arduino recieve int gives negative numbers after 5 digits

So I need to get a string from my pc to arduino, it's terminated by an '\n'. (its a 27bit binary number)
but for some reason after 5 digits it will give negative numbers, here's code

int PC_Receive() {
  String inString = "";
  int inChar = 0;
  do {
    while(Serial.available() == 0);
    inChar = Serial.read();
    if(isDigit(inChar)) {
      inString += (char)inChar;
    }
    
  } while(inChar != '\n');
  
  return inString.toInt();
}

For example when i send number 0x700FF0F\n to my arduino

What is the code that sends that?

kamhagh:
So I need to get a string from my pc to arduino, it's terminated by an '\n'. (its a 27bit binary number)
For example when i send number 0x700FF0F\n to my arduino it will see 7 0 0 f f 0 f \ n (not 7 0 0 f f 0 f \n)

If your description is correct, then it is the sender's fault, not the receiver's fault.

But, since you did not post your actual code, it may well be that your receiver code is not what you think it is either.

Woooh. You just went back and changed the description of the problem, the title in the original message, and the code to something different.

Don't do that.

It means the comments after it make no sense for others reading later. Just add a new message down the bottom with your updated code/information.

arduinodlb:
If your description is correct, then it is the sender's fault, not the receiver's fault.

But, since you did not post your actual code, it may well be that your receiver code is not what you think it is either.

yeah it's weird it's arduino's consoles fault, other serial apps work fine, but as i have a new problem i changed the subject and tittle! wow that was a quick answer :smiley:

arduinodlb:
Woooh. You just went back and changed the description of the problem, he title in the original message, and the code to something different.

Don't do that.

It means the comments after it make no sense for others reading later. Just add a new message down the bottom with your updated code/information.

:o sorry, i didn't expect a reply this quickly! I don't have the old message anymore!

I won't do that the next time :frowning:

kamhagh:
So I need to get a string from my pc to arduino, it's terminated by an '\n'. (its a 27bit binary number)
but for some reason after 5 digits it will give negative numbers, here's code

int is 16 bit in Arduino (-32,768 to 32,767).

String.toInt() returns a long. Change your return to long for 32 bits.

long PC_Receive() 
{
...
}

arduinodlb:
int is 16 bit in Arduino.

Change your return to long for 32 bits (signed)

long PC_Receive() 

{
...
}

oh...

now I have a different problem:

void loop() {
  if(Serial.available() > 0) {
    /*
    you will need to write your own way of receiving input and determining when to loop in here...
     I am just including the part that sets the color picked above.
     pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
     */

    long hexval= PC_Receive(); // You sent: 0x01FF8C3C or, integer value,33524796
    Serial.println(hexval, HEX);
    if(hexval = 251658240) {
      clearLEDs();
      leds.show();
    }
    int led=hexval >> 24;
    int r = hexval >> 16 & 0xFF;
    int g = hexval >> 8 & 0xFF;
    int b = hexval & 0xFF;
    Serial.print("Led: ");
    Serial.println(led, HEX);
    Serial.print("R: ");
    Serial.println(r);
    Serial.print("B: ");
    Serial.println(b);
    Serial.print("G: ");
    Serial.println(g);
    leds.setPixelColor(led,r,g,b); //LED 1 as color FF8C3C
    leds.show();
    //    delay(500);
  }

for some reason my LED is always F and rbg are all 0, i guess it has to do something with the value types, but i don’t see why, as result is less then 16 bytes (8 bytes)

if(hexval = 251658240

The oldest one in the book.

hexval should also be

unsigned long hexval.

arduinodlb:
hexval should also be

unsigned long hexval.

My PC is off so i can't try it noe, i will after dinner, (also i can't my my hands, weird, do much coding)

Buy why unsigned? Long has enough bits doesn't it?

kamhagh:
My PC is off so i can’t try it noe, i will after dinner, (also i can’t my my hands, weird, do much coding)

Buy why unsigned? Long has enough bits doesn’t it?

ิbecause RGB values are positive integers, so unsigned is the correct datatype.

AWOLs post identified your current problem however.

arduinodlb:
ิbecause RGB values are positive integers, so unsigned is the correct datatype.

AWOLs post identified your current problem however.

:o i didn't see that one!

I make that mistake all the time :smiley: thanks a lot!