Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

!7 Segment 4 Digit Display:can't get all digits to display different numbers at once!

Status
Not open for further replies.

red913

Newbie level 5
Newbie level 5
Joined
Mar 28, 2013
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,370
Project: 7 segment LED board with 4 different digits. Each of the individual digits will cycle from 1 to 2 to 3, etc. up to 9, then 0 and start over. There is a separate tactile button for each digit. Button goes to transistor which connects the a pin from the atmega328 chip to ground to signal the action to change numbers. Through testing of various code I've confirmed my circuitry is correct and functioning. So as to the actual code I've written for my project so far:
Code:
//set integers for switches
const int S1 = A3;
const int S2 = A2;
const int S3 = A1;
const int S4 = A0;
//set integers for 7 segment display
const int SegA = 11;
const int SegB = 10;
const int SegC = 9;
const int SegD = 8;
const int SegE = 5;
const int SegF = 4;
const int SegG = 3;
const int SegDP = 2; //decimal point
const int SegCOL = 1; //colon, ":"
//set integers for digits.(i.e D1 is equal to digit 1 and also T1, transistor 1, on board. 
const int D1 = 13;
const int D2 = 12;
const int D3 = 7;
const int D4 = 6;
//track swith read value
int buttonState1 = HIGH;
int buttonState2 = HIGH;
int buttonState3 = HIGH;
int buttonState4 = HIGH;
int counterD1 = 0;
int counterD2 = 0;
int counterD3 = 0;
int counterD4 = 0;


//following section SUPPOSED to turn off all other digits except the one the button was pressed to activate.

void (*DigitFunctions[10])( int );

void turnOffAllExcept(int targetDigit)
{
  byte digitPin[] = {
    13, 12, 7, 6            };

  byte selectedDigit = targetDigit; // any of 0 .. 3

  for (byte n = 0; n < 4; n++) {

    digitalWrite(digitPin[n], LOW); // all off
  }
  digitalWrite(digitPin[selectedDigit], HIGH);
}


void turnOnNumberForDigit(int value, int targetDigit)
{
  void (*turnOnNumber)(int targetDigit);
  turnOnNumber = DigitFunctions[value];//get function N# where # == value
  turnOnNumber(targetDigit);
}
bool didPressButtonDown(int button, int buttonState)
{
  int buttonStatus = digitalRead(button);
  if(buttonStatus == LOW &&  buttonStatus != buttonState){
    buttonState = LOW;
    return true;
  }
  else if(buttonStatus == HIGH && buttonStatus != buttonState){
    buttonState = HIGH;
  }
  return false;
}

//N1
void turnOnNumber1(int targetDigit){
  digitalWrite(SegA, HIGH);
  digitalWrite(SegB, LOW);
  digitalWrite(SegC, LOW);
  digitalWrite(SegD, HIGH);
  digitalWrite(SegE, HIGH);
  digitalWrite(SegF, HIGH);
  digitalWrite(SegG, HIGH);
  digitalWrite(SegDP, HIGH);
  turnOffAllExcept(targetDigit);

}

//N2
void turnOnNumber2(int targetDigit){
  digitalWrite(SegA, LOW);
  digitalWrite(SegB, LOW);
  digitalWrite(SegC, HIGH);
  digitalWrite(SegD, LOW);
  digitalWrite(SegE, LOW);
  digitalWrite(SegF, HIGH);
  digitalWrite(SegG, LOW);
  digitalWrite(SegDP, HIGH);
  turnOffAllExcept(targetDigit);
}

void turnOnNumber3(int targetDigit){
  digitalWrite(SegA, LOW);
  digitalWrite(SegB, LOW);
  digitalWrite(SegC, LOW);
  digitalWrite(SegD, LOW);
  digitalWrite(SegE, HIGH);
  digitalWrite(SegF, HIGH);
  digitalWrite(SegG, LOW);
  digitalWrite(SegDP, HIGH);
  turnOffAllExcept(targetDigit);
}

void turnOnNumber4(int targetDigit){
  digitalWrite(SegA, HIGH);
  digitalWrite(SegB, LOW);
  digitalWrite(SegC, LOW);
  digitalWrite(SegD, HIGH);
  digitalWrite(SegE, HIGH);
  digitalWrite(SegF, LOW);
  digitalWrite(SegG, LOW);
  digitalWrite(SegDP, HIGH);
  turnOffAllExcept(targetDigit);
}

void turnOnNumber5(int targetDigit){
  digitalWrite(SegA, LOW);
  digitalWrite(SegB, HIGH);
  digitalWrite(SegC, LOW);
  digitalWrite(SegD, LOW);
  digitalWrite(SegE, HIGH);
  digitalWrite(SegF, LOW);
  digitalWrite(SegG, LOW);
  digitalWrite(SegDP, HIGH);
  turnOffAllExcept(targetDigit);
}

void turnOnNumber6(int targetDigit){
  digitalWrite(SegA, LOW);
  digitalWrite(SegB, HIGH);
  digitalWrite(SegC, LOW);
  digitalWrite(SegD, LOW);
  digitalWrite(SegE, LOW);
  digitalWrite(SegF, LOW);
  digitalWrite(SegG, LOW);
  digitalWrite(SegDP, HIGH);
  turnOffAllExcept(targetDigit);
}

void turnOnNumber7(int targetDigit){
  digitalWrite(SegA, LOW);
  digitalWrite(SegB, LOW);
  digitalWrite(SegC, LOW);
  digitalWrite(SegD, HIGH);
  digitalWrite(SegE, HIGH);
  digitalWrite(SegF, HIGH);
  digitalWrite(SegG, HIGH);
  digitalWrite(SegDP, HIGH);
  turnOffAllExcept(targetDigit);
}

