简体   繁体   中英

Displaying negative numbers in Arduino

My friend and I are wiring/coding a digital thermometer on an Arduino board, and I'm writing the code. We've got the thermometer working great, and the basic temp data going to the 4-digit 7-segment LED screen we're using for output. I'm trying to write the code to display negative (sub-zero) temperatures, and am having trouble getting proper output. Instead of outputting a negative sign, it outputs an 8.

Here's the loop() method:

void loop(void) {
 int temp = getTemp();
 boolean neg = false;
 if (temp < 0) {
   // Since the temperature is negative, multiplying it by -2 and adding it
   // to itself gives us the absolute value of the number
   temp += (temp * (-2));
   // We set the neg boolean to true, indicating that we're dealing with a negative number
   neg = true;
 }
 displayNumber(temp, neg);
}

Here's the (truncated) displayNumber() method:

void displayNumber(int toDisplay, boolean negative) {

int num = toDisplay;

// The digits are 1-4, left to right
for(int digit = 4; digit > 0 ; digit--) {
//Turn on a digit for a short amount of time
switch(digit) {
case 1:
  // The leftmost digit only needs to be on for temps 100.0 or above, 
  // or to display the negative sign for temps -10.0 or below
  if (num >= 1000 || (num >= 100 && negative == true)) {
    digitalWrite(digit1, HIGH);
    }
  if (num >= 100 && negative == true) {
    lightNumber(11);
  }
  break;
case 2:
  // Only needs to be on for temps 10.0 degrees or above, or 
  // for single-digit subzero temps.
  if (num >= 100 || negative == true) {
    digitalWrite(digit2, HIGH);
  }
  if (num < 100 && negative == true) {
    lightNumber(11);
  }
  break;
case 3:
  digitalWrite(digit3, HIGH);
  break;
case 4:
  digitalWrite(digit4, HIGH);
  break;
}

//Turn on the right segments for this digit
lightNumber(toDisplay % 10);
toDisplay /= 10;

//Turn off all segments
lightNumber(10); 

//Turn off all digits
digitalWrite(digit1, LOW);
digitalWrite(digit2, LOW);
digitalWrite(digit3, LOW);
digitalWrite(digit4, LOW);    
}
}

...And the code for the lightNumber() method turns the segments on or off properly for the numbers 0-9, with 10 being all segments off, and 11 being only the center segment on, for a negative sign. It uses a switch statement with the integer parameter as the switch. The problem is, when I send displayNumber() a negative value, instead of a negative sign in front of the number, I get an eight displayed where the negative sign should be. Any ideas why?

I think you're overthinking your if statements. In your version both if statements are executed when the number is negative. Try this:

case 1:
  // The leftmost digit only needs to be on for temps 100.0 or above, 
  // or to display the negative sign for temps -10.0 or below
  if (num >= 1000 ){
    digitalWrite(digit1, HIGH);
    }
  if (num >= 100 && negative == true) {
    lightNumber(11);
  }
  break;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM