countdown timer

My knowledge of arduino is pretty basic and I am doing a few projects to try get me up to speed. Right now I am playing with seven segments and making a countdown timer. I did this instructable http://www.instructables.com/id/Arduino-7-segment-countdown-timer/ which was great for counting down from 20 seconds but the code is really long. Any suggestions on how I would shorten the code so I could countdown from say a minute?

any help would be appreciated :)

cheers,

ck

The code should not get any longer because you want to count down for a longer period of time.

Post the code you are using if you want help with it.

int pin1 = 2;
int pin2 = 3; // --6–
int pin3 = 4; // 5 | | 7
int pin4 = 5; // |–4–|
int pin5 = 6; // 1 | | 3
int pin6 = 7; // --2–
int pin7 = 8;
int gnd1 = 11; // gnd1 is display 1’s gnd
int gnd2 = 9; // gnd2 is display 2’s gnd
int timer = 500; // A timer, to run the for loop 500 times, which turns out as 1 second.
int value; // The value, part of the FADING display

void setup(){
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT); //The following sets up all of the pins for use.
pinMode(pin5, OUTPUT);
pinMode(pin6, OUTPUT);
pinMode(pin7, OUTPUT);
pinMode(gnd1, OUTPUT);
pinMode(gnd2, OUTPUT);

for (int i=0; i<timer; i++){ // The for loop, for running the program 500 times.
digitalWrite(pin1, B1);
digitalWrite(pin2, B1);
digitalWrite(pin3, B0);
digitalWrite(pin4, B1);
digitalWrite(pin5, B0);
digitalWrite(pin6, B1);
digitalWrite(pin7, B1);
digitalWrite(gnd1, B0);
digitalWrite(gnd2, B1);
delay(0.5); // Writes 20 to the display.
digitalWrite(pin1, B1);
digitalWrite(pin2, B1);
digitalWrite(pin3, B1);
digitalWrite(pin4, B0);
digitalWrite(pin5, B1);
digitalWrite(pin6, B1);
digitalWrite(pin7, B1);
digitalWrite(gnd1, B1);
digitalWrite(gnd2, B0);
delay(0.5);

}