void turnOnNumber8(int targetDigit){
  digitalWrite(SegA, LOW);
  digitalWrite(SegB, LOW);
  digitalWrite(SegC, LOW);
  digitalWrite(SegD, LOW);
  digitalWrite(SegE, LOW);
  digitalWrite(SegF, LOW);
  digitalWrite(SegG, LOW);
  digitalWrite(SegDP, HIGH);
  turnOffAllExcept(targetDigit);
}
void turnOnNumber9(int targetDigit){
  digitalWrite(SegA, LOW);
  digitalWrite(SegB, LOW);
  digitalWrite(SegC, LOW);
  digitalWrite(SegD, HIGH);
  digitalWrite(SegE, HIGH);
  digitalWrite(SegF, LOW);
  digitalWrite(SegG, LOW);
  digitalWrite(SegDP, HIGH);
  turnOffAllExcept(targetDigit);
}

void turnOnNumber0(int targetDigit){
  digitalWrite(SegA, LOW);
  digitalWrite(SegB, LOW);
  digitalWrite(SegC, LOW);
  digitalWrite(SegD, LOW);
  digitalWrite(SegE, LOW);
  digitalWrite(SegF, LOW);
  digitalWrite(SegG, HIGH);
  digitalWrite(SegDP, HIGH);
  turnOffAllExcept(targetDigit);
}




void setup() {
  //set switches as inputs with internal pullup resistor
  pinMode(S1, INPUT_PULLUP);
  pinMode(S2, INPUT_PULLUP);
  pinMode(S3, INPUT_PULLUP);
  pinMode(S4, INPUT_PULLUP);
  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(SegCOL, OUTPUT);
  pinMode(D1, OUTPUT);
  pinMode(D2, OUTPUT);
  pinMode(D3, OUTPUT);
  pinMode(D4, OUTPUT);

  DigitFunctions[0] = &turnOnNumber0;
  DigitFunctions[1] = &turnOnNumber1;
  DigitFunctions[2] = &turnOnNumber2;
  DigitFunctions[3] = &turnOnNumber3;
  DigitFunctions[4] = &turnOnNumber4;
  DigitFunctions[5] = &turnOnNumber5;
  DigitFunctions[6] = &turnOnNumber6;
  DigitFunctions[7] = &turnOnNumber7;
  DigitFunctions[8] = &turnOnNumber8;
  DigitFunctions[9] = &turnOnNumber9;






}




void loop() {

  //Keeps colon sign on always
  digitalWrite(SegCOL, LOW);

  if(didPressButtonDown(S1, buttonState1)){
    if(counterD1 == 9){
      counterD1 = -1;// it will be incremented to 0 on the next line
    }
    turnOnNumberForDigit(++counterD1, D1);
    digitalWrite(D1,HIGH);
    delay(5);  

  }
  if(didPressButtonDown(S2, buttonState2)){
    if(counterD2 == 9){
      counterD2 = -1;// it will be incremented to 0 on the next line
    }
    turnOnNumberForDigit(++counterD2, D2);
    digitalWrite(D2,HIGH);
    delay(5); 

  }
  if(didPressButtonDown(S3, buttonState3)){
    if(counterD3 == 9){
      counterD3 = -1;// it will be incremented to 0 on the next line
    }
    turnOnNumberForDigit(++counterD3, D3);
    digitalWrite(D3,HIGH);
    delay(5); 
  }
  if(didPressButtonDown(S4, buttonState4)){
    if(counterD4 == 9){
      counterD4 = -1;// it will be incremented to 0 on the next line
    }
    turnOnNumberForDigit(++counterD4, D4);
    digitalWrite(D4,HIGH);
    delay(5);    
  }

  delay(175);
}

Please bare with me as this is only the second program I've ever written and I'm still quite the newb. Just learning how to use VOID to make a function was a big deal for me. lol

But here is the section of code that I'm struggling with. Someone else wrote this for me to replace what I had before and this doesn't work either.
Code:
void (*DigitFunctions[10])( int );

void turnOffAllExcept(int targetDigit)
{
  byte digitPin[] = {
    13, 12, 7, 6            };

  byte selectedDigit = targetDigit; // any of 0 .. 3

  for (byte n = 0; n < 4; n++) {

    digitalWrite(digitPin[n], LOW); // all off
  }
  digitalWrite(digitPin[selectedDigit], HIGH);
}

Here is the idea behind what I need to do.I think it's called multiplexing. But it needs to keep a memory of where each digit is set at. Let's say it's displaying [4 3 2 1]. When I press button 1, the digit one(meaning only number displayed in the far left space) will become a 5 and now [5 3 2 1] will be showing. So it needs to remember the number for each of the digits and now to display them all at once it needs to do the following:
  1. activate the code to display the function that corresponds to the number 5
  2. turn on digit number 1
  3. turn off digit number 1
  4. activate the code to display the function that corresponds to the number 3
  5. turn on digit number 2
  6. turn off digit number 2
  7. etc... turn on a function, then 3 off 3 on 4 off 4, you get the idea
There will be some timing delays added of course. But that's it. It seems like a simple task, but it never works.
 

just my 2 cents worth

a 'digit' maps to certain segments, so I would keep a 2 dimensional array; digit and segments

when the switch is pressed, turn off all segments - I would not fiddle about turning off selected segments, then turn on the segments according to the next digit
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top