Advertisement
Guest User

Star Wars "Imperial March" on Arduino

a guest
Jan 4th, 2014
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.59 KB | None | 0 0
  1. #include "pitches.h"
  2.  
  3. const int ledPin = 13;      // build-in LED
  4. const int btnPin = 2;       // button pin
  5. const int spkPin = 12;      // piezo speaker pin
  6. const int ledPins[] { 5, 6, 7, 8, 9, 10 }; // "music visualization" LEDs
  7.  
  8. // Melody is written as:                   
  9. // | note | duration | delay |
  10. // note is predefined pitch value
  11. // duration and delay are fractions of a second (1000 / N)         
  12. int melody[] = {
  13.     // intro 1..
  14.     NOTE_A3,    2,  8,
  15.     NOTE_A3,    2,  8,
  16.     NOTE_A3,    2,  8,
  17.     NOTE_F3,    3,  8,
  18.     NOTE_C4,    8,  16,        
  19.     NOTE_A3,    2,  8,
  20.     NOTE_F3,    3,  8,
  21.     NOTE_C4,    8,  16,
  22.     NOTE_A3,    1,  8,
  23.     // intro 2
  24.     NOTE_E4,    2,  8,
  25.     NOTE_E4,    2,  8,
  26.     NOTE_E4,    2,  8,
  27.     NOTE_F4,    3,  8,
  28.     NOTE_C4,    8,  16,
  29.     NOTE_GS3,   2,  8,
  30.     NOTE_F3,    3,  8,
  31.     NOTE_C4,    8,  16,
  32.     NOTE_A3,    1,  8,    
  33.                
  34.     // interesting part begins here :)
  35.     NOTE_A4,    2,  8,
  36.     NOTE_A3,    3,  8,
  37.     NOTE_A3,    6,  16,
  38.     NOTE_A4,    2,  8,
  39.     NOTE_GS4,   4,  8,
  40.     NOTE_G4,    4,  32,
  41.     NOTE_FS4,   8,  32,
  42.     NOTE_F4,    8,  32,
  43.     NOTE_FS4,   4,  2,  // extra delay
  44.  
  45.     NOTE_AS3,   4,  8,
  46.     NOTE_DS4,   2,  16,
  47.     NOTE_D4,    4,  8,
  48.     NOTE_CS4,   4,  32,
  49.     NOTE_C4,    8,  32,
  50.     NOTE_AS3,   8,  32,
  51.     NOTE_C4,    4,  2,  // extra delay
  52.  
  53.     NOTE_F3,    8,  8,
  54.     NOTE_GS3,   2,  16,
  55.     NOTE_F3,    3,  32,
  56.     NOTE_A3,    8,  16,
  57.     NOTE_C4,    2,  16,
  58.     NOTE_A4,    3,  32,
  59.     NOTE_C4,    8,  32,
  60.     NOTE_E4,    1,  8,
  61.  
  62.     // repeat...
  63.     NOTE_A4,    2,  8,
  64.     NOTE_A3,    3,  8,
  65.     NOTE_A3,    6,  16,
  66.     NOTE_A4,    2,  8,
  67.     NOTE_GS4,   4,  8,
  68.     NOTE_G4,    4,  32,
  69.     NOTE_FS4,   8,  32,
  70.     NOTE_F4,    8,  32,
  71.     NOTE_FS4,   4,  2,  // extra delay
  72.    
  73.     NOTE_AS3,   4,  8,
  74.     NOTE_DS4,   2,  16,
  75.     NOTE_D4,    4,  8,
  76.     NOTE_CS4,   4,  16,
  77.     NOTE_C4,    8,  32,
  78.     NOTE_AS3,   8,  32,
  79.     NOTE_C4,    4,  2,  // extra delay
  80.  
  81.     NOTE_F3,    8,  8,
  82.     NOTE_GS3,   2,  16,
  83.     NOTE_F3,    3,  32,
  84.     NOTE_C4,    8,  32,
  85.    
  86.     // ending
  87.     NOTE_A3,    2,  8,
  88.     NOTE_F3,    3,  8,
  89.     NOTE_C3,    8,  16,
  90.     NOTE_A3,    1,  1
  91. };
  92.                    
  93.                      
  94. int length = 66;
  95. int noteIdx = 0;
  96. int curDelay = 0;
  97. unsigned long tmpTime = 0;
  98.  
  99. int ledState = LOW;
  100. boolean pressed = false;
  101.  
  102. const int maxVal = 255;
  103. int currentLedsAmount = 0;
  104. int fadeInterval = 0;
  105. int fadeTmp = 0;
  106.  
  107. void setLeds(int note)
  108. {
  109.     currentLedsAmount = map(note, NOTE_C3, NOTE_A4, 1, 3);
  110.     updateLeds(currentLedsAmount);
  111. }
  112.  
  113. void updateLeds(int amount)
  114. {        
  115.     int vals[6] = {0,0,0,0,0,0};
  116.    
  117.     if (amount > 0) {
  118.         vals[2] = maxVal;
  119.         vals[3] = maxVal;
  120.     }
  121.     if (amount > 1) {
  122.         vals[1] = maxVal;
  123.                 vals[4] = maxVal;
  124.     }
  125.     if (amount > 2) {
  126.         vals[0] = maxVal;
  127.         vals[5] = maxVal;
  128.     }
  129.    
  130.     for (int i=0; i < 6; i++)
  131.         analogWrite(ledPins[i], vals[i]);
  132. }
  133.  
  134. void setup()
  135. {  
  136.     pinMode(ledPin, OUTPUT);
  137.     pinMode(spkPin, OUTPUT);
  138.     pinMode(btnPin, INPUT);
  139.     for (int i=0; i < 6; i++)
  140.         pinMode(ledPins[i], OUTPUT);
  141.     Serial.begin(9600);
  142. }
  143.  
  144. void loop()
  145. {
  146.         // play next note
  147.         unsigned long time = millis();
  148.         unsigned long dt = time - tmpTime;
  149.     if (ledState == HIGH)
  150.     {  
  151.         if (dt > curDelay) {
  152.             int freq = melody[noteIdx*3];
  153.             int dur = 1000 / melody[noteIdx*3+1];
  154.             curDelay = dur + 500 / melody[noteIdx*3+2];
  155.             tmpTime = time;
  156.                         fadeInterval = dur;
  157.             setLeds(freq);
  158.             tone(spkPin, freq, dur);  
  159.             if (++noteIdx >= length)
  160.             noteIdx = 0;
  161.         }
  162.     }
  163.  
  164.     // check button state and set build-in LED
  165.  
  166.     int newState = digitalRead(btnPin);
  167.  
  168.     if (newState == HIGH && !pressed) {
  169.                 pressed = true;          
  170.     }
  171.     else if (pressed && newState == LOW) {
  172.             ledState = ledState == HIGH ? LOW : HIGH;
  173.                 noteIdx = 0;
  174.             pressed = false;
  175.     }
  176.  
  177.     digitalWrite(ledPin, ledState);
  178.        
  179.         dt = time - tmpTime;
  180.         if (currentLedsAmount > 0 && dt > fadeInterval)
  181.             updateLeds(0);
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement