Code Example

int faster;

void splash(){
    PORTD = B00000100;
    PORTB = B00000010;
    tone(12,500);
    delay(200);
    PORTD = B00001000;
    PORTB = B00000001;
    tone(12,400);
    delay(180);
    PORTD = B10010000;
    PORTB = B00000000;
    tone(12,300);
    delay(160);
    PORTD = B01100000;
    PORTB = B00000000;
    tone(12,200);
    delay(140);
    PORTD = B10010000;
    PORTB = B00000000;
    tone(12,100);
    delay(120);
    PORTD = B00001000;
    PORTB = B00000001;
    tone(12,300);
    delay(100);
    PORTD = B00000100;
    PORTB = B00000010;
    tone(12,150);
    delay(80);
}

void setup()
{
DDRD = DDRD | B11111100; //Data Direction Register for Port D 1 means output
DDRB = DDRB | B00000011; //Data Direction Register for Port B 1 means output
/* This is the same as written above
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT); 
  pinMode(4, OUTPUT); 
  pinMode(5, OUTPUT); 
  pinMode(6, OUTPUT); 
  pinMode(7, OUTPUT); 
  pinMode(8, OUTPUT); 
  pinMode(9, OUTPUT);
  */
}
 
void loop()
{
  for(faster=100; faster>2; faster--){
    faster = faster - 1;
    PORTD = B10000000;
    PORTB = B00000010;
    delay(faster);
    PORTD = B01000000;
    PORTB = B00000001;
    delay(faster);
    PORTD = B10100000;
    PORTB = B00000000;
    delay(faster);
    PORTD = B01010000;
    PORTB = B00000000;
    delay(faster);
    PORTD = B00101000;
    PORTB = B00000000;
    delay(faster);
    PORTD = B00010100;
    PORTB = B00000000;
    delay(faster);
    PORTD = B00001000;
    PORTB = B00000010;
    delay(faster);
    PORTD = B00000100;
    PORTB = B00000001;
    delay(faster);
    tone(12,1500 - faster*10);
    }
    faster = 100;
    splash();
}