Instructables

Help with my arduino project. (2, 7 segment with piezo buzzer and relay)

Hi, i need some help with my arduino project.

I have 2, 7 segment displays which countdown from 20 to zero en then the piezo buzzer beebs for a while.
My question is: 

I want a relay switched on for 20 second at the same time with the 7 segment countdown.
so if the countdown starts, the relay opens and when it is complete  20, 19, 18, 17, 16,15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0  
then the relay should close.

here is my code for so far.
if anyone can help me with tips or anything, that would be awesome ;)

gr Mark

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 = 100;                               //   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);
  pinMode(12, OUTPUT);              // Output for the repeating beep
 
 
  for (int i=0; i    digitalWrite(pin1, B0);                      
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B1);                        
   digitalWrite(gnd2, B0);
   delay(5);                                        //          Writes 20 to the display.
   digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(5);
  
  }
 
for (int i=0; i    digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(5);                                        //          Writes 19 to the display.
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(5);
  
  }
  for (int i=0; i    digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(5);                                        //          Writes 18 to the display.
   digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(5);
  
  }
  for (int i=0; i    digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(5);                                        //          Writes 17 to the display.
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(5);
  
  }
  for (int i=0; i    digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(5);                                        //          Writes 16 to the display.
   digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B1);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(5);
  
  }
  for (int i=0; i    digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(5);                                        //          Writes 15 to the display.
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B1);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(5);
  
  }
  for (int i=0; i    digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(5);                                        //          Writes 14 to the display.
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(5);
  
  }
  for (int i=0; i    digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(5);                                        //          Writes 13 to the display.
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(5);
  
  }
  for (int i=0; i    digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(5);                                        //          Writes 12 to the display.
   digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(5);
  
  }
  for (int i=0; i    digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(5);                                        //          Writes 11 to the display.
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(5);
  
  }
  for (int i=0; i    digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(5);                                        //          Writes 10 to the display.
   digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(5);
  
  }
  for (int i=0; i    digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(5);                                        //          Writes 09 to the display.
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(5);
  
  }
 
  for (int i=0; i    digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(5);                                        //          Writes 08 to the display.
   digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(5);
  
  }
  for (int i=0; i    digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(5);                                        //          Writes 07 to the display.
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(5);
  
  }
  for (int i=0; i    digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(5);                                        //          Writes 06 to the display.
   digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B1);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(5);
  
  }
  for (int i=0; i    digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(5);                                        //          Writes 05 to the display.
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B1);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(5);
  
  }
  for (int i=0; i    digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(5);                                        //          Writes 04 to the display.
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(5);
  
  }
  for (int i=0; i    digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(5);                                        //          Writes 03 to the display.
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(5);
  
  }
  for (int i=0; i    digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(5);                                        //          Writes 02 to the display.
   digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B1);
   digitalWrite(pin4, B0);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(5);
  
  }
  for (int i=0; i    digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(5);                                        //          Writes 01 to the display.
   digitalWrite(pin1, B1);
   digitalWrite(pin2, B1);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B1);
   digitalWrite(pin6, B1);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(5);
  
  }
  for (int i=0; i    digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B1);
   digitalWrite(gnd2, B0);
   delay(5);                                        //          Writes 00 to the display.
   digitalWrite(pin1, B0);
   digitalWrite(pin2, B0);
   digitalWrite(pin3, B0);
   digitalWrite(pin4, B1);
   digitalWrite(pin5, B0);
   digitalWrite(pin6, B0);
   digitalWrite(pin7, B0);
   digitalWrite(gnd1, B0);
   digitalWrite(gnd2, B1);
   delay(5);
  
  }
 
 
}
void loop() {
 
  digitalWrite(12, HIGH);   // set the beep on
  delay(300);              // wait for amount of time
  digitalWrite(12, LOW);    // set the beep off
  delay(500);
 
  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);
  } 
 
}
jduffy541 year ago
I'd suggest a separate void to drive the display. basically, just use a series of if-then statements for the digits 0-9, then just set a variable to the value. here's a syntax-less bit of "code" as an example

void loop(){
delay 1
value--
if (value == 0){
digitalWrite (buzzerpin, HIGH);
//fading code for the buzzer
}
refreshdisplay(); //this part outputs the numbers
}

void refreshdisplay(){
value1 = map(value, 0, 90, 0, 90);//sets ones between 0 and 90//this an next line not really needed, but nice if you want to change the input range
value10 = map(value, 0, 9, 0, 9);//sets tens between 0 and 9
value1 = round(value1);//rounds the variables
value10 = round(value10);
value1 = value1 - (value10 * 10);//removes all but the ones-place digit
if (value1 ==0){
digitalWrite(1, HIGH);//change these lines for your display, basically what you did above
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
}

if (value1 ==1){
digitalWrite(1, HIGH);//change these lines for your display, basically what you did above
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
}
if (value1 ==2){
digitalWrite(1, HIGH);//change these lines for your display, basically what you did above
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
}
//continue this up to 9

if (value10 ==0){
digitalWrite(1, HIGH);//change these lines for your display, basically what you did above
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
}
//continue this up to 9

}

thats pretty much everything. Remember, this does NOT have proper syntax, so it wont run. Its just to give you an idea. It will make it easier to run your code because you don't have to sorry as much about driving the display, as thats done in void refreshdisplay(){
markie (author)  jduffy541 year ago
Thanks for the reply, it was a wild ago since i posted this. in the meanwhile i got it up and running :)
(i should delete this post, and learn better english)
Thanks anyway for your effort, appreciate it.

gr mark
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!