Im trying to work on a 7-segment 4-digit display but the code isnt working

Im trying to code a 7-segment 4-digit display. I keep getting a error message but i dont know what to do. Im honestly never worked on this try of project before but im trying it out. I was hoping to program the display so that all all of the pins are being used, seeing as i was using it to show temp. but ive never built nor code something like this so i had to pick through other programs for coding parts.

I have...

void setup() {
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(22, OUTPUT);
  pinMode(23, OUTPUT);
  pinMode(24, OUTPUT);
  pinMode(25, OUTPUT);
  pinMode(26, INPUT_PULLUP);//
  Serial.begin(9600);// serial monitor speed
  Serial.println("Hi:)");//check serial monitor
  pinMode(pinA, OUTPUT);
  pinMode(pinB, OUTPUT);
  pinMode(pinC, OUTPUT);
  pinMode(pinD, OUTPUT);
  pinMode(pinE, OUTPUT);
  pinMode(pinF, OUTPUT);
  pinMode(pinG, OUTPUT);
  pinMode(D1, OUTPUT);
  pinMode(D2, OUTPUT);
  pinMode(D3, OUTPUT);
  pinMode(D4, OUTPUT);
  pinMode(DP, OUTPUT);
}
  void showdigits (int number)
{
int pinA = 2;
int pinB = 3;
int pinC = 4;
int pinD = 5;
int pinE = 6;
int pinF = 7;
int pinG = 8;
int D1 = 9;
int D2 = 10;
int D3 = 11;
int D4 = 12;
}
void loop() {

// THIS MOVES BELOW CODE TO THE NEXT LCD BLOCK
digitalWrite(D1, HIGH);
digitalWrite(D2, LOW);
digitalWrite(D3, LOW);
digitalWrite(D4, LOW);
//THIS ENDS THE MOVEMENT AND REMAINS ON THE LAST BLOCK FOR THE FOLLOWING DIGITS

//0
 digitalWrite(pinA, LOW); 
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, HIGH);
delay(1000); // wait for a second

// THIS MOVES BELOW CODE TO THE NEXT LCD BLOCK
digitalWrite(D1, LOW);
digitalWrite(D2, HIGH);
digitalWrite(D3, LOW);
digitalWrite(D4, LOW);
//THIS ENDS THE MOVEMENT AND REMAINS ON THE LAST BLOCK FOR THE FOLLOWING DIGITS

}
}

The intention of this project was to have a one of the 2 rows of red and green LEDs light up when the temp. was written above 99.4 F and light up green if it was under 99.4 F. So i decide a push button was the easiest to code from common knowledge i already have. Can anyone help me with figuring out this code. Its honestly way about what i should be doing or knowing im general cause im only in Highschool but I was hoping to have this project work for STEM class.

Hi

what board are you using for this project?
Uno, mini, ESPxx, Due, etc......?

I forgot to mention that im not actually using a temp. sensor in this project i would just be having it display a temp. then light us the color thats either red or green depend on if its above 99.4F or not

Im Using a Arduino Mega seeign as i needed to more Digital pins then the uno had.

What is this nonsense suppose to do? Variables should be outside functions!

Hi

I corrected the error message, but I don't understand what you want to do with this code.

int pinA = 2;
int pinB = 3;
int pinC = 4;
int pinD = 5;
int pinE = 6;
int pinF = 7;
int pinG = 8;
int D1 = 9;
int D2 = 10;
int D3 = 11;
int D4 = 12;

