Program se sastoji od 3 ledice, 3 otpornika i piezo zvucnikom. U pocetnom stanju LED lampice i piezo zvucnik je ugasen. Pritiskom tipke na mikrokontroleru pokrece se prva melodija, crvena i zelena ledica naizmjenicno svijetle u ritmu melodije. Drugim pritiskom pokrece se druga melodija, zelena i zuta ledica naizmjenicno svijetle u ritmu melodije. Trecim pritiskom pokrece se treca melodije, crvena i zuta ledica naizmjenicno svijetle u ritmu melodije. Cetvrtim pritiskom zaustavlja se melodija odnosno rad piezo zvucnika, ledice se gase i program se postavlja u pocetno stanje.
Dependencies: Cicvaric_buzzer Cicvaric_led
main.cpp
- Committer:
- petarcicvaric
- Date:
- 2021-12-02
- Revision:
- 8:35e50a21f3e8
- Parent:
- 7:73b6e074ca35
File content as of revision 8:35e50a21f3e8:
#include "mbed.h" #include "led.h" #include "buzzer.h" Serial pc(USBTX, USBRX); LED led1(D2); LED led2(D3); LED led3(D4); Beep buzzer(D5); Timer debounce; InterruptIn tipka(D6); DigitalOut zelenaledica(LED2); Ticker blink; int x=0; int i=0; int j=0; int k=0; //Frekvencija nota float a4=440.00; float b3=246.94; float b4=493.88; float c4=261.63; float c5=523.25; float c5s=554.40; float d4=293.66; float d5=587.33; float e4=329.63; float e5=659.25; float f4=349.23; float f4s=370.00; float f5=698.46; float g4=392.00; float g4s=415.30; float g5=783.99; //Trajanje tona float t1=16; float t2=8; float t3=4; float t4=2; float t5=1; //Melodija Jingle Bells float jinglebells_freq[26] = {e5,e5,e5,e5,e5,e5,e5,g5,c5,d5,e5,f5,f5,f5,f5,f5,e5,e5,e5,e5,e5,d5,d5,e5,d5,g5}; float jinglebells_tempo[26] = {t2,t2,t3,t2,t2,t3,t2,t2,t2,t2,t4,t2,t2,t2,t2,t2,t2,t2,t1,t1,t2,t2,t2,t2,t3,t3,}; //Melodija Santa Claus is coming to town float santaclaus_freq[28] = {g4,e4,f4,g4,g4,g4,a4,b4,c5,c5,c5,e4,f4,g4,g4,g4,a4,g4,f4,f4,e4,g4,c4,e4,d4,f4,b3,c4}; float santaclaus_tempo[28] = {t2,t2,t2,t3,t3,t3,t2,t2,t3,t3,t3,t2,t2,t3,t3,t3,t2,t2,t3,t4,t3,t3,t3,t3,t3,t4,t3,t5}; //Melodija We wish you a merry Christmas float wewish_freq[30] = {e4,a4,a4,b4,a4,g4s,f4s,f4s,f4s,b4,b4,c5s,b4,a4,g4s,e4,e4,c5s,c5s,d5,c5s,b4,a4,f4s,e4,e4,f4s,b4,g4s,a4}; float wewish_tempo[30] = {t3,t3,t2,t2,t2,t2,t3,t3,t3,t3,t2,t2,t2,t2,t3,t3,t3,t3,t2,t2,t2,t2,t3,t3,t2,t2,t3,t3,t3,t4}; //Funkcija za melodiju "Jingle Bells" void Melodija1() { for(i=0; i<26; i++) { buzzer.beep(jinglebells_freq[i], jinglebells_tempo[i]); led1.stanje(1); led2.stanje(0); wait(0.075); led1.stanje(0); led2.stanje(1); wait(1/jinglebells_tempo[i]); if(x==2){ break;} } } //Funkcija za melodiju "Santa Claus is coming to town" void Melodija2() { for(j=0; j<28; j++) { buzzer.beep(santaclaus_freq[j], santaclaus_tempo[j]); led2.stanje(1); led3.stanje(0); wait(0.075); led2.stanje(0); led3.stanje(1); wait(1/santaclaus_tempo[j]); if(x==3){ break;} } } //Funkcija za melodiju "We wish you a merry Christmas" void Melodija3() { for(k=0; k<30; k++) { buzzer.beep(wewish_freq[k], wewish_tempo[k]); led1.stanje(1); led3.stanje(0); wait(0.075); led1.stanje(0); led3.stanje(1); wait(1/wewish_tempo[k]); if(x==4){ led1.stanje(0); led2.stanje(0); led3.stanje(0); buzzer.beep(500, 0.05); break;} } }//Funkcija za detekciju pritiska tipkala void TipkaStisnuta() { if(debounce.read_ms()>200) { x++; } debounce.reset(); } //Funkcija za blinkanje integrirane zelene LED diode ukoliko je sklop aktivan void SklopAktivan() { if(x==1 or x==2 or x==3) { zelenaledica=!zelenaledica; } else{ zelenaledica=0; } } int main() {//Postavljanje početnog stanja LED-ica led1.stanje(0); led2.stanje(0); led3.stanje(0); debounce.start(); tipka.rise(&TipkaStisnuta); blink.attach(&SklopAktivan,0.5); //While petlja while (true) { if(x==1){ Melodija1(); } else{ if(x==2){ Melodija2(); } if(x==3){ Melodija3(); } if(x==4){ x=0; } } } }