It seems floating point is bad?

Discussion in 'Arduino IDE' started by jdonavan, Dec 1, 2013.

  1. jdonavan

    jdonavan New Member

    Joined:
    Oct 28, 2013
    Messages:
    34
    Likes Received:
    0
    This simple script below will lock up the Arduino on my quad. Anyone with an idea of why that would be? Is it not safe to cast ints to doubles (or vice versa)

    Code:
    void setup() {
      Serial.begin(9600);
      delay(5000);
      Serial.println("TEST PROGRAM ");
    }
    
    void loop() {
      int var = 42;
      
      Serial.println("loop start");
      Serial.println(var);
      Serial.println((double)var);
      Serial.println(var * 1.8);
      
      delay(2000);
    }
    // end
    
     
  2. jdonavan

    jdonavan New Member

    Joined:
    Oct 28, 2013
    Messages:
    34
    Likes Received:
    0
    I've been using Pin 2 myself...

    After producing the simple test case I tried to move forward without trying to send floating point data. Right now I can't send ANY data via the serial connection. Just a simple "Print a dot every two seconds" script produces no output in the serial monitor.
     
  3. Lifeboat_Jim

    Lifeboat_Jim New Member

    Joined:
    Sep 16, 2013
    Messages:
    399
    Likes Received:
    1
    (Ok, I've split out the other persons post and my answer to them)

    For yourself, try rewriting the line 'int var = 42;' to, perhaps, 'int myVar = 42;' and of course change the subsequent two uses of it.

    Note: var is quite often a keyword used to implicitly type a variable, whereas int would explicitly type it.

    I don't know if that is the case here but that's what my experience is telling me in other languages, esp. C#.
     
  4. jdonavan

    jdonavan New Member

    Joined:
    Oct 28, 2013
    Messages:
    34
    Likes Received:
    0
    Var is not a keyword.

    The problem isn't the variable name. Everything is fine until you you try to call the Serial print functions with floating point number.
     
  5. jdonavan

    jdonavan New Member

    Joined:
    Oct 28, 2013
    Messages:
    34
    Likes Received:
    0
    Though at this point the Serial communication is completely broken for me. NOTHING comes across.
     
  6. Lifeboat_Jim

    Lifeboat_Jim New Member

    Joined:
    Sep 16, 2013
    Messages:
    399
    Likes Received:
    1
    Agreed. The 'Serial' library isn't performing as the documentation states it ought to.

    The 'Serial' library is documented as supporting "any data type" (not sure they mean that literally, but for sure Strings and Floats are supposed to work out of the box). Clearly it doesn't.

    Here is a workaround. Use an array of chars with a Null terminator.

    char myTestString [ ] = "Test raw char *\0";
    Serial.println(myTestString);
     
  7. jdonavan

    jdonavan New Member

    Joined:
    Oct 28, 2013
    Messages:
    34
    Likes Received:
    0
    Not to be an ass but: How is that remotely a work around?
     
  8. Lifeboat_Jim

    Lifeboat_Jim New Member

    Joined:
    Sep 16, 2013
    Messages:
    399
    Likes Received:
    1
    You're been an a** ;-)

    I cut and pasted that to several posts as several people are having related issues with the Serial library.

    If you can't work out how to convert a float to an array of characters then I'll work out a code snippet for you when I have time. I would have thought this would have got you back on the right track and able to create your own workaround?
     
  9. jdonavan

    jdonavan New Member

    Joined:
    Oct 28, 2013
    Messages:
    34
    Likes Received:
    0
    I'm capable of converting a float to an ASCII string to send across the wire... I can also not use the String class as well. I could refactor all of the Arduino libraries to do the same. That's not a viable workaround.

    Instead of suggesting insulting workarounds why not let us know what's being done to figure out these issues. Right now I don't know if there's a hardware problem, a compiler problem or something else.
     

Share This Page