for (int i=0; i<timer; i++){
digitalWrite(pin1, B0);
digitalWrite(pin2, B0);
digitalWrite(pin3, B1);
digitalWrite(pin4, B0);
digitalWrite(pin5, B0);
digitalWrite(pin6, B0);
digitalWrite(pin7, B1);
digitalWrite(gnd1, B0);
digitalWrite(gnd2, B1);
delay(0.5); // Writes 19 to the display.
digitalWrite(pin1, B0);
digitalWrite(pin2, B0);
digitalWrite(pin3, B1);
digitalWrite(pin4, B1);
digitalWrite(pin5, B1);
digitalWrite(pin6, B1);
digitalWrite(pin7, B1);
digitalWrite(gnd1, B1);
digitalWrite(gnd2, B0);
delay(0.5);

}
for (int i=0; i<timer; i++){
digitalWrite(pin1, B0);
digitalWrite(pin2, B0);
digitalWrite(pin3, B1);
digitalWrite(pin4, B0);
digitalWrite(pin5, B0);
digitalWrite(pin6, B0);
digitalWrite(pin7, B1);
digitalWrite(gnd1, B0);
digitalWrite(gnd2, B1);
delay(0.5); // Writes 18 to the display.
digitalWrite(pin1, B1);
digitalWrite(pin2, B1);
digitalWrite(pin3, B1);
digitalWrite(pin4, B1);
digitalWrite(pin5, B1);
digitalWrite(pin6, B1);
digitalWrite(pin7, B1);
digitalWrite(gnd1, B1);
digitalWrite(gnd2, B0);
delay(0.5);

}
for (int i=0; i<timer; i++){
digitalWrite(pin1, B0);
digitalWrite(pin2, B0);
digitalWrite(pin3, B1);
digitalWrite(pin4, B0);
digitalWrite(pin5, B0);
digitalWrite(pin6, B0);
digitalWrite(pin7, B1);
digitalWrite(gnd1, B0);
digitalWrite(gnd2, B1);
delay(0.5); // Writes 17 to the display.
digitalWrite(pin1, B0);
digitalWrite(pin2, B0);
digitalWrite(pin3, B1);
digitalWrite(pin4, B0);
digitalWrite(pin5, B0);
digitalWrite(pin6, B1);
digitalWrite(pin7, B1);
digitalWrite(gnd1, B1);
digitalWrite(gnd2, B0);
delay(0.5);

}
for (int i=0; i<timer; i++){
digitalWrite(pin1, B0);
digitalWrite(pin2, B0);
digitalWrite(pin3, B1);
digitalWrite(pin4, B0);
digitalWrite(pin5, B0);
digitalWrite(pin6, B0);
digitalWrite(pin7, B1);
digitalWrite(gnd1, B0);
digitalWrite(gnd2, B1);
delay(0.5); // Writes 16 to the display.
digitalWrite(pin1, B1);
digitalWrite(pin2, B1);
digitalWrite(pin3, B1);
digitalWrite(pin4, B1);
digitalWrite(pin5, B1);
digitalWrite(pin6, B1);
digitalWrite(pin7, B0);
digitalWrite(gnd1, B1);
digitalWrite(gnd2, B0);
delay(0.5);

}
for (int i=0; i<timer; i++){
digitalWrite(pin1, B0);
digitalWrite(pin2, B0);
digitalWrite(pin3, B1);
digitalWrite(pin4, B0);
digitalWrite(pin5, B0);
digitalWrite(pin6, B0);
digitalWrite(pin7, B1);
digitalWrite(gnd1, B0);
digitalWrite(gnd2, B1);
delay(0.5); // Writes 15 to the display.
digitalWrite(pin1, B0);
digitalWrite(pin2, B1);
digitalWrite(pin3, B1);
digitalWrite(pin4, B1);
digitalWrite(pin5, B1);
digitalWrite(pin6, B1);
digitalWrite(pin7, B0);
digitalWrite(gnd1, B1);
digitalWrite(gnd2, B0);
delay(0.5);

}
for (int i=0; i<timer; i++){
digitalWrite(pin1, B0);
digitalWrite(pin2, B0);
digitalWrite(pin3, B1);
digitalWrite(pin4, B0);
digitalWrite(pin5, B0);
digitalWrite(pin6, B0);
digitalWrite(pin7, B1);
digitalWrite(gnd1, B0);
digitalWrite(gnd2, B1);
delay(0.5); // Writes 14 to the display.
digitalWrite(pin1, B0);
digitalWrite(pin2, B0);
digitalWrite(pin3, B1);
digitalWrite(pin4, B1);
digitalWrite(pin5, B1);
digitalWrite(pin6, B0);
digitalWrite(pin7, B1);
digitalWrite(gnd1, B1);
digitalWrite(gnd2, B0);
delay(0.5);

}
for (int i=0; i<timer; i++){
digitalWrite(pin1, B0);
digitalWrite(pin2, B0);
digitalWrite(pin3, B1);
digitalWrite(pin4, B0);
digitalWrite(pin5, B0);
digitalWrite(pin6, B0);
digitalWrite(pin7, B1);
digitalWrite(gnd1, B0);
digitalWrite(gnd2, B1);
delay(0.5); // Writes 13 to the display.
digitalWrite(pin1, B0);
digitalWrite(pin2, B1);
digitalWrite(pin3, B1);
digitalWrite(pin4, B1);
digitalWrite(pin5, B0);
digitalWrite(pin6, B1);
digitalWrite(pin7, B1);
digitalWrite(gnd1, B1);
digitalWrite(gnd2, B0);
delay(0.5);

}
for (int i=0; i<timer; i++){
digitalWrite(pin1, B0);
digitalWrite(pin2, B0);
digitalWrite(pin3, B1);
digitalWrite(pin4, B0);
digitalWrite(pin5, B0);
digitalWrite(pin6, B0);
digitalWrite(pin7, B1);
digitalWrite(gnd1, B0);
digitalWrite(gnd2, B1);
delay(0.5); // Writes 12 to the display.
digitalWrite(pin1, B1);
digitalWrite(pin2, B1);
digitalWrite(pin3, B0);
digitalWrite(pin4, B1);
digitalWrite(pin5, B0);
digitalWrite(pin6, B1);
digitalWrite(pin7, B1);
digitalWrite(gnd1, B1);
digitalWrite(gnd2, B0);
delay(0.5);

}
for (int i=0; i<timer; i++){
digitalWrite(pin1, B0);
digitalWrite(pin2, B0);
digitalWrite(pin3, B1);
digitalWrite(pin4, B0);
digitalWrite(pin5, B0);
digitalWrite(pin6, B0);
digitalWrite(pin7, B1);
digitalWrite(gnd1, B0);
digitalWrite(gnd2, B1);
delay(0.5); // Writes 11 to the display.
digitalWrite(pin1, B0);
digitalWrite(pin2, B0);
digitalWrite(pin3, B1);
digitalWrite(pin4, B0);
digitalWrite(pin5, B0);
digitalWrite(pin6, B0);
digitalWrite(pin7, B1);
digitalWrite(gnd1, B1);
digitalWrite(gnd2, B0);
delay(0.5);

}
for (int i=0; i<timer; i++){
digitalWrite(pin1, B0);
digitalWrite(pin2, B0);
digitalWrite(pin3, B1);
digitalWrite(pin4, B0);
digitalWrite(pin5, B0);
digitalWrite(pin6, B0);
digitalWrite(pin7, B1);
digitalWrite(gnd1, B0);
digitalWrite(gnd2, B1);
delay(0.5); // Writes 10 to the display.
digitalWrite(pin1, B1);
digitalWrite(pin2, B1);
digitalWrite(pin3, B1);
digitalWrite(pin4, B0);
digitalWrite(pin5, B1);
digitalWrite(pin6, B1);
digitalWrite(pin7, B1);
digitalWrite(gnd1, B1);
digitalWrite(gnd2, B0);
delay(0.5);

}
for (int i=0; i<timer; i++){
digitalWrite(pin1, B1);
digitalWrite(pin2, B1);
digitalWrite(pin3, B1);
digitalWrite(pin4, B0);
digitalWrite(pin5, B1);
digitalWrite(pin6, B1);
digitalWrite(pin7, B1);
digitalWrite(gnd1, B0);
digitalWrite(gnd2, B1);
delay(0.5); // Writes 09 to the display.
digitalWrite(pin1, B0);
digitalWrite(pin2, B0);
digitalWrite(pin3, B1);
digitalWrite(pin4, B1);
digitalWrite(pin5, B1);
digitalWrite(pin6, B1);
digitalWrite(pin7, B1);
digitalWrite(gnd1, B1);
digitalWrite(gnd2, B0);
delay(0.5);

}