void setup() {
  //  pinMode(2, OUTPUT);
  //  pinMode(3, OUTPUT);
  //  pinMode(4, OUTPUT);
  //  pinMode(5, OUTPUT);
  //  pinMode(6, OUTPUT);
  //  pinMode(7, OUTPUT);
  //  pinMode(8, OUTPUT);
  //  pinMode(9, OUTPUT);
  //  pinMode(10, OUTPUT);
  //  pinMode(11, OUTPUT);
  //  pinMode(12, OUTPUT);

  pinMode(13, OUTPUT);
  pinMode(22, OUTPUT);
  pinMode(23, OUTPUT);
  pinMode(24, OUTPUT);
  pinMode(25, OUTPUT);
  pinMode(26, INPUT_PULLUP);//

  pinMode(pinA, OUTPUT);
  pinMode(pinB, OUTPUT);
  pinMode(pinC, OUTPUT);
  pinMode(pinD, OUTPUT);
  pinMode(pinE, OUTPUT);
  pinMode(pinF, OUTPUT);
  pinMode(pinG, OUTPUT);
  pinMode(D1, OUTPUT);
  pinMode(D2, OUTPUT);
  pinMode(D3, OUTPUT);
  pinMode(D4, OUTPUT);
  Serial.begin(9600);// serial monitor speed
  Serial.println("Hi:)");//check serial monitor
  //  pinMode(DP, OUTPUT);
}
void showdigits (int number)
{
  //  int pinA = 2;
  //  int pinB = 3;
  //  int pinC = 4;
  //  int pinD = 5;
  //  int pinE = 6;
  //  int pinF = 7;
  //  int pinG = 8;
  //  int D1 = 9;
  //  int D2 = 10;
  //  int D3 = 11;
  //  int D4 = 12;
}
void loop() {

  // THIS MOVES BELOW CODE TO THE NEXT LCD BLOCK
  digitalWrite(D1, HIGH);
  digitalWrite(D2, LOW);
  digitalWrite(D3, LOW);
  digitalWrite(D4, LOW);
  //THIS ENDS THE MOVEMENT AND REMAINS ON THE LAST BLOCK FOR THE FOLLOWING DIGITS

  //0
  digitalWrite(pinA, LOW);
  digitalWrite(pinB, LOW);
  digitalWrite(pinC, LOW);
  digitalWrite(pinD, LOW);
  digitalWrite(pinE, LOW);
  digitalWrite(pinF, LOW);
  digitalWrite(pinG, HIGH);
  delay(1000); // wait for a second

  // THIS MOVES BELOW CODE TO THE NEXT LCD BLOCK
  digitalWrite(D1, LOW);
  digitalWrite(D2, HIGH);
  digitalWrite(D3, LOW);
  digitalWrite(D4, LOW);
  //THIS ENDS THE MOVEMENT AND REMAINS ON THE LAST BLOCK FOR THE FOLLOWING DIGITS

}

That was a reminder to myself for which pins i had where because i dont have do memory of attention spand. I should probably of written this as a comment instead

Thank you, as of right now im nowhere close to finishing this code i was just trying to test it out to see if i can get the digits to show up on the display first.

…a simple reason why you should declare and name variables as early in the code as possible.

Otherwise, you have some of the structures and principles ‘right’, but definitely need to rethink your program flow.

All variables, Always? Or just these in this case?

I'm assuming you're trying to multiplex 4 seven-segment displays. You don't provide much information so I had to make some guesses... Give this a try:

#define SEG_ON      LOW     //logic levels to turn segments on or off
#define SEG_OFF     HIGH

#define DIG_ON      HIGH    //logic levels to turn digits on or off
#define DIG_OFF     LOW

//frame period is 4x (DIGIT_TIME + DIGIT_INTER_TIME)
//  4 x (6240 + 10) == 25000uS or 40Hz
//  this sort of rate is required with LEDs to avoid flicker
const uint32_t DIGIT_TIME = 6240ul;
const uint32_t DIGIT_INTER_TIME = 10ul;

const uint8_t Segs[] = { 2, 3, 4, 5, 6, 7, 8 };
const uint8_t Digits[] = { 9, 10, 11, 12 };

const uint8_t digitPatterns[] = 
{
    //abcdefg-
    0b00000010,     //0  for this table, a '0' turns a segment 'on'
    0b10011110,     //1
    0b00100100,     //2
    0b00001100,     //3
    0b10011000,     //4
    0b01001000,     //5
    0b01000000,     //6
    0b00011111,     //7
    0b00000000,     //8
    0b00001000      //9
    
};

//individual digits to display
uint8_t
    digitVals[4] = {0};

//whole value
uint16_t
    valueToDisplay = 0;

uint32_t
    tNow;
    
