Simple Seven Segment display counter

I'm writing a simple code to display 0-9 on a seven segment display, with a one second delay between each number. I have a long version of the code written, but now my task is to write this without global variables and magic numbers. I know I can use #define to assign a pin to each segment letter (A, B, C....), but I'm not sure how to implement this without global variables. Can someone help me to understand this better?

The inital program is below. Note that segment is pin 2, and so on:

//This program counts from 0-9 with a one second delay between each number

int a = 2;
int b = 3;
int c = 4;
int d = 5;
int e = 6;
int f = 7;
int g = 8;

void setup() {
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
pinMode(f, OUTPUT);
pinMode(g, OUTPUT);
}

void loop() {
digitalWrite(a, LOW); // display zero
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
delay(1000); // wait for a second

digitalWrite(a, HIGH); // display one
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
delay(1000); // wait for a second

digitalWrite(a, LOW); // display TWO
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, LOW);
delay(1000); // wait for a second

digitalWrite(a, LOW); // display THREE
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, LOW);
delay(1000); // wait for a second

digitalWrite(a, HIGH); // display FOUR
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
delay(1000); // wait for a second

digitalWrite(a, LOW); // display FIVE
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
delay(1000); // wait for a second

digitalWrite(a, LOW); // display SIX
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
delay(1000); // wait for a second

digitalWrite(a, LOW); // display SEVEN
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
delay(1000); // wait for a second

digitalWrite(a, LOW); // display EIGHT
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
delay(1000); // wait for a second

digitalWrite(a, LOW); // display NINE
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
delay(1000); // wait for a second
}

?

So you want to do this?
#define a 2
.
.
.
#define g 8

If you do it like this, you may want to use better names, like Seg_A, Seg_B . . . Seg_G.

Then once you have that done, make a byte array to hold all the bytes for each number (and letters).

void ShowNumber(byte Num)
{
//Do work here with a for loop and bitRead()
}

ShowNumber(5) → should show the number 5

This is the sort of thing that you are looking for

``````#define segA 2
#define segB 3
#define segC 4
#define segD 5
#define segE 6
#define segF 7
#define segG 8

void setup()
{
pinMode(segA, OUTPUT);
pinMode(segB, OUTPUT);
pinMode(segC, OUTPUT);
pinMode(segD, OUTPUT);
pinMode(segE, OUTPUT);
pinMode(segF, OUTPUT);
pinMode(segF, OUTPUT);
}
``````

Anywhere in the code that you want to use pin 2, for instance, use segA in the code.

The program can also be vastly improved and shortened by creating a function to turn on the segments and an array to hold the list of segments to the turned on or off for each number, but that can come later.

Is #define considered a global variable then? I'm not familiar with byte arrays yet, I'm still in a class learning.

#define is used for substitution, so yes it seen throughout the entire code, but no is it not considered an actual global variable.

Example:

``````//default (both commented out) 188 global bytes used

//byte test = 1; // 190 global bytes used
#define test 1 // 188 global bytes used

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.print(test + test);
}

void loop() {
// put your main code here, to run repeatedly:

}
``````

Is #define considered a global variable then?

No. #define simply causes the defined text to be replaced in the code before it is compiled. It can be a handy way to make code more readable by allowing automatic substitution of names by pin numbers, for example. In which case changing the define at the top of a program would change the pin number used throughout the program.