Executing multiple commands with 1 command

I just got my arduino uno starter pack today and I'm playing around with the 4 digit 7 segment display. I want to make a counter that would add numbers when a button is pressed. It starts with 0, when button is pressed it goes to 2, pressed again to 3...

I don't want any help with the button code because I want to figure it out on my own :slight_smile:

I would need some help with number displaying code. In order to display a number on 1 segment I have to write down 8 lines of code and I dont like doing that for 10000 times. Is there any way to group those 8 lines into a single command made by user? Just like you define a variable or constante I would like to define a command that executes group of commands.

Here is a code for one number only to keep it simple.
Oh, and I don't want to use any libraries. This way I'm forced to learn how to manage my sketches :slight_smile:

This is the link where I found the guide on which my sketch is based on:


/*
A

F | | B
| G | |------------|
--- | 8. 8. 8. 8. |
E | | C |-------------|

D

This is how display looks. On the right side you can see general placement.
First "8." from left to righ is segment 1, second one segment 2...
On the left you can see how each light si named in this sketch
Number of my display is 5461BS*/

int pinDOT = 1; //sets light decimal dot to digital pin 1
int pinA = 2; //sets light A to digital pin 2
int pinB = 3;
int pinC = 4;
int pinD = 5;
int pinE = 6;
int pinF = 7;
int pinG = 8;
int pinC1 = 9; //segment 1 is set to pin 9
int pinC2 = 10; //if i want to print a number on segment one I have to turn segment 1 on
int pinC3 = 11; //and segments 2, 3 and 4 off
int pinC4 = 12;

void setup() {
pinMode (pinA, OUTPUT); //sets all display pins as outputs
pinMode (pinB, OUTPUT);
pinMode (pinC, OUTPUT);
pinMode (pinD, OUTPUT);
pinMode (pinE, OUTPUT);
pinMode (pinF, OUTPUT);
pinMode (pinG, OUTPUT);
pinMode (pinC1, OUTPUT);
pinMode (pinC2, OUTPUT);
pinMode (pinC3, OUTPUT);
pinMode (pinC4, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(pinC1, HIGH); //tells arduino to use first segment of the display
digitalWrite(pinC2, LOW);
digitalWrite(pinC3, LOW);
digitalWrite(pinC4, LOW);

digitalWrite(pinA, LOW); //tells arduino to write a S (looks same as 5) and leaves the dot turned off
digitalWrite(pinB, HIGH); //instead of 8 lines i would like to use one. Something like: "print 5"
digitalWrite(pinC, LOW); //when "print 5" is called all 8 digitalWrite commands are executed
digitalWrite(pinD, LOW);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
digitalWrite(pinDOT, HIGH);
}

7ssegment_4digit.ino (1.82 KB)

It's called a "function".

Here's what the function definition would look like:

void displayS() {
  digitalWrite(pinA, LOW);   //tells arduino to write a S (looks same as 5) and leaves the dot turned off
  digitalWrite(pinB, HIGH);   //instead of 8 lines i would like to use one. Something like: "print 5"
  digitalWrite(pinC, LOW);   //when "print 5" is called all 8 digitalWrite commands are executed
  digitalWrite(pinD, LOW);
  digitalWrite(pinE, HIGH);
  digitalWrite(pinF, LOW);
  digitalWrite(pinG, LOW);
  digitalWrite(pinDOT, HIGH);
}

I chose to call the function "displayS", but you can name it anything you like.
Then wherever you want to run that code, you call the function like this:

displayS();

More information on functions:
http://www.cplusplus.com/doc/tutorial/functions/

I almost go it then :slight_smile: I wasn't sure if void is only used for setup and loop ant how to recall it though.

Thanks a lot!

You're welcome.

domenguglo:
I wasn't sure if void is only used for setup and loop ant how to recall it though.

void is the return type of the function. It means the function doesn't return anything. Other times, it might be useful to have the function return something, and then you would use a different return type. Take this silly function for example:

bool pinIsHIGH(const byte pin) {
  if(digitalRead(pin) == HIGH) {
    return true;
  else {
    return false;
  }
}

Then you can do things like this in your code:

if(pinIsHIGH(3)) {

Another way to make the program more compact is to use the 8 bits of a byte to each store the state of one segment. Then the same function can display any pattern.

const byte DisplayS = B01001001;

void display(const byte V) {
 digitalWrite(pinA, V & B10000000);
 digitalWrite(pinB, V & B01000000);
 digitalWrite(pinC, V & B00100000;
 digitalWrite(pinD, V & B00010000);
 digitalWrite(pinE, V & B00001000);
 digitalWrite(pinF, V & B00000100);
 digitalWrite(pinG, V & B00000010);
 digitalWrite(pinDOT, V & B00000001);
}

 display(DisplayS);