-1

Building with an Adafruit Feather LoRa modul a radio contorl unit for a car. Steering input being converted from a PWM into a 8 bit integer. Sender send to Reciver while Reciver reads it yout with

...(char)LoRa.read()...

I would like to change 3 digits from a char datatype into an integer without translate the char decimal number into an integer without being a converted to a 8 bit long datatype, so I can use the values for the steering. Any solution with a function or class to solve this? Best would be a 3 liner code.

Pertev
  • 3
  • 3
  • `String LoRaData = LoRa.readString();` or `String Lorasata = LoRa.readStringUntil("\n");` and then `LoraData.toInt()`. Read more about [Stream class](http://reference.arduino.cc/reference/en/language/functions/communication/stream/) which many of communication class inherited from. Read [String class](https://reference.arduino.cc/reference/cs/language/variables/data-types/stringobject/) to get familiar with all the String methods. If you like to read the data into a char array instead of a of a String, you can also use [atoi()](https://cplusplus.com/reference/cstdlib/atoi/). – hcheung Apr 25 '23 at 12:58

1 Answers1

0

In order to convert the first 3 digits of char which we get into an integer , try this code

int steeringValue = (LoRa.read() - '0') * 100 + (LoRa.read() - '0') * 10 + (LoRa.read() - '0');
Farhan
  • 1
  • 1
  • It helped to understand the datatypes, but it still messes up with a values. I am getting nummber with the size of 4 to 5 digits. Just want to mention. The base code is from the LoRa Library – Pertev Apr 25 '23 at 11:54
  • @Pertev Probably for numbers under the "100"? It'll always expect 3 characters in the numbers and if you provide 2 or 1, the read() will timeout and return int(-1) and mess up the result completely – KIIV Apr 25 '23 at 13:49
  • Understood. So for higher numbers like 255 I need to add one more line with 1000: (LoRa.read() - '0') * 1000 + – Pertev Apr 25 '23 at 14:35
  • This is not a reliable code if the input has variable length, it would be better to read the entire string in into a char array or String object, and then convert it to integer as I mentioned on the comment above. – hcheung Apr 26 '23 at 01:03