Serial.readString() Help

(I'm assuming "toInt" will barf on ASCII hex)

It does, Hence my question

Why is the String in hex in the first place ?

TheMemberFormerlyKnownAsAWOL:
The inappropriately-named String method "toInt" returns a long.

So, if you send the IR code as ASCII decimal, not hex, you'll be able to use "toInt" on the received String, and then simply cast the returned value to "unsigned long".

(I'm assuming "toInt" will barf on ASCII hex)

So this compiles with no errors;

irsend.sendNEC(code.toInt(), 32);

So basically if i send the decimal value instead of the hex, then it should all work fine, and if it all works fine then awesome, if not, I'll have to play around with it again.

UKHeliBob:
It does, Hence my question

It's in hex because the example sketch I used to see what value I needed gave me that value, so I just assumed that it needed to be hex when I sent it, I didn't realise that I could convert it to a decimal value and send that instead.

little update for every one that’s been helping me, i’ve just tested sending the decimal value and it works :smiley: so I’m going to go through and try reading the decimal value from the serial.readstring and doing the toInt. I’ll come back and let you guys know how it happens.

Awesome news everyone :smiley: It works! So now I can turn my lights off by sending the IR codes over the serial com port and then the arduino sends out the code without it being hard coded.

Here’s the finished code;

#include <IRremote.h>
int serialval = 0;
IRsend irsend;
void setup()
{
Serial.begin(9600);
int serialval = 0;
}
void loop(){
String code;
code = Serial.readString();
if (code != ""){
irsend.sendNEC(code.toInt(), 32);
}}

This is a hell of a lot shorter and better than doing it the way I was originally doing it. so its gone from 75 lines of code just in the void loop to 4 lines of code :smiley: You guys are great, thanks again for the help :smiley: Now all I have to do is go through and convert the hex codes to decimal values and change my config for my visual studio app and its all golden :smiley:

void loop()
{
  if (Serial.available()){
      irsend.sendNEC(Serial.readString().toInt(), 32);
  }
}

TheMemberFormerlyKnownAsAWOL:

void loop()

{
  if (Serial.available()){
      irsend.sendNEC(Serial.readString().toInt(), 32);
  }
}

Oh god, I'm a dope XD I did not spot that, i think I had a bit of tunnel vision there XD

knil92:
Oh god, I'm a dope XD I did not spot that, i think I had a bit of tunnel vision there XD

What have you not spotted -- a redundant code?