for (int i=0; i<timer; i++){
digitalWrite(pin1, B1);
digitalWrite(pin2, B1);
digitalWrite(pin3, B1);
digitalWrite(pin4, B0);
digitalWrite(pin5, B1);
digitalWrite(pin6, B1);
digitalWrite(pin7, B1);
digitalWrite(gnd1, B0);
digitalWrite(gnd2, B1);
delay(0.5); // Writes 08 to the display.
digitalWrite(pin1, B1);
digitalWrite(pin2, B1);
digitalWrite(pin3, B1);
digitalWrite(pin4, B1);
digitalWrite(pin5, B1);
digitalWrite(pin6, B1);
digitalWrite(pin7, B1);
digitalWrite(gnd1, B1);
digitalWrite(gnd2, B0);
delay(0.5);

}

}
  for (int i=0; i<timer; i++){
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(0.5);                                        //          Writes 07 to the display.
   digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(0.5);
   
  }
  for (int i=0; i<timer; i++){
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(0.5);                                        //          Writes 06 to the display.
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(0.5);
   
  }
  for (int i=0; i<timer; i++){
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(0.5);                                        //          Writes 05 to the display.
   digitalWrite(pin1, B0);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(0.5);
   
  }
  for (int i=0; i<timer; i++){
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(0.5);                                        //          Writes 04 to the display.
   digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B1);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(0.5);
   
  }
  for (int i=0; i<timer; i++){
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(0.5);                                        //          Writes 03 to the display.
   digitalWrite(pin1, B0);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(0.5);
   
  }
  for (int i=0; i<timer; i++){
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(0.5);                                        //          Writes 02 to the display.
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(0.5);
   
  }
  for (int i=0; i<timer; i++){
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(0.5);                                        //          Writes 01 to the display.
   digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B1);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(0.5);
   
  }
  for (int i=0; i<timer; i++){
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(0.5);                                        //          Writes 00 to the display.
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(0.5);
   
  }
  
  
}

