Print 3-digit value on Seven Segment Display

Hello, there !

In one of the my project i am trying to print 3-digit (+Ve and -Ve) hard code decimal value on seven segment display. I am able to display digit 0 to 8 for all 3 digit but when i pass value “9” like random value “889” it shows only activated “G-segment” only for number 9 and 88 is showing correct in both +Ve and -Ve value. The code written for digit 9 is also ok but unable to find why it is like.
Currently i have 8-digit Seven segment display form there i am using 3 digit of it.

References :
1] abs()

Source code :

#include <LiquidCrystal.h>
 
// segment pins definitions
#define SegA 0
#define SegB 1
#define SegC 2
#define SegD 3
#define SegE 4
#define SegF 5  
#define SegG 6
#define SegDP 7

#define digit_0 8
#define digit_1 10
#define digit_2 12
#define digit_3 14
#define digit_4 16
#define digit_5 18
#define digit_6 22
#define digit_7 24
 
uint8_t current_digit;
int  temp;

LiquidCrystal lcd (38,37,36,35,34,33);

void setup()
{
    lcd.begin (16,4);
  
    pinMode(SegA, OUTPUT);
    pinMode(SegB, OUTPUT);
    pinMode(SegC, OUTPUT);
    pinMode(SegD, OUTPUT);
    pinMode(SegE, OUTPUT);
    pinMode(SegF, OUTPUT);
    pinMode(SegG, OUTPUT);
    pinMode(SegDP, OUTPUT);

    pinMode(digit_0, OUTPUT);
    pinMode(digit_1, OUTPUT);   
    pinMode(digit_2, OUTPUT);
    pinMode(digit_3, OUTPUT);    
    pinMode(digit_4, OUTPUT);
    pinMode(digit_5, OUTPUT);    
    pinMode(digit_6, OUTPUT);
    pinMode(digit_7, OUTPUT);  
 
digitalWrite(digit_0,LOW);
digitalWrite(digit_1,LOW);
digitalWrite(digit_2,LOW);
digitalWrite(digit_3,LOW);
digitalWrite(digit_4,LOW);
digitalWrite(digit_5,LOW);
digitalWrite(digit_6,LOW);
digitalWrite(digit_7,LOW);
 
  // Timer1 module overflow interrupt configuration
  TCCR1A = 0;
  TCCR1B = 1;  // enable Timer1 with prescaler = 1 ( 16 ticks each 1 µs)
  TCNT1  = 0;  // set Timer1 preload value to 0 (reset)
  TIMSK1 = 1;  // enable Timer1 overflow interrupt
}
 
ISR(TIMER1_OVF_vect)   // Timer1 interrupt service routine (ISR)
{
digitalWrite(digit_0,LOW);
digitalWrite(digit_1,LOW);
digitalWrite(digit_2,LOW);
digitalWrite(digit_3,LOW);
digitalWrite(digit_4,LOW);
digitalWrite(digit_5,LOW);
digitalWrite(digit_6,LOW);
digitalWrite(digit_7,LOW);
digitalWrite(SegDP,HIGH);
 
  int abs_temp = abs(temp);  // abs: absolute value
  switch (current_digit)
  {
    case 1:
        if(temp < 0)
        {
          disp(10);  // print minus sign (-)
          digitalWrite(digit_6, HIGH);
        }
        else
        {
        digitalWrite(digit_6, LOW);  
        }
      break;

    case 2:
      disp(abs_temp / 100);   
      digitalWrite(digit_5, HIGH);     
      break;
      
    case 3:
      disp( (abs_temp / 10) % 10);   
      digitalWrite(digit_4, HIGH);     
      break;
 
    case 4:
      disp(abs_temp % 10);   
      digitalWrite(digit_3, HIGH);
      digitalWrite(SegDP, LOW);   // ON 
  }
 
  current_digit = (current_digit % 4) + 1;
}
 

void loop()
{
  temp =  -878;  //-878
  
  lcd.setCursor(0,0);
  lcd.print(temp);
  delay(100);  
}
 
