![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Semafor 6 sec crveno, 3 zuto, 10 zeleno dok je u kvaru svira himna
Dependencies: mbed segmentDisplay_semafor_himna
main.cpp@0:e2e3de23a106, 2021-12-09 (annotated)
- Committer:
- ihrvojevic
- Date:
- Thu Dec 09 18:50:58 2021 +0000
- Revision:
- 0:e2e3de23a106
konstrukcijski
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ihrvojevic | 0:e2e3de23a106 | 1 | #include "mbed.h" |
ihrvojevic | 0:e2e3de23a106 | 2 | #include "segmentDisplay.h" |
ihrvojevic | 0:e2e3de23a106 | 3 | |
ihrvojevic | 0:e2e3de23a106 | 4 | Serial pc(USBTX, USBRX); //Komunikacija s računalom |
ihrvojevic | 0:e2e3de23a106 | 5 | DigitalOut redLED(PA_9); //Izlaz crvene LED diode |
ihrvojevic | 0:e2e3de23a106 | 6 | DigitalOut yellowLED(PC_7); //Izlaz žute LED diode |
ihrvojevic | 0:e2e3de23a106 | 7 | DigitalOut greenLED(PB_6); //Izlaz zelene LED diode |
ihrvojevic | 0:e2e3de23a106 | 8 | PwmOut buzzer(PA_7); //Zvučnik |
ihrvojevic | 0:e2e3de23a106 | 9 | InterruptIn button1(PA_6); //Interrupt pin - tipkalo1 |
ihrvojevic | 0:e2e3de23a106 | 10 | Ticker blink; //Ticker za blinkanje LED dioda |
ihrvojevic | 0:e2e3de23a106 | 11 | Timer debounce; //Timer za micanje debouncinga |
ihrvojevic | 0:e2e3de23a106 | 12 | void toggle(void); //Semafor u kvaru - zaustavlja se rad |
ihrvojevic | 0:e2e3de23a106 | 13 | int state=1; // inicijalizacija varijable |
ihrvojevic | 0:e2e3de23a106 | 14 | int sec=0; // inicijalizacija varijable |
ihrvojevic | 0:e2e3de23a106 | 15 | |
ihrvojevic | 0:e2e3de23a106 | 16 | float frequency[]={659.3, 659.3, 659.3, 587.3, 587.3, 523.3, 523.3, 392, 349.2, 329.6, 349.2, |
ihrvojevic | 0:e2e3de23a106 | 17 | 392, 440, 392, 349.2, 329.6, 349.2, 392, 659.3, 659.3, 659.3, 587.3, 587.3, 523.3, 523.3, 392, |
ihrvojevic | 0:e2e3de23a106 | 18 | 349.2, 329.6, 349.2, 392, 440, 493.9, 493.9, 587.3, 523.3, 493.9, 493.9, 493.9, 440, 493.9, |
ihrvojevic | 0:e2e3de23a106 | 19 | 493.9, 523.3, 587.3, 493.9, 587.3, 587.3, 587.3, 587.3, 587.3, 523.3, 493.9, 440, 392, 659.3, |
ihrvojevic | 0:e2e3de23a106 | 20 | 659.3, 659.3, 587.3, 587.3, 523.3, 523.3, 392, 349.2, 329.6, 349.2, 392, 440, 493.9, 493.9, |
ihrvojevic | 0:e2e3de23a106 | 21 | 587.3, 523.3, 0}; |
ihrvojevic | 0:e2e3de23a106 | 22 | //trajanje nota |
ihrvojevic | 0:e2e3de23a106 | 23 | float beat[]={1, 1, 1.5, 0.5, 0.5, 0.5, 1, 2, 0.5, 0.5, 0.5, 0.5, 2, 0.5, 0.5, 0.5, 0.5, 2, 1, 1, 1.5, 0.5, |
ihrvojevic | 0:e2e3de23a106 | 24 | 0.5, 0.5, 1, 2, 0.5, 0.5, 0.5, 0.5, 2, 0.5, 0.5, 1, 2, 1, 1, 1.5, 0.5, 1, 0.5, 0.5, 1.5, 0.5, 0.5, 0.5, 0.5, |
ihrvojevic | 0:e2e3de23a106 | 25 | 0.5, 1, 1, 1, 1, 2, 1, 1, 1.5, 0.5, 0.5, 0.5, 1, 2, 0.5, 0.5, 0.5, 0.5, 2, 0.5, 0.5, 1, 2, 0.5}; |
ihrvojevic | 0:e2e3de23a106 | 26 | |
ihrvojevic | 0:e2e3de23a106 | 27 | void yellowBlink() |
ihrvojevic | 0:e2e3de23a106 | 28 | { |
ihrvojevic | 0:e2e3de23a106 | 29 | yellowLED=!yellowLED; |
ihrvojevic | 0:e2e3de23a106 | 30 | } |
ihrvojevic | 0:e2e3de23a106 | 31 | void odbrojavanje() // Odbrojavanje vremenskog perioda do promjene signalizacije na semaforu |
ihrvojevic | 0:e2e3de23a106 | 32 | { |
ihrvojevic | 0:e2e3de23a106 | 33 | while (sec>0 && state!=5) { |
ihrvojevic | 0:e2e3de23a106 | 34 | Seg1 = SegConvert(sec); // Ispisivanje na 7.seg display |
ihrvojevic | 0:e2e3de23a106 | 35 | wait_us (1000000); |
ihrvojevic | 0:e2e3de23a106 | 36 | sec--; |
ihrvojevic | 0:e2e3de23a106 | 37 | } |
ihrvojevic | 0:e2e3de23a106 | 38 | } |
ihrvojevic | 0:e2e3de23a106 | 39 | void signalizacija(int red, int yellow, int green, int trajanje) // Signalizacija semafora |
ihrvojevic | 0:e2e3de23a106 | 40 | { |
ihrvojevic | 0:e2e3de23a106 | 41 | redLED = red; |
ihrvojevic | 0:e2e3de23a106 | 42 | yellowLED = yellow; |
ihrvojevic | 0:e2e3de23a106 | 43 | greenLED= green; |
ihrvojevic | 0:e2e3de23a106 | 44 | sec = trajanje; |
ihrvojevic | 0:e2e3de23a106 | 45 | } |
ihrvojevic | 0:e2e3de23a106 | 46 | |
ihrvojevic | 0:e2e3de23a106 | 47 | int main() |
ihrvojevic | 0:e2e3de23a106 | 48 | { |
ihrvojevic | 0:e2e3de23a106 | 49 | debounce.start(); |
ihrvojevic | 0:e2e3de23a106 | 50 | SegInit(); // Pozivanje funkcije za inicijalizaciju 7 seg. displaya |
ihrvojevic | 0:e2e3de23a106 | 51 | while (true) { |
ihrvojevic | 0:e2e3de23a106 | 52 | button1.rise(&toggle); // Interrupt, Semafor trenutno ne radi svira himna |
ihrvojevic | 0:e2e3de23a106 | 53 | if(state==1) { // Crveno svijetlo na semaforu |
ihrvojevic | 0:e2e3de23a106 | 54 | pc.printf("Zaustavi se.\n"); |
ihrvojevic | 0:e2e3de23a106 | 55 | blink.detach(); |
ihrvojevic | 0:e2e3de23a106 | 56 | signalizacija(1,0,0,6); |
ihrvojevic | 0:e2e3de23a106 | 57 | state=2; |
ihrvojevic | 0:e2e3de23a106 | 58 | } else if(state==2) { // Prijelaz iz crvenog svijetla u žuto te u zeleno svijetlo |
ihrvojevic | 0:e2e3de23a106 | 59 | pc.printf("Pripremi se za kretanje.\n"); |
ihrvojevic | 0:e2e3de23a106 | 60 | signalizacija(1,1,0,3); |
ihrvojevic | 0:e2e3de23a106 | 61 | state=3; |
ihrvojevic | 0:e2e3de23a106 | 62 | } else if(state==3) { // Zeleno svijetlo na semaforu |
ihrvojevic | 0:e2e3de23a106 | 63 | pc.printf("Kreni.\n"); |
ihrvojevic | 0:e2e3de23a106 | 64 | signalizacija(0,0,1,9); |
ihrvojevic | 0:e2e3de23a106 | 65 | state=4; |
ihrvojevic | 0:e2e3de23a106 | 66 | } else if(state==4) { // Prijelaz iz zelenoga svijetla u žuto te u crveno svijetlo |
ihrvojevic | 0:e2e3de23a106 | 67 | pc.printf("Priprema za crveno svijetlo.\n"); |
ihrvojevic | 0:e2e3de23a106 | 68 | signalizacija(0,1,0,3); |
ihrvojevic | 0:e2e3de23a106 | 69 | state=1; |
ihrvojevic | 0:e2e3de23a106 | 70 | |
ihrvojevic | 0:e2e3de23a106 | 71 | } else if(state==5) { // Semafor u kvaru |
ihrvojevic | 0:e2e3de23a106 | 72 | pc.printf("Semafor u kvaru.\n"); |
ihrvojevic | 0:e2e3de23a106 | 73 | signalizacija(0,0,0,0); |
ihrvojevic | 0:e2e3de23a106 | 74 | blink.attach(&yellowBlink, 0.75); // Ticker blinkanje LED žute |
ihrvojevic | 0:e2e3de23a106 | 75 | state=6; |
ihrvojevic | 0:e2e3de23a106 | 76 | Seg1 = SegConvert(10); |
ihrvojevic | 0:e2e3de23a106 | 77 | for (int i=0; i<=70; i++) { |
ihrvojevic | 0:e2e3de23a106 | 78 | buzzer.period(1/(frequency[i])); // postavljane PWM perioda |
ihrvojevic | 0:e2e3de23a106 | 79 | buzzer=0.5; // set duty cycle |
ihrvojevic | 0:e2e3de23a106 | 80 | wait(0.5*beat[i]); |
ihrvojevic | 0:e2e3de23a106 | 81 | if (i==70){ |
ihrvojevic | 0:e2e3de23a106 | 82 | state=4; |
ihrvojevic | 0:e2e3de23a106 | 83 | } |
ihrvojevic | 0:e2e3de23a106 | 84 | } |
ihrvojevic | 0:e2e3de23a106 | 85 | } |
ihrvojevic | 0:e2e3de23a106 | 86 | odbrojavanje(); |
ihrvojevic | 0:e2e3de23a106 | 87 | } |
ihrvojevic | 0:e2e3de23a106 | 88 | |
ihrvojevic | 0:e2e3de23a106 | 89 | } |
ihrvojevic | 0:e2e3de23a106 | 90 | |
ihrvojevic | 0:e2e3de23a106 | 91 | //Interrupt, Semafor u kvaru |
ihrvojevic | 0:e2e3de23a106 | 92 | void toggle() |
ihrvojevic | 0:e2e3de23a106 | 93 | { |
ihrvojevic | 0:e2e3de23a106 | 94 | if (debounce.read_ms()>200) { // only allow toggle if debounce timer |
ihrvojevic | 0:e2e3de23a106 | 95 | state=5; // has passed 200 ms |
ihrvojevic | 0:e2e3de23a106 | 96 | } |
ihrvojevic | 0:e2e3de23a106 | 97 | debounce.reset(); // restart timer when the toggle is performed |
ihrvojevic | 0:e2e3de23a106 | 98 | |
ihrvojevic | 0:e2e3de23a106 | 99 | } |
ihrvojevic | 0:e2e3de23a106 | 100 | |
ihrvojevic | 0:e2e3de23a106 | 101 |