Converting char to int

Hello,

For my project we were having trouble converting a char value, reading grom mySerial.read();

I fond @guix 's answer to a similar chat.

I used this code and changed a little bit for my needs.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(6,9);
void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  mySerial.begin(9600);
}
void loop() {
if ( mySerial.available () > 0 ) {
  static char input[6];
  static uint8_t i;
  char c = mySerial.read ();

  if ( c != '\r' && i < 5 ) // assuming "Carriage Return" is chosen in the Serial monitor as the line ending character
    input[i++] = c;
    
  else
  {
    input[i] = '\0';
    i = 0;
      
    int number = atoi( input );
    
    Serial.println( number );
  }
}}

But the only return I am having is 0.

Please let me know if I am not clear or if more information is needed. Thanks so much!

My code before using @guix 's code was something like that:

#include <SoftwareSerial.h>
int i;
SoftwareSerial mySerial(6,9); // RX, TX
char soc[6];
void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  mySerial.begin(9600);
}

void loop() {
for(i=0;i<5;i++){
    if (mySerial.available()) {
      soc[i] = (char)mySerial.read();
    }
  }
if(soc[0]=='s'){
  for(i=1;i<6;i++){
    Serial.print(soc[i]);
    }
    delay(5000);
    Serial.println("");
  }
}

Please let me know if I am not clear or if more information is needed. Thanks so much!

have a look at the examples in serial input basics

On the right of Serial Monitor select "Carriage Return" and 9600 baud.

Did that help?

try this

#include <SoftwareSerial.h>
SoftwareSerial mySerial(6, 9); // RX, TX
int someNumber;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  mySerial.begin(9600);
}

void loop() {
  if (mySerial.available() > 0) {
    someNumber = mySerial.parseInt();
    Serial.println(someNumber);
  }
}
1 Like

where is the integer data comming from? are you typing it in? a program running on a PC? etc

It is a String from a Rasberry Pi

if it is a string terminated by carriage return or line feed try using readbytesuntil or readstringuntil then parse it using snscanf() or similar
also recommend you increase the serial monior speed to faster than the incomming baudrate, e.g. I use 115200
how have you connected it the RPi? e.g. TTL to TTL, USB to TTL?
what Arduino are you using?

1 Like

I think it would be good to get the code you have working. The solutions offered that solve your problem but use different methods may be enough for you, but there is something to be said for getting your character by character input code to work.

At the very least, it will mean you have done a non-blocking collection of an integer from an input stream. If everything you write is with non-blocking in mind, you will have an easier time doing more and more with one Arduino.

I got a bit further than you with your sketch but I had to change out the software serial for just reading from the serial port.

I don't think software serial is the problem, but you could echo the received characters to see if they are plausible.

In fact littering the code with Serial.print() statements to check the values of key variables and confirm the flow of the process is an easy and cheap method of finding errors you can't just see reading or playing with the running sketch.

I changed the line ending for the serial monitor input to carriage return only.

And I see integers not zero that bear some good resemblance to what I enter. There's something odd yet in your code around the logic of getting five character or finishing with the line ending character, but your code is close and I encourage you to get it functioning with or without help here.

I can't do more from here easily, when I get to the lab I will look again.

a7

It works really well thank you

Thanks for the comments. With @kolaha's code it worked really well. But in the end with the value we were reading we had a 0 sometimes so that's why I changed a little bit and at the and it looks like that ant it works really well.

void loop() {
  if (mySerial.available() > 0) {
   if(mySerial.parseInt()>0){
    someNumber = mySerial.parseInt();
    Serial.println(someNumber);
    }
  }
}

remember if you are connecting a UNO to the RPi serial TTL GPIO14 and GPIO15 that the UNO uses 5V logic and the RPi 3.3V logic
even using a level converter I managed to destroy the RPi UART on GPIO14 and GPIO15 - it worked OK for months then died

It seems odd that you can call parseInt twice, like why doesn't the first one eat the integer leaving nothing for the second?

Not quite a reason to leave for the lab, but it does make me wonder just how what works really well is, in fact, really working.

And I repeat, there's nothing wrong with solving your problem and getting on with it, but this is blocking code, I think, and that might end up being a problem, depending on how the characters are being sent from whomever is sending them.

a7

could be serial monitor is set to terminate a line of text with "both NL & CR"
the first parseint reads the integer data and the second 0 ???
after using parseint() or similar functions I tend to flush any control characters out of the input stream

1 Like

in this way you read a number twice
better

void loop() {
  if (mySerial.available() > 5) {
   
    someNumber = mySerial.parseInt();
    Serial.println(someNumber);
 
  }
}

or

void loop() {
  if (mySerial.available() > 0) {
    delay(10);
    someNumber = mySerial.parseInt();
    if(someNumber )Serial.println(someNumber);
    while (mySerial.available() > 0)mySerial.read();
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.