void disp(byte number)
{
  switch (number)
  {
    case 0:
      digitalWrite(SegA, HIGH);
      digitalWrite(SegB, HIGH);
      digitalWrite(SegC, HIGH);
      digitalWrite(SegD, HIGH);
      digitalWrite(SegE, HIGH);
      digitalWrite(SegF, HIGH);
      digitalWrite(SegG, LOW);
    break;
 
    case 1:
      digitalWrite(SegA, LOW);
      digitalWrite(SegB, HIGH);
      digitalWrite(SegC, HIGH);
      digitalWrite(SegD, LOW);
      digitalWrite(SegE, LOW);
      digitalWrite(SegF, LOW);
      digitalWrite(SegG, LOW);
      break;
 
    case 2:
      digitalWrite(SegA, HIGH);
      digitalWrite(SegB, HIGH);
      digitalWrite(SegC, LOW);
      digitalWrite(SegD, HIGH);
      digitalWrite(SegE, HIGH);
      digitalWrite(SegF, LOW);
      digitalWrite(SegG, HIGH);
      break;
 
    case 3: 
      digitalWrite(SegA, HIGH);
      digitalWrite(SegB, HIGH);
      digitalWrite(SegC, HIGH);
      digitalWrite(SegD, HIGH);
      digitalWrite(SegE, LOW);
      digitalWrite(SegF, LOW);
      digitalWrite(SegG, HIGH);
      break;
 
    case 4:
      digitalWrite(SegA, LOW);
      digitalWrite(SegB, HIGH);
      digitalWrite(SegC, HIGH);
      digitalWrite(SegD, LOW);
      digitalWrite(SegE, LOW);
      digitalWrite(SegF, HIGH);
      digitalWrite(SegG, HIGH);
      break;
 
    case 5:
      digitalWrite(SegA, HIGH);
      digitalWrite(SegB, LOW);
      digitalWrite(SegC, HIGH);
      digitalWrite(SegD, HIGH);
      digitalWrite(SegE, LOW);
      digitalWrite(SegF, HIGH);
      digitalWrite(SegG, HIGH);
      break;
 
    case 6: 
      digitalWrite(SegA, HIGH);
      digitalWrite(SegB, LOW);
      digitalWrite(SegC, HIGH);
      digitalWrite(SegD, HIGH);
      digitalWrite(SegE, HIGH);
      digitalWrite(SegF, HIGH);
      digitalWrite(SegG, HIGH);
      break;
    
    case 7: 
      digitalWrite(SegA, HIGH);
      digitalWrite(SegB, HIGH);
      digitalWrite(SegC, HIGH);
      digitalWrite(SegD, LOW);
      digitalWrite(SegE, LOW);
      digitalWrite(SegF, LOW);
      digitalWrite(SegG, LOW);
      break;
 
    case 8:
      digitalWrite(SegA, HIGH);
      digitalWrite(SegB, HIGH);
      digitalWrite(SegC, HIGH);
      digitalWrite(SegD, HIGH);
      digitalWrite(SegE, HIGH);
      digitalWrite(SegF, HIGH);
      digitalWrite(SegG, HIGH);
      break;
 
    case 9: 
      digitalWrite(SegA, HIGH);
      digitalWrite(SegB, HIGH);
      digitalWrite(SegC, HIGH);
      digitalWrite(SegD, HIGH);
      digitalWrite(SegE, LOW);
      digitalWrite(SegF, HIGH);
      digitalWrite(SegG, HIGH);

    case 10:  // print -
      digitalWrite(SegA, LOW);
      digitalWrite(SegB, LOW);
      digitalWrite(SegC, LOW);
      digitalWrite(SegD, LOW);
      digitalWrite(SegE, LOW);
      digitalWrite(SegF, LOW);
      digitalWrite(SegG, HIGH);
  }
}

Hello,

You forgot break; in cases 9 and 10, also you don't have a default case.

And same in the switch statement in your ISR, no break; for case 4 (even if it's the last case, it's better to put it anyway) and no default case

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