K_njb
K_njb

Reputation: 1

Displaying '-' sign on seven segment display for negative temperature

I'm currently working on Arduino Nano. My task is to write a code to display the ambient temperature (-19 to 50) on the seven segment display using the temperature sensor. My code is almost correct. It can display positive temperature perfectly on the seven segment display. However my code still cannot read and display the '-' sign for negative temperatures. Here is the current code:

`

`
// Pins for seven-segment display segments
const int segmentPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, A2, A3};
const int numSegments = 14;

float temperature = 0.0;
float temperatureSum = 0.0;
int temperatureCount = 0;

void setup() {
  for (int i = 0; i < numSegments; i++) {
    pinMode(segmentPins[i], OUTPUT);
  }

  Serial.begin(9600);
  analogReference(INTERNAL);
}

void loop() {
  int temperatureSum = 0;
  for (int i = 0; i < 10; i++) {
    long sensorValue = analogRead(A1);
    Serial.println(sensorValue);
    long voltage =  (sensorValue) * (1100 / 1023.0);
    long temperature = (voltage - 500) / 10;
    temperatureSum += temperature;
    delay(10);
  }
  int averagedTemperature = temperatureSum / 10;

  // Test Wert
  averagedTemperature = -13;

  Serial.println(averagedTemperature);
  /*float averagedTemperature = temperatureSum / 10.0;
  averagedTemperature = round(averagedTemperature * 10.0) / 10.0;*/

  if (averagedTemperature > 0) {
    averagedTemperature += 0.5;
  } else {
    averagedTemperature -= 0.5;
  }

 if (averagedTemperature < -19.0) {
    averagedTemperature = -19.0;
  } else if (averagedTemperature > 50.0) {
    averagedTemperature = 50.0;
  }

  //displayTemperature(averagedTemperature);
  displayTemperature(averagedTemperature);
  //Serial.println(averagedTemperature);

  delay(500);
}

void displayTemperature(float temp) {
  int tens;
  int ones;

  if (temp >= 0) {
    tens = (int)(temp / 10);
    ones = (int)(temp) % 10;
  } else {
    temp = -temp; // Convert negative temperature to positive 
    tens = -(int)(temp / 10);
    ones = -(int)(temp) % 10;
    tens = -tens; // Display negative signs for tens digit
  }

   if (tens == 0 && ones != 0 && temp < 0) {
    tens = -10; // Display '-' sign for -1 to -9
    ones = -ones; // Calculate the positive ones digit for negative temperature
   }

  if (tens == 0 && temp < 0) {
    if (temp > -10) {
      tens = -1; // Display -1 for temperature between -10 and -19
    } else {
      tens = -(int)(temp / 10); // Display tens digit for temperature between -10 and -19
      ones = -(int)(temp) % 10; // Calculate the ones digit for negative temperature
    }
    
  }

  displayDigit(tens, 0);
  delay(5);
  displayDigit(ones, 7);
}

void displayDigit(int digit, int startIndex) {
  const byte digitSegments[] = {
    B0000001,  // 0
    B1001111,  // 1
    B0010010,  // 2
    B0000110,  // 3
    B1001100,  // 4
    B0100100,  // 5
    B0100000,  // 6
    B0001111,  // 7
    B0000000,  // 8
    B0000100,  // 9
  };

  const byte digitSegmentsNegative[] = {
    B1111110, // displaying '-' symbol for negative temperatures
    B1001110  // -1
  };

  const byte* digitSegmentsToDisplay;

  if (digit >= 0) {
    digitSegmentsToDisplay = digitSegments;
  } else {
    digitSegmentsToDisplay = digitSegmentsNegative;
    digit = -digit; // Convert digit to positive for indexing digitSegmentsnegative
  }

  for (int i = 0; i < 7; i++) {
    digitalWrite(segmentPins[startIndex + i], bitRead(digitSegments[digit], 6 - i));
  }
}    

For example, when the temperature (-1 to -9) is -8 degree Celcius, the first digit on the seven segment display should show the '-' sign and the second digit show 8. With the code, the first digit displays 0 instead of '-' sign and second digit displays 8 (which is good). Furthermore, when the temperature (-10 to -19) is -15 degree Celcius, the first digit should display the '-' sign and 1 together side-by-side (so the first digit would show -1) and second digit 5 (is already good). With the code, the first digit only display 1 without the '-' sign and second digit display 5 correctly. It seems the code somehow doe snot recognise the '-' yet, although I've already written the binary code for '-' and '-1'.

Upvotes: 0

Views: 278

Answers (1)

Delta_G
Delta_G

Reputation: 3243

 else {
temp = -temp; // Convert negative temperature to positive 
tens = -(int)(temp / 10);
ones = -(int)(temp) % 10;
tens = -tens; // Display negative signs for tens digit

}

Here if you have a negative number you flip it to positive with that first line. Then on each of the following lines you flip it back to negative with another minus sign. So if you started with a negative number, then tens and ones now have negative numbers. Then you flip tens again. So it is once again positive. I think you want to send tens as a negative number and ones as a positive.

But really I wouldn't do it this way. I would have the display digits function just take the digit to display and not worry about the minus sign. You could have this calling code write the minus sign first. So imagine that instead of calling to display digit twice, you call three times, once to display a minus sign, then a second time with tens then a third time with ones.

Upvotes: 0

Related Questions