Go Down

Topic: [SOLVED] Project 3: Serial.print(float) hangs Arduino (Read 6058 times) previous topic - next topic


Jun 30, 2013, 12:58 pm Last Edit: Jun 30, 2013, 02:18 pm by gmf811 Reason: 1
EDIT: Rebuilding cross-avr/gcc with multilib enabled fixed everything that follows...

Hi all, I'm having some teething troubles completing project 3 - "love-o-meter".

Completing the code exactly as per the book, the Arduino reboots, the onboard LED (pin13) lights up and nothing else happens. Mucking about with the code it seems that if I try Serial.print(floating point number) the board fails to pass setup() and locks up. I can work around it (sometimes) by casting to integer, although again it seems to be rather fragile.

As I can only find one other thread from 2010 describing this issue, I suppose it's not a common problem.  Please could someone give me some advice on what in my setup is likely to be causing this?

System: Linux 3.8.13-gentoo x86_64
Arduino version: =dev-embedded/arduino-1.0.5
RXTX version: =dev-java/rxtx-2.2_pre2

Some code:
Code: [Select]
// Constants
const int sensorPin = A0;
const float refTemp = 25.0;

void setup()
 for(int pin=2; pin<5; pin++){
   pinMode(pin, OUTPUT);
   digitalWrite(pin, LOW);
 /* I set up Pin13 driven LOW to confirm that
    the board successfully passed setup().
    When I try to write Serial.print(floating point)
    later in the project, Pin13 stays high suggesting
    that it doesn't pass setup() */
 pinMode(13, OUTPUT);
 digitalWrite(13, LOW);

void loop()
 int sensorVal = analogRead(sensorPin);
 Serial.print("S: ");
 Serial.print(", V: ");
 float v = (sensorVal/1024.0) * 5.0;
 float t = (v - 0.5) * 100;
 // Uncomment the below to hang the Arduino

 /* I moved the last bits of Serial.print  
    below the LED code so that I could work
    on the temperature and voltage variables
    without having to edit this */
   digitalWrite(2, LOW);
   digitalWrite(3, LOW);
   digitalWrite(4, LOW);
 } else if(t>=refTemp+2 && t < refTemp+4) {
   digitalWrite(2, HIGH);
   digitalWrite(3, LOW);
   digitalWrite(4, LOW);
 } else if(t>=refTemp+4 && t < refTemp+6) {
   digitalWrite(2, HIGH);
   digitalWrite(3, HIGH);
   digitalWrite(4, LOW);
 } else if(t>=refTemp+6) {
   digitalWrite(2, HIGH);
   digitalWrite(3, HIGH);
   digitalWrite(4, HIGH);
 /* The following works:
 Serial.print(", mV: ");
 v = v*1000;
 Serial.print(", T: ");
 /* The following workaround assembles T as
    two integers with a text decimal place
    but if I do this then I can't print
    v or int(v) without a lock up.
    I certainly can't have both t and v to
    two decimal places.
 Serial.print(", T: ");
   t = t - int(t);
 } else {
   t = int(t) - t;
 t = t*1000;
 Serial.print(int(t)); //Serial.println() here would hang the Arduino again
 // Heartbeat, slows readings to 2s
 digitalWrite(13, HIGH);
 digitalWrite(13, LOW);


EDIT: Rebuilding cross-avr/gcc with multilib enabled fixed everything that follows...

I have the same issue and I don't understand how to rebuild... rebuild what exactly? I'm only familiar with the IDE so far. Could you point me in the right direction please?

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131