Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Box_buzz Box_led
main.cpp
00001 #include "mbed.h" 00002 #include "led.h" 00003 #include "buzzer.h" 00004 00005 00006 Timer debounce; //tajmer koji koristimo za debounce 00007 InterruptIn button(D7); //definiranje tipke za hardverski prekid 00008 LED led1(D3); //definiranje pina za zelena ledicu 00009 LED led2(D4); //definiranje pina za crvena ledicu 00010 Beep buzzer(D5); //definiranje pina za zvučnik 00011 DigitalOut ledica(LED2); //definiranje ledice na Nucleo pločici 00012 Ticker blink; //definiranje tickera 00013 int a=0; //definiranje integera u nulu 00014 int b=0; 00015 int x=0; 00016 00017 //Frequency //Frekvencija svakog tona melodije 00018 float a4=440.00;float b3=246.94;float b4b=446;float b4=493.88; 00019 float c4=261.63;float c4_1=130;float c5=523.25;float c5s=554.40; 00020 float d4=293.66;float d4b=277;float d5=587.33; 00021 float e4=329.63;float e5=659.25; 00022 float f4=349.23;float f4s=370.00;float f5=698.46; 00023 float g3=196; float g4=392.00;float g4s=415.30;float g5=783.99; 00024 00025 //Duration //trajanje tonova 00026 float t1=16; float t2=8; float t3=4; float t4=2; float t5=6; 00027 00028 00029 //"Happy Birthday song" //prva melodija 00030 float happy_freq[25] = {g4,g4,a4,g4,c5,b4,g4,g4,a4,g4,d5,c5,g4,g4,g5,e5,c5,b4,a4,f5,f5,e5,c5,d5,c5}; 00031 float happy_tempo[25] = {t3,t3,t3,t3,t3,t3,t3,t3,t3,t3,t3,t3,t3,t3,t3,t3,t3,t3,t2,t3,t3,t3,t3,t3,t3}; 00032 00033 void song1() 00034 { 00035 for(a=0; a<25; a++) { //petlja za prvu melodiju 00036 buzzer.beep(happy_freq[a], happy_tempo[a]); 00037 led1.state(1); 00038 wait(0.05); 00039 led1.state(0); 00040 wait(1/happy_tempo[a]); //paljenje i gašenje ledica u ritmu melodije 00041 if(x==2){ //pritiskom na tipkalo petlja se prekida i pokreće druga melodija 00042 break;} 00043 } 00044 } 00045 00046 //''Scary song'' 00047 float scary_freq[20]={c5s, f4s, f4s, c5s, f4s, f4s, c5s, f4s, d5, f4s, c5s, f4s, f4s, c5s, f4s, f4s, c5s, f4s, d5, f4s}; 00048 float scary_tempo[20]={t5, t5, t5, t5, t5, t5, t5, t5, t5, t5, t5, t5, t5, t5, t5, t5, t5, t5, t5, t5}; 00049 00050 void song2() 00051 { 00052 for(b=0; b<20; b++) { 00053 buzzer.beep(scary_freq[b], scary_tempo[b]); 00054 led2.state(0); 00055 wait(0.05); 00056 led2.state(1); 00057 wait(1/scary_tempo[b]); 00058 if (x==3){ 00059 led1.state(0); 00060 led2.state(0); 00061 buzzer.beep(1000, 0.01); 00062 break;} 00063 } 00064 } 00065 //Funkcija za detekciju tipke 00066 void pressbutton() 00067 { 00068 if(debounce.read_ms()>100) { //sprečava signalu da titra 00069 x++; 00070 } 00071 debounce.reset(); 00072 } 00073 00074 00075 //Funkcija za blicanje zelene tipke kada je sklop aktivan 00076 void active() 00077 { 00078 if(x==1 or x==2) 00079 { 00080 ledica=!ledica; 00081 } 00082 else{ 00083 ledica=0; 00084 } 00085 } 00086 //Postavljanje početnog stanja sklopa 00087 int main() 00088 { 00089 led1.state(0); 00090 led2.state(0); 00091 debounce.start(); 00092 button.rise(&pressbutton); // poziva funkciju pressbutton na rastući brid signala tipke 00093 blink.attach(&active,0.5); // blink ticker koji poziva funkciju active u intervalu 0.5 00094 //while petlja 00095 while (true) { 00096 if(x==1){ 00097 song1(); 00098 } 00099 else{ 00100 if(x==2){ 00101 song2(); 00102 } 00103 if(x==3) 00104 { 00105 x=0; 00106 } 00107 } 00108 } 00109 }
Generated on Sun Jul 17 2022 23:02:38 by
1.7.2