1
//Numbers 0-9.
int numberArray[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, 0, 1, 0, 0 }, //9
};

//Turn off all segments (common anode --> high = off)
void reset() {
  for (int i = 12; i >= 6; i--) {
    digitalWrite(i, 1);
  }
}

//turn on both segments (don't write anything to them)
void on(){
  digitalWrite(4,1);
  digitalWrite(5,1);
}

void setup() {
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
  reset();
  on();
}
void loop() {
  for (int i=0;i<99;i++){
    twoDigitNumber(i);
  }
}

void twoDigitNumber(int number){
    digitalWrite(4,1);                   //turn on left display
    digitalWrite(5,0);                   //turn on right display
    digit(int(floor(number/10)));        //display the tens digit
    delay(10);
    digitalWrite(4,0);                   //turn off left display
    digitalWrite(5,1);                   //turn on right display
    digit(int(number-floor(number/10))); //display the ones digit
    delay(10);
}

//make the displays show a single digit number
void digit(int number) {
  for (int i = 12; i >= 6; i--) {
    digitalWrite(i, numberArray[number][12 - i]);
  }
}

This is my first time doing anything at all with Arduino, and I'm also brand new to the language.

I have a bit of circuitry attached to the display. I'm using 2n5551 NPN transistors to act as a switch. Collectors connected to 5v, bases are connected to pin 4/5 with a 200 ohm resistor. Emitters are connected to the common anode of the displays. pin 4 is for the left display, pin 5 for the right display. These are to display different numbers in each display. Something's probably wrong with this but I'm having a hard time learning about transistors.

When I use the function twoDigitNumber(), it only displays the number on the left display, but I can see a very dim copy of that same number on the right display. int() is there because i thought floor() would make it into a double and cause issues, but that didn't fix it.

I know every segment works. When I use digit(), it shows the same digit on both displays (as expected).

Any help is appreciated.

  • This line looks wrong: digit(int(number-floor(number/10))); - don't you want the number mod 10? That is: digit (number % 10); – Nick Gammon Jan 7 '18 at 4:50
  • If the number is 42 your calculation would give you 42 - 4 which would be 38 and that is not in the range 0 to 9. – Nick Gammon Jan 7 '18 at 4:52
  • Looks like I forgot to multiply the tens by 10 first before subtracting it. – Cats are Liquid Jan 7 '18 at 15:17
1

Missing pinmode(4,OUTPUT) in your setup routine.

As a general comment, using variable names instead of 'magic numbers' makes your code easier to read - and thus usually easier to debug. for example:

const int leftDisplay = 4;
const int rightDisplay = 5;
const int seg1 = 12;
const int seg2 = 11;
[...]

You can also use HIGH and LOW instead of 1 and 0 for the digitalwrite elements

  • That worked. Thanks. I'm ashamed the solution was so easy. – Cats are Liquid Jan 7 '18 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.