Arduino: 2 digit 7 segment display with temperature sensor

Hello!

I made an 2 digit 7 segment display with a temperature sensor. The temperature sensor is a mcp9700a, the datasheet can be found here . I also use an Arduino nano. So It all works, there is nothing wrong with the circuit or with the code, I just have one question about my code:

My Question:
So In the last section of my code, that starts at: if (Temperature> = 0 && Temperature <41), I use If-Blocks with the help of the number_arrays to assign the respective elements to the digits. So It kinda looks a bit bad to me that I so so many If-blocks and now I wanted to ask if there was any way to improve it so that I don't have to use so many if-blocks.

I dont know if you would need a circuit for that, because I only refer to the last section of the code and there ist no problem with the circuit or the code overall.

const int analogPin = A4;
int temperatureValue  = 0; // Sensor value from the temperature sensor
int tens;
int ones;
 
//The number array Is used to switch the respective LEDs on / off after each digit

int number_array[10][7] =
{ 
  { 0,0,0,0,0,0,1 },    // 0
  { 1,0,0,1,1,1,1 },    // 1
  { 0,0,1,0,0,1,0 },    // 2
  { 0,0,0,0,1,1,0 },    // 3
  { 1,0,0,1,1,0,0 },    // 4
  { 0,1,0,0,1,0,0 },    // 5
  { 0,1,0,0,0,0,0 },    // 6
  { 0,0,0,1,1,1,1 },    // 7
  { 0,0,0,0,0,0,0 },    // 8
  { 0,0,0,1,1,0,0 }     // 9
  };  