void loop() {
  
  
  digitalWrite(13, HIGH);   //  Turns the TIMER ENDED LED on.
  
   for(value = 0 ; value <= 255; value+=5) // fade in (from min to max) 
  { 
    analogWrite(gnd1, value);           // sets the value (range from 0 to 255) 
    analogWrite(gnd2, value);           // sets the value (range from 0 to 255) 
    delay(10);                            // waits for 30 milli seconds to see the dimming effect 
  } 
  for(value = 255; value >=0; value-=5)   // fade out (from max to min) 
  { 
    analogWrite(gnd1, value);           // sets the value (range from 0 to 255) 
    analogWrite(gnd2, value);           // sets the value (range from 0 to 255) 
    delay(10); 
  }  
  
}

sorry forgot to click the code button the first time. the code is so big it would not fit in to one post but thats pretty much it

If you look at the code, there is a bunch of code to show a 0, a bunch of code to show a 1, a bunch of code to show a 2, etc.

Put the code to show a 0 in a function, show0. Put the code to show a 1 in a function. Repeat for each of the other numbers. Make each function take 1 argument to define whether the digit is the left digit or the right digit.

Then, to show "03", all you'd need to do is:

show0(left);
delay(0.5);
show3(right);

Much shorter, eh?

By the way, the delay function takes an integer argument - it is the number of milliseconds to wait. Since 0.5 is truncated to 0, the delay call is doing nothing.

Once you have this done, you can use the switch statement to display a digit on the left or right:

switch(digit)
{
    case 0:
        show0(left); // or right
        break;
    case 1:
        show1(left);
        break;
    // repeat for 2 through 9
}

Then, you could put that code in a function, shwoDigit, that takes two arguments - the digit and the side.

Then, you could write a loop:

for(int v=upperLimit, v>=lowerLimit; v--)
{
    int rightDigit = v % 10; // get the right digit
    int leftDigit = v - rightDigit; // get the left digit

    showDigit(leftDigit, left);
    showDigit(rightDigit, right);
}

Now the code to count down from 20 to 0 requires setting upperLimit to 20, and lowerLimit to 0.

The code to count down from 60 to 0 requires setting upperLimit to 60, and lowerLimit to 0.

See, exactly the same amount of code.

Any time something has to be written out again and again, e.g. the way to make one of the digits say "5", you should suspect that There Must Be A Better Way.

It will be better not just because the code will be shorter, and because you will be able to do more things, more easily, but also becuase it gives bugs less places to hide.

In the "show 5" example, if you have just a simple subroutine to do it, instead of writing out all the code over and over, you can't have a typo in one of the "over and over write-outs" messing hings up, in an obscure way.

Yea thats what this is all about making it easier for me to understand and I was getting so many errors the bigger it got.

I kind of understanding your code. If I create functions for 1 through to 9 do I have to do a separate one for each display, as i have three? Then in my code just call up the number required for each display by its function?

The code to light up the proper segments to display a 3 in each position varies only in which ground pins are set HIGH or LOW. So, no, you don't need a separate function to display a 3 in each position. The function to show a 3 should be able, based on a supplied argument, and some if statements (or a switch statement) to display a 3 in any position.

If it makes it easier for you, though, at this stage to have show3Left, show3Center, and show3Right, then do that. You'll quickly see the similarities and replace them with a single function.

But start with some simplification of the code (called factoring). The more you practice, the easier it gets.

int pin1 = 2;
int pin2 = 3;                                    //                            --6--
int pin3 = 4;                                    //                         5 |     | 7
int pin4 = 5;                                    //                           |--4--|
int pin5 = 6;                                    //                         1 |     | 3
int pin6 = 7;                                    //                            --2--
int pin7 = 8;
int gnd1 = 11;                                 //                          gnd1 is display 1's gnd
int gnd2 = 9;                                 //                          gnd2 is display 2's gnd
int gnd3 = 13;
int timer = 500;                               //   A timer, to run the for loop 500 times, which turns out as 1 second.
int value;                                        //   The value, part of the FADING display


