Go Down

Topic: Double Digit 7 Segment Display (Read 376 times) previous topic - next topic

NathanWangNY

What I'm trying to do: using a double digit 7-segment display, count from 0 to 99 indefinitely with a 200 ms pause between numbers (without using delya(200)).

Method descriptions


chooseNum(): accepts an integer between 0 and 9 inclusive which will be displayed
dispNum(): must take 7 parameters. Any parameters given to dispNum() will be lighted. If there are extra spaces, a 0 will be ignored. For example, dispNum(a, b, 0, 0, 0, 0, 0); will turn segments a and b on.
switchState(): takes in HIGH or LOW. If HIGH, digit 1 is written to HIGH, and digit 2 is written to LOW (and vice versa).

So far, I can make the ones digit count from 0 to 9 repeatedly. When I try the following code both digits of the 7-segment display show the same thing counting from 0 to 9. This is very similar to the 7-segment display I am using: https://www.rastating.com/content/images/2014/Jan/WP_20130519_004_1_.jpg


Basically, how do I use digitalWrite() on dig1 and dig2 to make the display count from 0 to 99. I already know how to make the program display 0 to 9 on a loop.

const int a = 7, b = 8, c = 3, d = 2, e = 12, f = 10, g = 4, dig2 = 9, dig1 = 6; //segments with corresponding pin numbers
int digitsArray[9] = {a, b, c, d, e, f, g, dig2, dig1}; //for reset and output
int current, previous = 0, num = 0; //without delay
const int INTERVAL = 200; //millis delay time
int state = LOW, num2 = 1;

void setup() {
  Serial.begin(9600); //for debug

  for (int i = 0; i < 9; i++) {
    pinMode(digitsArray, OUTPUT); //all pins are outputs
  }

  reset();
  switchState(state);
}

void loop() {
  current = millis(); //time since program started

  if (current - previous > INTERVAL) {
    reset();
   
    if(num == 10)
    { 
      num = 0;
      switchState(LOW);
      reset();
      chooseNum(num2);
      num2 += 1;
     
      if(num2 == 10)
        num2 = 1;
    }

    switchState(HIGH);

    chooseNum(num);
    num += 1;

    previous = millis();
  }
 
  switchState(LOW);
  switchState(HIGH);
}

void dispNum(int num1, int num2, int num3, int num4, int num5, int num6, int num7) {
  int inputArray[7] = {num1, num2, num3, num4, num5, num6, num7}; //all parameters become an array

  for (int k = 0; k < 7; k++) {
    if (inputArray[k] != 0) //0 is a placeholder - nothing happens
    {
      digitalWrite(inputArray[k], LOW);
      Serial.println(inputArray[k]);
    }
  }
}

void reset() { //turns all segments off
  for (int n = 0; n < 8; n++) {
    digitalWrite(digitsArray[n], HIGH);
  }
}

void chooseNum(int num) {
  switch (num) { //case number corresponds to digit displayed
    case 0:
      dispNum(a, f, e, d, c, b, 0);
      break;
    case 1:
      dispNum(b, c, 0, 0, 0, 0, 0);
      break;
    case 2:
      dispNum(a, b, g, e, d, 0, 0);
      break;
    case 3:
      dispNum(a, b, g, c, d, 0, 0);
      break;
    case 4:
      dispNum(f, g, b, c, 0, 0, 0);
      break;
    case 5:
      dispNum(a, f, g, c, d, 0, 0);
      break;
    case 6:
      dispNum(a, f, e, d, c, g, 0);
      break;
    case 7:
      dispNum(a, b, c, 0, 0, 0, 0);
      break;
    case 8:
      dispNum(f, a, b, g, e, c, d);
      break;
    case 9:
      dispNum(f, a, b, g, c, d, 0);
      break;
  }
}

void switchState(int state) { //input state dig1 should be set to
  digitalWrite(dig1, state);
  digitalWrite(dig2, !state);
}


PaulRB

See this line from your code

    pinMode(digitsArray, OUTPUT); //all pins are outputs

Compare it back to your version. Something is missing. Notice how from that point, the rest of your code is in italics? There's because you didn't read the sticky post. Please read & fix.

Go Up