int pins1[8] = {2,3,4,5,6,7,8}; // array of led pins for digit #1
int pins2[8] = {9,10,11,12,13,A0,A1,A5}; // array of led pins for digit #2
// Below is a 2D array of appropriate LEDs for each digit.
// You should set these to correspond to each segment of the display
// In this example the digits are set like this (where "#" is a segment):
// 2
// #
// 7# #3
// #<------8
// 6# #4
// #
// 5
// THE CORRESPONDING PINS MIGHT BE DIFFERENT IN YOUR CASE, either rewire them to match
// this or change the code to match your layout.
//
// In Arduino language 1 == HIGH, 0 == LOW
//
// Corresponds to: 2,3,4,5,6,7,8 for pins1 (9,10,11,12,13,A0,A1,A5 for pins2)
byte digits[10][7] = { { 1,1,1,1,1,1,0 }, // = 0
{ 0,1,1,0,0,0,0 }, // = 1
{ 1,1,0,1,1,0,1 }, // = 2
{ 1,1,1,1,0,0,1 }, // = 3
{ 0,1,1,0,0,1,1 }, // = 4
{ 1,0,1,1,0,1,1 }, // = 5
{ 1,0,1,1,1,1,1 }, // = 6
{ 1,1,1,0,0,0,0 }, // = 7
{ 1,1,1,1,1,1,1 }, // = 8
{ 1,1,1,0,0,1,1 } // = 9
};
void setup() {
//OTHER CODE HERE
// Set LED pins as output
for (int i=0; i<7; i++){
pinMode(pins1[i], OUTPUT);
pinMode(pins2[i], OUTPUT);
}
}
void loop() {
//OTHER CODE HERE
displayDigit(23); // as an example call display function to show "23"
}
displayDigit(int num){
int dig1 = num / 10; // Figure out 1st digit
// i.e.: 23 / 10 = 2.3 = int 2
3 / 10 = 0.3 = int 0
int dig2 = num % 10; // Figure out 2nd digit. "%" is called "mod", returns the
// remainder of a division operation
// i.e.: 23 % 10 = 3
// 3 % 10 = 3
// Increment through both arrays of LEDs and set the appropriate segments
// 1(HIGH) or 0(LOW)
for(int i=0; i<7; i++) {
digitalWrite(pins1[i], digits[dig1][i]);
digitalWrite(pins2[i], digits[dig2][i]);
}
}
// This way there is no need for ledLOW() function, no need for all the individual function to // display each digit, consequently there is less things that can go wrong.