int show0(){
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B0);              // writes 0
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
}
  
int show1(){   
   digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B0);              // writes 1
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B1);
}
int show2(){   
    digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B0);          // writes 2
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
}
int show3(){  
   digitalWrite(pin1, B0);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B0);        // writes 3
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
 }
int show4(){ 
   digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B1);        // writes 4
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B1);
 }
int show5(){   
   digitalWrite(pin1, B0);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B1);        // writes 5
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B0);
 }
int show6(){   
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B1);        //writes 6
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B0);
 }
int show7(){   
   digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B0);        // writes 7
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
 }
int show8(){   
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B1);        // writes 8
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
 }
int show9(){   
   digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B1);        // writes 9
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
}

void setup(){
  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
  pinMode(pin4, OUTPUT);           //The following sets up all of the pins for use.
  pinMode(pin5, OUTPUT);
  pinMode(pin6, OUTPUT);
  pinMode(pin7, OUTPUT);
  pinMode(gnd1, OUTPUT);
  pinMode(gnd2, OUTPUT);
  pinMode(gnd3, OUTPUT);

That is as far as I have so far managed to go to for trying to start this countdown. I have managed to have three seven segments display the numbers 012 so I understand the grounding of them all and getting them to display. What I am struggling with is the functions and from reading all I can find I do not think I am getting it quite right.

Have you seen this reference page which trys to explain about functions: http://www.arduino.cc/en/Reference/FunctionDeclaration

Andrew

So is what is written above correct for functions?

The code you posted first had calls to set the ground pins, too. These functions do not have those calls...

There is no reason for show0, show1, etc. to be declared int functions, when they do not return values. They should be declared void functions.

Other than that, you (seem to) have a good start. Not all of your code showed up, so I can't tell how you are calling these functions.

so if I were to change each int show0 etc to void function0 it should work and as for calling that code I did not get that far as I do not understand how to call them and put the correct ground pins on to activate the right display

sorry about this struggling with this one.

 #include "LedControl.h"     // Include the LedControl library
LedControl lc=LedControl(12,11,10,1); // Set up an LED object on pins 12,11,10

int seconds_ones;
int minutes_ones;
int seconds_tens;        // Initialise values for tens and ones units of m,s
int minutes_tens;        // these are the values actually sent to the LEDs
volatile unsigned char tick;
unsigned char seconds = 0;
unsigned char minutes = 60;     // Inititialise actual values for mm,ss

ISR (TIMER2_OVF_vect) {        // When timer2 overflows...
  tick++;                      // increment tick


}


void display_time () {            // Function to display the time

    seconds_ones = seconds % 10;        // Get the 1's digit of seconds
    if (seconds>=10){             // If seconds greater than 10
     seconds_tens = seconds / 10;}      // Get 10's digit of seconds
    else {
   seconds_tens = 0;}          // Otherwise 10s is 0

    minutes_ones = minutes % 10;        // Repeat for minutes
    if (minutes>=10){
     minutes_tens = minutes / 10 ;}
    else {
   minutes_tens = 0;}


    lc.setDigit(0,0,(byte)seconds_ones,false);  // Send digits to LEDs
    lc.setDigit(0,1,(byte)seconds_tens,false);
    lc.setDigit(0,2,(byte)minutes_ones,false);
    lc.setDigit(0,3,(byte)minutes_tens,false);
}







void loop () {

if (tick) {                             // If a tick has occured
  seconds = seconds - 1;                // Increment the seconds
  tick = 0;                             // reset the tick flag
  if (seconds<0){                      // If a minute has passed
  seconds = 0;                          // Send seconds back to 0
  minutes = minutes - 1;                // Increment the minutes
    minutes = 0;                        // Send the minutes back to 0
    display_time ();             // send the time to the LEDs to be displayed
  }

I found this on another posting about countdown timers I think is this roughly what I am looking for?

Your first code contained:

  digitalWrite(pin1, B1);
  digitalWrite(pin2, B1);
  digitalWrite(pin3, B0);
  digitalWrite(pin4, B1);
  digitalWrite(pin5, B0);
  digitalWrite(pin6, B1);
  digitalWrite(pin7, B1);
  digitalWrite(gnd1, B0);                        
  digitalWrite(gnd2, B1);
  delay(0.5);                                        //          Writes 20 to the display.
  digitalWrite(pin1, B1);
  digitalWrite(pin2, B1);
  digitalWrite(pin3, B1);
  digitalWrite(pin4, B0);
  digitalWrite(pin5, B1);
  digitalWrite(pin6, B1);
  digitalWrite(pin7, B1);
  digitalWrite(gnd1, B1);
  digitalWrite(gnd2, B0);
  delay(0.5);

This displays a 2 on the left and a 0 on the right.

It also had this code:

   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(0.5);                                        //          Writes 02 to the display.
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(0.5);

This displays a 0 on the left and a 2 on the right.

The only difference between displaying a 2 on the left and a 2 on the right is the value written to the gnd1 and gnd2 pins. So, show2 should look like this:

void show2(int pos)
{
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);

   if(pos == 0)
   {
      digitalWrite(gnd1, B1);
      digitalWrite(gnd2, B0);
   }
   else
   {
      digitalWrite(gnd1, B0);
      digitalWrite(gnd2, B1);
   }
}

To show a 2 on the right:

show2(0);

To show a 2 on the left:

show2(1);

So far I have managed to get to this point

int pin1 = 2;
int pin2 = 3;
int pin3 = 4;
int pin4 = 5;
int pin5 = 6;
int pin6 = 7;
int pin7 = 8;
int gnd1 = 9;
int gnd2 = 11;
int gnd3 = 13;

void setup(){
  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
  pinMode(pin4, OUTPUT);           
  pinMode(pin5, OUTPUT);
  pinMode(pin6, OUTPUT);
  pinMode(pin7, OUTPUT);
  pinMode(gnd1, OUTPUT);
  pinMode(gnd2, OUTPUT);
  pinMode(gnd3, OUTPUT);

void show0(int pos){
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B0);             
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
   if(pos == 0)
   {
    digitalWrite(gnd1, B1);
    digitalWrite(gnd2, B0);
    digitalWrite(gnd3, B0);
   }
else if (pos == 0)
   {
    digitalWrite(gnd1, B0);
    digitalWrite(gnd2, B1);
    digitalWrite(gnd3, B0);
   }
else
   {
    digitalWrite(gnd1, B0);
    digitalWrite(gnd2, B0);
    digitalWrite(gnd3, B1);
   }
}
  
void show1(int pos){   
   digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B0);              
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B1);
if(pos == 0)
   {
    digitalWrite(gnd1, B1);
    digitalWrite(gnd2, B0);
    digitalWrite(gnd3, B0);
   }
else if (pos == 0)
   {
    digitalWrite(gnd1, B0);
    digitalWrite(gnd2, B1);
    digitalWrite(gnd3, B0);
   }
else
   {
    digitalWrite(gnd1, B0);
    digitalWrite(gnd2, B0);
    digitalWrite(gnd3, B1);
   }
} 
   

void show2(int pos){   
    digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B0);         
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
   if(pos == 0)
   {
    digitalWrite(gnd1, B1);
    digitalWrite(gnd2, B0);
    digitalWrite(gnd3, B0);
   }
else if (pos == 0)
   {
    digitalWrite(gnd1, B0);
    digitalWrite(gnd2, B1);
    digitalWrite(gnd3, B0);
   }
else
   {
    digitalWrite(gnd1, B0);
    digitalWrite(gnd2, B0);
    digitalWrite(gnd3, B1);
   }
}
void show3(int pos){  
   digitalWrite(pin1, B0);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B0);        
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
   if(pos == 0)
   {
    digitalWrite(gnd1, B1);
    digitalWrite(gnd2, B0);
    digitalWrite(gnd3, B0);
   }
else if (pos == 0)
   {
    digitalWrite(gnd1, B0);
    digitalWrite(gnd2, B1);
    digitalWrite(gnd3, B0);
   }
else
   {
    digitalWrite(gnd1, B0);
    digitalWrite(gnd2, B0);
    digitalWrite(gnd3, B1);
   }
 }
void show4(int pos){ 
   digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B1);        
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B1);
   if(pos == 0)
   {
    digitalWrite(gnd1, B1);
    digitalWrite(gnd2, B0);
    digitalWrite(gnd3, B0);
   }