void setup() {
  Serial.begin(9600); 
  pinMode(analogPin, INPUT);
  analogReference(INTERNAL);          // Reference to Arduino references: Is a built-in reference, set 
                                                                    to 1.1 V, increases the accuracy of the sensor
  pinMode(A3, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(2, OUTPUT);

}

/*
 * digit1 is the first digit of the 2-digit 7-segment display, represents the tens.
 * i counts the lines (elements) from 0 to 6
 */

void digit1(int i) 
{
  digitalWrite(2, number_array[i][0]);   //The first digit of segment a
  digitalWrite(3, number_array[i][1]);   //The first digit of segment b
  digitalWrite(4, number_array[i][2]);   //The first digit of segment c
  digitalWrite(5, number_array[i][3]);   //The first digit of segment d
  digitalWrite(6, number_array[i][4]);   //The first digit of segment e
  digitalWrite(7, number_array[i][5]);   //The first digit of segment f
  digitalWrite(8, number_array[i][6]);   //The first digit of segment g 
}

/*
 * digit2 is the second digit of the 2-digit 7-segment display, represents the ones.
 * The upper and lower lines of code from this comment are used to display the respective digits on the 2-digit 7-segment display
 */
 
void digit2(int i) 
{
  digitalWrite(9, number_array[i][0]);   //The second digit of segment a
  digitalWrite(10, number_array[i][1]);   //The second digit of segment b
  digitalWrite(11, number_array[i][2]);   //The second digit of segment c
  digitalWrite(12, number_array[i][3]);   //The second digit of segment d
  digitalWrite(A1, number_array[i][4]);   //The second digit of segment e
  digitalWrite(A2, number_array[i][5]);   //The second digit of segment f
  digitalWrite(A3, number_array[i][6]);   //The second digit of segment g
}

void loop() {
  int m = 0;
  // Formation of an average value from 15 values to get better values
  for (int a=0; a<15; a++) {                      
    temperatureValue = analogRead(analogPin);
    m = m + temperatureValue;
  }
  int k;
  k = m/15;
  long Temperature;
  // k is then transferred to the long
  long long_Temperature = 0;
  long_Temperature = (long) k;
  // Conversion in ° C
  //Uref = 1100 mV, mV is converted into ° C under this comment
  Temperature = (long_Temperature*1100)/1023-500;
  if (Temperature >= 0) {
    Temperature = Temperature + 5;                              
  }
  else if (Temperature < 0) {
    Temperature = Temperature - 5;
  }
  Temperature = Temperature/10;     // That is the scaling factor 10mV / ° C from the MCP9700A
  Serial.println("The temperature in ° C is: ");
  Serial.println(Temperature); // Temperature is also displayed in the serial monitor
  delay(750);

// The temperature maximum is 40 degrees

  if (Temperature >= 0 && Temperature < 41) {
      
    ones = Temperature%10;
    tens = Temperature/10;
      
    if (tens == 0) {
      digit1(0);
    }

    else if (tens == 1) {
      digit1(1);
    }

    else if (tens == 2) {
      digit1(2);
    }

    else if (tens == 3) {
      digit1(3);
    }

    else if (tens == 4) {
      digit1(4);
    }
      
    else if (tens == 5) {
      digit1(5);
    }

    else if (tens == 6) {
      digit1(6);
    }
    
    if (ones == 0) {
      digit2(0);
    }

    else if (ones == 1) {
      digit2(1);
    }

    else if (ones == 2) {
      digit2(2);
    }

    else if (ones == 3) {
      digit2(3);
    }

    else if (ones == 4) {
      digit2(4);
    }
      
    else if (ones == 5) {
      digit2(5);
    }

    else if (ones == 6) {
      digit2(6);
    }

    else if (ones == 7) {
      digit2(7);
    }

    else if (ones == 8) {
      digit2(8);
    }

    else if (ones == 9) {
      digit2(9);
    }
  }
}

Why do users choose to ignore the advice on posting code ?

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here to make it easier to read and copy for examination

Sorry, my mistake should be fixed now

Why not simply

digit1(tens);

140 bytes of precious RAM to store 9 bytes-worth of data is not a good ratio.

Hello!

you mean like this? I tested the code below and it works. Thanks for the answer!

const int analogPin = A4;
int temperatureValue  = 0; // Sensor value from the temperature sensor
int tens;
int ones;
 
//The number array Is used to switch the respective LEDs on / off after each digit

int number_array[10][7] =
{ 
  { 0,0,0,0,0,0,1 },    // 0
  { 1,0,0,1,1,1,1 },    // 1
  { 0,0,1,0,0,1,0 },    // 2
  { 0,0,0,0,1,1,0 },    // 3
  { 1,0,0,1,1,0,0 },    // 4
  { 0,1,0,0,1,0,0 },    // 5
  { 0,1,0,0,0,0,0 },    // 6
  { 0,0,0,1,1,1,1 },    // 7
  { 0,0,0,0,0,0,0 },    // 8
  { 0,0,0,1,1,0,0 }     // 9
  };  

void setup() {
  Serial.begin(9600); 
  pinMode(analogPin, INPUT);
  analogReference(INTERNAL);          // Reference to Arduino references: Is a built-in reference, set 
                                                                    to 1.1 V, increases the accuracy of the sensor
  pinMode(A3, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(2, OUTPUT);

}

void loop() {
  int m = 0;
  // Formation of an average value from 15 values to get better values
  for (int a=0; a<15; a++) {                      
    temperatureValue = analogRead(analogPin);
    m = m + temperatureValue;
  }
  int k;
  k = m/15;
  long Temperature;
  // k is then transferred to the long
  long long_Temperature = 0;
  long_Temperature = (long) k;
  // Conversion in ° C
  //Uref = 1100 mV, mV is converted into ° C under this comment
  Temperature = (long_Temperature*1100)/1023-500;
  if (Temperature >= 0) {
    Temperature = Temperature + 5;                              
  }
  else if (Temperature < 0) {
    Temperature = Temperature - 5;
  }
  Temperature = Temperature/10;     // That is the scaling factor 10mV / ° C from the MCP9700A
  Serial.println("The temperature in ° C is: ");
  Serial.println(Temperature); // Temperature is also displayed in the serial monitor
  delay(750);

    ones = Temperature%10;
    tens = Temperature/10;
      
  digitalWrite(2, number_array[tens][0]);   //The first digit of segment a
  digitalWrite(3, number_array[tens][1]);   //The first digit of segment b
  digitalWrite(4, number_array[tens][2]);   //The first digit of segment c
  digitalWrite(5, number_array[tens][3]);   //The first digit of segment d
  digitalWrite(6, number_array[tens][4]);   //The first digit of segment e
  digitalWrite(7, number_array[tens][5]);   //The first digit of segment f
  digitalWrite(8, number_array[tens][6]);   //The first digit of segment g 

  digitalWrite(9, number_array[ones][0]);   //The second digit of segment a
  digitalWrite(10, number_array[ones][1]);   //The second digit of segment b
  digitalWrite(11, number_array[ones][2]);   //The second digit of segment c
  digitalWrite(12, number_array[ones][3]);   //The second digit of segment d
  digitalWrite(A1, number_array[ones][4]);   //The second digit of segment e
  digitalWrite(A2, number_array[ones][5]);   //The second digit of segment f
  digitalWrite(A3, number_array[ones][6]);   //The second digit of segment g

}

@OP
There is a nice Library named SevSeg.h which you can use to drive your 2-digit multiplexed type 7-segment Display Unit with temperature value. You can read and practice the post of this link:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.