void setup() 
{
    //prep segs and digits
    for( uint8_t i=0; i<7; i++ )
    {
        pinMode( Segs[i], OUTPUT );
        digitalWrite( Segs[i], SEG_OFF );
        
    }//for
    
    for( uint8_t i=0; i<4; i++ )
    {
        pinMode( Digits[i], OUTPUT );
        digitalWrite( Digits[i], DIG_OFF );
        
    }//for

    //initialize the display digits
    calcDigits( valueToDisplay );
                
}//setup

void loop( void )
{
    static uint32_t
        tCount = 0ul;
    static uint16_t 
        count = 0;

    //for demo, tick up the value to display once every 100mS or 10Hz
    tNow = micros();
    if( (tNow - tCount) >= 100000ul )
    {
        tCount = tNow;
        
        valueToDisplay++;
        if( valueToDisplay == 10000ul )
            valueToDisplay = 0;

        calcDigits( valueToDisplay );
            
    }//if

    //must be called very rapidly to maintain digit multiplexing
    muxDigits();    
    
}//loop

void calcDigits( uint16_t val )
{
    digitVals[0] = val / 1000;
    digitVals[1] = (val % 1000) / 100;
    digitVals[2] = (val % 100) / 10;
    digitVals[3] = val % 10;
    
}//calcDigits

void muxDigits( void )
{
    static uint32_t
        tDigit = 0ul;       
    static uint8_t
        digitIdx = 0;
    uint8_t
        mask,
        pattern;

    tNow = micros();
    if( (tNow - tDigit) <= DIGIT_TIME )
        return;
        
    //turn off the current digit
    digitalWrite( Digits[digitIdx], DIG_OFF );

    //dead time before turning on the next to prevent "ghosting"
    delayMicroseconds( DIGIT_INTER_TIME );

    //bump the digit to be displayed; when we've displayed
    //the last, go back to the first
    digitIdx++;
    if( digitIdx == 4 )
        digitIdx = 0;

    //write the segments for the next digit
    pattern = digitPatterns[ digitVals[digitIdx] ];
    mask = 0x80;
    for( uint8_t i=0; i<7; i++ )
    {
        digitalWrite( Segs[i], (pattern & mask) ? SEG_OFF:SEG_ON );
        mask >>= 1;
        
    }//for

    //turn the next digit on
    digitalWrite( Digits[digitIdx], DIG_ON );

    tDigit = tNow;
                    
}//muxDigits

@hps461003

There are some problems in your circuit (which you have not shown) ; where, same DPin (digital pin) has been assigned to more than one IO lines. So, build your circuit step-by-step as per following instructions:

1. Connect Red Leds and Green Leds with MEGA as per diagram of Fig-1. This circuit is approximately made based on the DPin information of your sketch.
MegaLedSw
Figure-1:

2. Connect switch K1 as per Fig-1. K1 is a spring-returned push-type switch.

3. Upload the following sketch in MEGA. This program turns ON Red Leds (Green Leds remain OFF) when K1 is pressed and turns ON Green Leds (Red Leds remain OFF) when K1 is released. Initially all Leds are OFF.

void setup()
{
 Serial.begin(9600);
 for (int i = 2; i < 14; i++)
 {
   pinMode(i, OUTPUT);
 }
 pinMode(22, OUTPUT);
 pinMode(23, OUTPUT);
 pinMode(24, OUTPUT);
 pinMode(25, OUTPUT);
 //------------------
 pinMode(26, INPUT_PULLUP);
 //-----------------------

 for (int i = 2; i < 14; i++)
 {
   digitalWrite(i, LOW);
 }
 for (int i = 22; i < 26; i++)
 {
   digitalWrite(i, LOW);
 }
}

void loop()
{
 if (digitalRead(26) == LOW) //K1 is pressed
 {
   for (int i = 2; i < 10; i++)   //Red Leds are ON; Temp > 99.4 degF
   {
     digitalWrite(i, HIGH);
   }
 }
 else
 {
   for (int i = 10; i < 14; i++)   //Green Leds are ON; Temp < 99.4 degF
   {
     digitalWrite(i, LOW);
   }
   for (int i = 22; i < 26; i++)
   {
     digitalWrite(i, LOW);
   }
 }
}

