Pages: [1]   Go Down
Author Topic: [SOLVED] Project 3: Serial.print(float) hangs Arduino  (Read 3720 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
// Constants
const int sensorPin = A0;
const float refTemp = 25.0;

void setup()
{
  Serial.begin(9600);
  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(sensorVal);
  Serial.print(", V: ");
  float v = (sensorVal/1024.0) * 5.0;
  float t = (v - 0.5) * 100;
  // Uncomment the below to hang the Arduino
  //Serial.print(v);

  
  /* 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 */
    
  if(t<refTemp){
    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(int(v));
  Serial.print(", T: ");
  Serial.println(int(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: ");
  Serial.print(int(t));
  if(t>0){
    t = t - int(t);
  } else {
    t = int(t) - t;
  }
  t = t*1000;
  Serial.print(".");
  Serial.print(int(t)); //Serial.println() here would hang the Arduino again
  Serial.println("degC");
  
  // Heartbeat, slows readings to 2s
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}

« Last Edit: June 30, 2013, 07:18:24 am by gmf811 » Logged

Pages: [1]   Go Up
Jump to: