//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.
digit(int(number-floor(number/10)));
- don't you want the number mod 10? That is:digit (number % 10);
42 - 4
which would be 38 and that is not in the range 0 to 9.