4. Connect the CA-type display devices (DP1 - DP3) as per Fig-1.
5. Upload the following sketch to see 3 at DP1 position of the display unit. All other display positions remain blank.

void setup()
{
  Serial.begin(9600);
  for (int i = 27; i < 39; i++)
  {
    pinMode(i, OUTPUT);
  }

  byte y = 0b10110000; //DP, G, .., B, A
  for (int i = 34, j = 0; i >= 27; i--, j++)
  {
    bool n = bitRead(y, j);//read bits of y
    digitalWrite(i, n);
  }
  digitalWrite(38, HIGH); //HIGH (~5V) at D1
  digitalWrite(37, LOW);
  digitalWrite(36, LOW);
  digitalWrite(35, LOW);
}

void loop()
{

}

6. Upload the following sketch (not tested) to see that 12.34 appears on the display unit.

#include<SevSeg.h>
SevSeg sevSeg;
int numberToShow = 0x994F;

void setup()
{
  Serial.begin(9600);

  byte segDPins[] = {34, 33, 32, 31, 30, 29, 28, 27}; //DPin-34 = Seg-A, ..
  byte caDPins[] = {38, 37, 36, 35}; //DPin-38 = ca0, ...
  sevSeg.begin(COMMON_ANODE, 4, caDPins, segDPins, false, false, false, false);
  sevSeg.setNumber(numberToShow, 2, HIGH); //99.4F
}

void loop()
{
  sevSeg.refreshDisplay();
}

7. Upload the following sketch to see that 99.4F appears on display unit when you press the switch and 93.5F appears on the display when K1 is released.
(...pending....)

8. Now, combine the sketches of Step-3 and 7 to see that both CA7SD unit and Leds are activated correctly in response to the close/open condition of K1.
(...pending...)

Reading: on SevSeg.h
The Library named SevSeg.h contains ready-made codes, routines, functions, and LUT tables written by somebody else, and it can be used to drive multi-digit 7-segment Display Unit. Now, the user is not required to execute pinMode(), digitalWrite(), bitRead(), delay(), for() and other commands in the sketch. The Library must be down loaded from the internet and then it has to be included in the sketch and IDE.

The Library contains following functions and declarations to drive multiplexed 7-seg display unit.

(1) sevSeg.begin(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);

 sevSeg.begin(displayType, numDigits, ccDPins, segDPins, softCurrentLimitResistor,
updateWithDelays, leadingZeros, disableDecPoint);

arg1= Display type : Value = COMMON_CATHODE or COMMON_ANODE.
When COMMOM_CATHODE is selected, the program automatically consults digit-vs-ccCode LUT Table (Fig-1.7) to collect the ccCodes for the digits of display unit.

arg2 = Number of digits to be shown: Value = 1 to 8

arg3 = an array that contains DPins with which the cc-pins of the digits are to be connected

arg4 = an array that contains DPins with which the segments-pins of the digits are to be connected

arg5 = SoftcurrentLimitResistor (Software controlled): Value = true or false
true means software controlled current limit resistor;
false means that the current limiting resistors are installed manually.

arg6 = updateWithDelays : true or false (choose false; why? No idea.)

arg7 = leadingZeros : Value = true or false;
true means keep the leading zeroes like: 05

arg8 = disableDecPoint : Value = true or false;
false means that we can show decimal point at any position of the display unit. The position is determined by sevseg.SetNumber() function.

sevSeg.begin(COMMON_ANODE, 4, caDPins, segDPins, false, false, false, false); //yes! decimal point

(2) sevSeg.setNumber(arg1, arg2, arg3);
arg1 = Number to be shown
arg2 = position of decimal point counting from right side of display unit
arg3 = arg1's base (LOW = decimal; HIGH = HEX)

(3) sevSeg.refreshDisplay();

Hi @GolamMostafa
Where did that schematic come from.
Is it @hps461003 ?

If not then let get @hps461003 to please post a circuit diagram.

Tom... :grinning: :coffee: :+1: :australia:

It is approximately made based on the DPin information found in @hps461003 sketch.

Ment to type 'These variables' - EBBAK issue I think.

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