Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pitches.h"
- const int ledPin = 13; // build-in LED
- const int btnPin = 2; // button pin
- const int spkPin = 12; // piezo speaker pin
- const int ledPins[] { 5, 6, 7, 8, 9, 10 }; // "music visualization" LEDs
- // Melody is written as:
- // | note | duration | delay |
- // note is predefined pitch value
- // duration and delay are fractions of a second (1000 / N)
- int melody[] = {
- // intro 1..
- NOTE_A3, 2, 8,
- NOTE_A3, 2, 8,
- NOTE_A3, 2, 8,
- NOTE_F3, 3, 8,
- NOTE_C4, 8, 16,
- NOTE_A3, 2, 8,
- NOTE_F3, 3, 8,
- NOTE_C4, 8, 16,
- NOTE_A3, 1, 8,
- // intro 2
- NOTE_E4, 2, 8,
- NOTE_E4, 2, 8,
- NOTE_E4, 2, 8,
- NOTE_F4, 3, 8,
- NOTE_C4, 8, 16,
- NOTE_GS3, 2, 8,
- NOTE_F3, 3, 8,
- NOTE_C4, 8, 16,
- NOTE_A3, 1, 8,
- // interesting part begins here :)
- NOTE_A4, 2, 8,
- NOTE_A3, 3, 8,
- NOTE_A3, 6, 16,
- NOTE_A4, 2, 8,
- NOTE_GS4, 4, 8,
- NOTE_G4, 4, 32,
- NOTE_FS4, 8, 32,
- NOTE_F4, 8, 32,
- NOTE_FS4, 4, 2, // extra delay
- NOTE_AS3, 4, 8,
- NOTE_DS4, 2, 16,
- NOTE_D4, 4, 8,
- NOTE_CS4, 4, 32,
- NOTE_C4, 8, 32,
- NOTE_AS3, 8, 32,
- NOTE_C4, 4, 2, // extra delay
- NOTE_F3, 8, 8,
- NOTE_GS3, 2, 16,
- NOTE_F3, 3, 32,
- NOTE_A3, 8, 16,
- NOTE_C4, 2, 16,
- NOTE_A4, 3, 32,
- NOTE_C4, 8, 32,
- NOTE_E4, 1, 8,
- // repeat...
- NOTE_A4, 2, 8,
- NOTE_A3, 3, 8,
- NOTE_A3, 6, 16,
- NOTE_A4, 2, 8,
- NOTE_GS4, 4, 8,
- NOTE_G4, 4, 32,
- NOTE_FS4, 8, 32,
- NOTE_F4, 8, 32,
- NOTE_FS4, 4, 2, // extra delay
- NOTE_AS3, 4, 8,
- NOTE_DS4, 2, 16,
- NOTE_D4, 4, 8,
- NOTE_CS4, 4, 16,
- NOTE_C4, 8, 32,
- NOTE_AS3, 8, 32,
- NOTE_C4, 4, 2, // extra delay
- NOTE_F3, 8, 8,
- NOTE_GS3, 2, 16,
- NOTE_F3, 3, 32,
- NOTE_C4, 8, 32,
- // ending
- NOTE_A3, 2, 8,
- NOTE_F3, 3, 8,
- NOTE_C3, 8, 16,
- NOTE_A3, 1, 1
- };
- int length = 66;
- int noteIdx = 0;
- int curDelay = 0;
- unsigned long tmpTime = 0;
- int ledState = LOW;
- boolean pressed = false;
- const int maxVal = 255;
- int currentLedsAmount = 0;
- int fadeInterval = 0;
- int fadeTmp = 0;
- void setLeds(int note)
- {
- currentLedsAmount = map(note, NOTE_C3, NOTE_A4, 1, 3);
- updateLeds(currentLedsAmount);
- }
- void updateLeds(int amount)
- {
- int vals[6] = {0,0,0,0,0,0};
- if (amount > 0) {
- vals[2] = maxVal;
- vals[3] = maxVal;
- }
- if (amount > 1) {
- vals[1] = maxVal;
- vals[4] = maxVal;
- }
- if (amount > 2) {
- vals[0] = maxVal;
- vals[5] = maxVal;
- }
- for (int i=0; i < 6; i++)
- analogWrite(ledPins[i], vals[i]);
- }
- void setup()
- {
- pinMode(ledPin, OUTPUT);
- pinMode(spkPin, OUTPUT);
- pinMode(btnPin, INPUT);
- for (int i=0; i < 6; i++)
- pinMode(ledPins[i], OUTPUT);
- Serial.begin(9600);
- }
- void loop()
- {
- // play next note
- unsigned long time = millis();
- unsigned long dt = time - tmpTime;
- if (ledState == HIGH)
- {
- if (dt > curDelay) {
- int freq = melody[noteIdx*3];
- int dur = 1000 / melody[noteIdx*3+1];
- curDelay = dur + 500 / melody[noteIdx*3+2];
- tmpTime = time;
- fadeInterval = dur;
- setLeds(freq);
- tone(spkPin, freq, dur);
- if (++noteIdx >= length)
- noteIdx = 0;
- }
- }
- // check button state and set build-in LED
- int newState = digitalRead(btnPin);
- if (newState == HIGH && !pressed) {
- pressed = true;
- }
- else if (pressed && newState == LOW) {
- ledState = ledState == HIGH ? LOW : HIGH;
- noteIdx = 0;
- pressed = false;
- }
- digitalWrite(ledPin, ledState);
- dt = time - tmpTime;
- if (currentLedsAmount > 0 && dt > fadeInterval)
- updateLeds(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement