Go Down

Topic: Subtracting serial read parseInt from regular int (Read 309 times) previous topic - next topic

Jul 26, 2018, 11:03 pm Last Edit: Jul 26, 2018, 11:11 pm by adamaero
Code: [Select]

void setup() {
    int a = 30;  // KEY ~ 30 mm = 1.2" = 0.1'

  int len, m, measured, feet;   
  char testStr[] = "R0033\r";     // testing string
  len = strlen(testStr);
  //Serial.println(&testStr[len - 4]);
m = Serial.parseInt(testStr[len - 4]);     // This worked for just printing it. But it does not work when trying to do more.
// measured TTL serial output converted, only last three digits

  diffFromKey = abs(key - measured);

I am trying to take a measurement from a sensor and subtract it from what it's supposed to be reading. So in this case, it's supposed to read 30 mm, but "measures" 033 mm. (From there I want to do this:)

Code: [Select]

//    if(diffFromKey < 5)
//    lcd.print("Good: highly accurate reading near " + feet + "'");
//    if(diffFromKey <= 9 && diffFromKey >= 5)
//    lcd.print("Good: moderately close reading near " + feet + "'");   
//    if(diffFromKey > 9)
//    lcd.print("Bad: inaccurate reading of sensor.\nTry again or replace sensor.");


Please post your complete program.

I have no idea what you mean when you imply that an int produced by Serial.parseInt() is different from any other sort of int. They are all "regular" ints.

Serial Input Basics - simple reliable ways to receive data.
Two or three hours spent thinking and reading documentation solves most programming problems.


Code: [Select]
m = Serial.parseInt(testStr[len - 4]);     // This worked for just printing it. But it does not work when trying to do more.

It doesn't work because that's not how it works. Read the documentation for parseInt
If you want an integer from that string try
Code: [Select]
m = atoi(&testStr[len - 4]);

Don't send me technical questions via Private Message.

Code: [Select]
m = atoi(&testStr[len - 4]);
Thank you el_supremo!
Robin2, I can post the whole code later. It's just that almost everything else is unrelated.


It's just that almost everything else is unrelated.
Then you write a smaller code without all that irrelevant stuff.  But it should still be complete and exhibit the problem.  It's really hard to debug something from just one line of code. 

When you know what the problem is THEN you can tell me what's relevant to finding it.  But then you are wasting my time since you already know.  If you don't know what the problem is then don't pretend that you know what is relevant to finding it. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Jul 27, 2018, 10:38 pm Last Edit: Jul 27, 2018, 10:40 pm by adamaero
Delta_G, the issue was solved. And yes, I did cut it out. I'm meaning to be completionist; I'll post the whole result of what I'm doing.

Go Up