else if (pos == 0)
   {
    digitalWrite(gnd1, B0);
    digitalWrite(gnd2, B1);
    digitalWrite(gnd3, B0);
   }
else
   {
    digitalWrite(gnd1, B0);
    digitalWrite(gnd2, B0);
    digitalWrite(gnd3, B1);
   }
 }
void show5(int pos){   
   digitalWrite(pin1, B0);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B1);        
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B0);
   if(pos == 0)
   {
    digitalWrite(gnd1, B1);
    digitalWrite(gnd2, B0);
    digitalWrite(gnd3, B0);
   }
else if (pos == 0)
   {
    digitalWrite(gnd1, B0);
    digitalWrite(gnd2, B1);
    digitalWrite(gnd3, B0);
   }
else 
   {
    digitalWrite(gnd1, B0);
    digitalWrite(gnd2, B0);
    digitalWrite(gnd3, B1);
   }
 }
void show6(int pos){   
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B1);       
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B0);
   if(pos == 0)
   {
    digitalWrite(gnd1, B1);
    digitalWrite(gnd2, B0);
    digitalWrite(gnd3, B0);
   }
else if (pos == 0)
   {
    digitalWrite(gnd1, B0);
    digitalWrite(gnd2, B1);
    digitalWrite(gnd3, B0);
   }
else
   {
    digitalWrite(gnd1, B0);
    digitalWrite(gnd2, B0);
    digitalWrite(gnd3, B1);
   }
 }
void show7(int pos){   
   digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B0);        
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
   if(pos == 0)
   {
    digitalWrite(gnd1, B1);
    digitalWrite(gnd2, B0);
    digitalWrite(gnd3, B0);
   }
else if (pos == 0)
   {
    digitalWrite(gnd1, B0);
    digitalWrite(gnd2, B1);
    digitalWrite(gnd3, B0);
   }
else
   {
    digitalWrite(gnd1, B0);
    digitalWrite(gnd2, B0);
    digitalWrite(gnd3, B1);
   }
 }
void show8(int pos){   
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B1);        
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
   if (pos == 0)
   {
    digitalWrite(gnd1, B1);
    digitalWrite(gnd2, B0);
    digitalWrite(gnd3, B0);
   }
else if (pos == 0)
   {
    digitalWrite(gnd1, B0);
    digitalWrite(gnd2, B1);
    digitalWrite(gnd3, B0);
   }
else
   {
    digitalWrite(gnd1, B0);
    digitalWrite(gnd2, B0);
    digitalWrite(gnd3, B1);
   }
 }
void show9(int pos){   
   digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B1);        
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B1);
   if(pos == 0)
   {
    digitalWrite(gnd1, B1);
    digitalWrite(gnd2, B0);
    digitalWrite(gnd3, B0);
   }
else if (pos == 0)
   {
    digitalWrite(gnd1, B0);
    digitalWrite(gnd2, B1);
    digitalWrite(gnd3, B0);
   }
else
   {
    digitalWrite(gnd1, B0);
    digitalWrite(gnd2, B0);
    digitalWrite(gnd3, B1);
   }
}

I am heading in the right direction now?

Heading in the right direction, yes. Tripping over your feet, yes.

First, the showX functions are not supposed to be defined in the setup method. They go before setup(), after setup() but before loop(), or after loop().

Second, the show0 method contains this:

   if(pos == 0)
   {
    digitalWrite(gnd1, B1);
    digitalWrite(gnd2, B0);
    digitalWrite(gnd3, B0);
   }
else if (pos == 0)
   {
    digitalWrite(gnd1, B0);
    digitalWrite(gnd2, B1);
    digitalWrite(gnd3, B0);
   }
else
   {
    digitalWrite(gnd1, B0);
    digitalWrite(gnd2, B0);
    digitalWrite(gnd3, B1);
   }

If pos is 0, the first block executes. If not, the else if and else portions are evaluated. You have two blocks, mutually exclusive, for the same condition.

Move the curly brace that defines the end of setup before "void show0(int pos){" to resolve the first issue. Rethink the logic of the if/else if/else structure to resolve the second issue (replicated in several places).