![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Punjenje i pražnjenje spremnika vode.
main.cpp@0:f5a0afbce297, 2022-07-15 (annotated)
- Committer:
- ipoljak1
- Date:
- Fri Jul 15 16:22:41 2022 +0000
- Revision:
- 0:f5a0afbce297
Ovo je prva revizija. Funkcionalnost ovog programa je da puni i prazni spremnik vode.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ipoljak1 | 0:f5a0afbce297 | 1 | #include "mbed.h" |
ipoljak1 | 0:f5a0afbce297 | 2 | |
ipoljak1 | 0:f5a0afbce297 | 3 | DigitalOut ledgreen(D4); |
ipoljak1 | 0:f5a0afbce297 | 4 | DigitalOut ledred(D5); |
ipoljak1 | 0:f5a0afbce297 | 5 | DigitalOut ledyellow(D6); |
ipoljak1 | 0:f5a0afbce297 | 6 | InterruptIn button(D2); |
ipoljak1 | 0:f5a0afbce297 | 7 | Timer debounce; |
ipoljak1 | 0:f5a0afbce297 | 8 | PwmOut speaker(D9); |
ipoljak1 | 0:f5a0afbce297 | 9 | Timer speaker1; |
ipoljak1 | 0:f5a0afbce297 | 10 | Timer ledice; |
ipoljak1 | 0:f5a0afbce297 | 11 | BusInOut inputSW(D10, D11, D12, D13); |
ipoljak1 | 0:f5a0afbce297 | 12 | |
ipoljak1 | 0:f5a0afbce297 | 13 | void toggle(void); |
ipoljak1 | 0:f5a0afbce297 | 14 | void task1(void); |
ipoljak1 | 0:f5a0afbce297 | 15 | bool flag = false; |
ipoljak1 | 0:f5a0afbce297 | 16 | |
ipoljak1 | 0:f5a0afbce297 | 17 | |
ipoljak1 | 0:f5a0afbce297 | 18 | int main() |
ipoljak1 | 0:f5a0afbce297 | 19 | { |
ipoljak1 | 0:f5a0afbce297 | 20 | |
ipoljak1 | 0:f5a0afbce297 | 21 | debounce.start(); |
ipoljak1 | 0:f5a0afbce297 | 22 | button.mode(PullUp); |
ipoljak1 | 0:f5a0afbce297 | 23 | button.rise(&toggle); |
ipoljak1 | 0:f5a0afbce297 | 24 | inputSW.mode(PullUp); |
ipoljak1 | 0:f5a0afbce297 | 25 | |
ipoljak1 | 0:f5a0afbce297 | 26 | while(1) { |
ipoljak1 | 0:f5a0afbce297 | 27 | |
ipoljak1 | 0:f5a0afbce297 | 28 | switch(inputSW) { |
ipoljak1 | 0:f5a0afbce297 | 29 | |
ipoljak1 | 0:f5a0afbce297 | 30 | case 0x0: |
ipoljak1 | 0:f5a0afbce297 | 31 | |
ipoljak1 | 0:f5a0afbce297 | 32 | if(speaker1.read_ms()>2000) { |
ipoljak1 | 0:f5a0afbce297 | 33 | |
ipoljak1 | 0:f5a0afbce297 | 34 | speaker=0; |
ipoljak1 | 0:f5a0afbce297 | 35 | |
ipoljak1 | 0:f5a0afbce297 | 36 | |
ipoljak1 | 0:f5a0afbce297 | 37 | speaker1.reset(); |
ipoljak1 | 0:f5a0afbce297 | 38 | speaker1.stop(); |
ipoljak1 | 0:f5a0afbce297 | 39 | |
ipoljak1 | 0:f5a0afbce297 | 40 | |
ipoljak1 | 0:f5a0afbce297 | 41 | ledice.start(); |
ipoljak1 | 0:f5a0afbce297 | 42 | |
ipoljak1 | 0:f5a0afbce297 | 43 | |
ipoljak1 | 0:f5a0afbce297 | 44 | } |
ipoljak1 | 0:f5a0afbce297 | 45 | if(ledice.read_ms()>0) { |
ipoljak1 | 0:f5a0afbce297 | 46 | |
ipoljak1 | 0:f5a0afbce297 | 47 | ledgreen = 0; |
ipoljak1 | 0:f5a0afbce297 | 48 | ledred = 0; |
ipoljak1 | 0:f5a0afbce297 | 49 | ledyellow = 0; |
ipoljak1 | 0:f5a0afbce297 | 50 | } |
ipoljak1 | 0:f5a0afbce297 | 51 | |
ipoljak1 | 0:f5a0afbce297 | 52 | |
ipoljak1 | 0:f5a0afbce297 | 53 | if(ledice.read_ms()>1000) { |
ipoljak1 | 0:f5a0afbce297 | 54 | |
ipoljak1 | 0:f5a0afbce297 | 55 | ledgreen=1; |
ipoljak1 | 0:f5a0afbce297 | 56 | } |
ipoljak1 | 0:f5a0afbce297 | 57 | |
ipoljak1 | 0:f5a0afbce297 | 58 | if(ledice.read_ms()>3000) { |
ipoljak1 | 0:f5a0afbce297 | 59 | |
ipoljak1 | 0:f5a0afbce297 | 60 | ledred=1; |
ipoljak1 | 0:f5a0afbce297 | 61 | } |
ipoljak1 | 0:f5a0afbce297 | 62 | if(ledice.read_ms()>5000) { |
ipoljak1 | 0:f5a0afbce297 | 63 | |
ipoljak1 | 0:f5a0afbce297 | 64 | ledyellow=1; |
ipoljak1 | 0:f5a0afbce297 | 65 | ledice.reset(); |
ipoljak1 | 0:f5a0afbce297 | 66 | ledice.stop(); |
ipoljak1 | 0:f5a0afbce297 | 67 | |
ipoljak1 | 0:f5a0afbce297 | 68 | if(debounce.read_ms()>2000) { |
ipoljak1 | 0:f5a0afbce297 | 69 | task1(); |
ipoljak1 | 0:f5a0afbce297 | 70 | wait(2.0); |
ipoljak1 | 0:f5a0afbce297 | 71 | speaker=0; |
ipoljak1 | 0:f5a0afbce297 | 72 | debounce.reset(); |
ipoljak1 | 0:f5a0afbce297 | 73 | } |
ipoljak1 | 0:f5a0afbce297 | 74 | |
ipoljak1 | 0:f5a0afbce297 | 75 | |
ipoljak1 | 0:f5a0afbce297 | 76 | } |
ipoljak1 | 0:f5a0afbce297 | 77 | break; |
ipoljak1 | 0:f5a0afbce297 | 78 | |
ipoljak1 | 0:f5a0afbce297 | 79 | case 0x1: |
ipoljak1 | 0:f5a0afbce297 | 80 | |
ipoljak1 | 0:f5a0afbce297 | 81 | if(flag==false) { |
ipoljak1 | 0:f5a0afbce297 | 82 | |
ipoljak1 | 0:f5a0afbce297 | 83 | ledyellow=0; |
ipoljak1 | 0:f5a0afbce297 | 84 | wait(0.5); |
ipoljak1 | 0:f5a0afbce297 | 85 | ledyellow=1; |
ipoljak1 | 0:f5a0afbce297 | 86 | wait(0.5); |
ipoljak1 | 0:f5a0afbce297 | 87 | ledyellow=0; |
ipoljak1 | 0:f5a0afbce297 | 88 | wait(0.5); |
ipoljak1 | 0:f5a0afbce297 | 89 | ledyellow=1; |
ipoljak1 | 0:f5a0afbce297 | 90 | wait(0.5); |
ipoljak1 | 0:f5a0afbce297 | 91 | ledyellow=0; |
ipoljak1 | 0:f5a0afbce297 | 92 | |
ipoljak1 | 0:f5a0afbce297 | 93 | if(debounce.read_ms()>2000) { |
ipoljak1 | 0:f5a0afbce297 | 94 | task1(); |
ipoljak1 | 0:f5a0afbce297 | 95 | wait(2.0); |
ipoljak1 | 0:f5a0afbce297 | 96 | speaker=0; |
ipoljak1 | 0:f5a0afbce297 | 97 | debounce.reset(); |
ipoljak1 | 0:f5a0afbce297 | 98 | } |
ipoljak1 | 0:f5a0afbce297 | 99 | flag = true; |
ipoljak1 | 0:f5a0afbce297 | 100 | } |
ipoljak1 | 0:f5a0afbce297 | 101 | break; |
ipoljak1 | 0:f5a0afbce297 | 102 | |
ipoljak1 | 0:f5a0afbce297 | 103 | case 0x3: |
ipoljak1 | 0:f5a0afbce297 | 104 | |
ipoljak1 | 0:f5a0afbce297 | 105 | if(flag==true) { |
ipoljak1 | 0:f5a0afbce297 | 106 | |
ipoljak1 | 0:f5a0afbce297 | 107 | ledred=0; |
ipoljak1 | 0:f5a0afbce297 | 108 | wait(0.5); |
ipoljak1 | 0:f5a0afbce297 | 109 | ledred=1; |
ipoljak1 | 0:f5a0afbce297 | 110 | wait(0.5); |
ipoljak1 | 0:f5a0afbce297 | 111 | ledred=0; |
ipoljak1 | 0:f5a0afbce297 | 112 | wait(0.5); |
ipoljak1 | 0:f5a0afbce297 | 113 | ledred=1; |
ipoljak1 | 0:f5a0afbce297 | 114 | wait(0.5); |
ipoljak1 | 0:f5a0afbce297 | 115 | ledred=0; |
ipoljak1 | 0:f5a0afbce297 | 116 | |
ipoljak1 | 0:f5a0afbce297 | 117 | if(debounce.read_ms()>2000) { |
ipoljak1 | 0:f5a0afbce297 | 118 | task1(); |
ipoljak1 | 0:f5a0afbce297 | 119 | wait(2.0); |
ipoljak1 | 0:f5a0afbce297 | 120 | speaker=0; |
ipoljak1 | 0:f5a0afbce297 | 121 | debounce.reset(); |
ipoljak1 | 0:f5a0afbce297 | 122 | } |
ipoljak1 | 0:f5a0afbce297 | 123 | flag = false; |
ipoljak1 | 0:f5a0afbce297 | 124 | } |
ipoljak1 | 0:f5a0afbce297 | 125 | break; |
ipoljak1 | 0:f5a0afbce297 | 126 | |
ipoljak1 | 0:f5a0afbce297 | 127 | case 0x7: |
ipoljak1 | 0:f5a0afbce297 | 128 | |
ipoljak1 | 0:f5a0afbce297 | 129 | if(flag==false) { |
ipoljak1 | 0:f5a0afbce297 | 130 | |
ipoljak1 | 0:f5a0afbce297 | 131 | ledgreen=0; |
ipoljak1 | 0:f5a0afbce297 | 132 | wait(0.5); |
ipoljak1 | 0:f5a0afbce297 | 133 | ledgreen=1; |
ipoljak1 | 0:f5a0afbce297 | 134 | wait(0.5); |
ipoljak1 | 0:f5a0afbce297 | 135 | ledgreen=0; |
ipoljak1 | 0:f5a0afbce297 | 136 | wait(0.5); |
ipoljak1 | 0:f5a0afbce297 | 137 | ledgreen=1; |
ipoljak1 | 0:f5a0afbce297 | 138 | wait(0.5); |
ipoljak1 | 0:f5a0afbce297 | 139 | ledgreen=0; |
ipoljak1 | 0:f5a0afbce297 | 140 | |
ipoljak1 | 0:f5a0afbce297 | 141 | if(debounce.read_ms()>2000) { |
ipoljak1 | 0:f5a0afbce297 | 142 | task1(); |
ipoljak1 | 0:f5a0afbce297 | 143 | wait(2.0); |
ipoljak1 | 0:f5a0afbce297 | 144 | speaker=0; |
ipoljak1 | 0:f5a0afbce297 | 145 | debounce.reset(); |
ipoljak1 | 0:f5a0afbce297 | 146 | } |
ipoljak1 | 0:f5a0afbce297 | 147 | flag = true; |
ipoljak1 | 0:f5a0afbce297 | 148 | } |
ipoljak1 | 0:f5a0afbce297 | 149 | break; |
ipoljak1 | 0:f5a0afbce297 | 150 | |
ipoljak1 | 0:f5a0afbce297 | 151 | } |
ipoljak1 | 0:f5a0afbce297 | 152 | |
ipoljak1 | 0:f5a0afbce297 | 153 | |
ipoljak1 | 0:f5a0afbce297 | 154 | |
ipoljak1 | 0:f5a0afbce297 | 155 | |
ipoljak1 | 0:f5a0afbce297 | 156 | } |
ipoljak1 | 0:f5a0afbce297 | 157 | } |
ipoljak1 | 0:f5a0afbce297 | 158 | |
ipoljak1 | 0:f5a0afbce297 | 159 | void toggle() |
ipoljak1 | 0:f5a0afbce297 | 160 | { |
ipoljak1 | 0:f5a0afbce297 | 161 | |
ipoljak1 | 0:f5a0afbce297 | 162 | if (debounce.read_ms()>200) { |
ipoljak1 | 0:f5a0afbce297 | 163 | |
ipoljak1 | 0:f5a0afbce297 | 164 | speaker1.start(); |
ipoljak1 | 0:f5a0afbce297 | 165 | ledgreen = 1; |
ipoljak1 | 0:f5a0afbce297 | 166 | ledred = 1; |
ipoljak1 | 0:f5a0afbce297 | 167 | ledyellow = 1; |
ipoljak1 | 0:f5a0afbce297 | 168 | task1(); |
ipoljak1 | 0:f5a0afbce297 | 169 | |
ipoljak1 | 0:f5a0afbce297 | 170 | } |
ipoljak1 | 0:f5a0afbce297 | 171 | debounce.reset(); |
ipoljak1 | 0:f5a0afbce297 | 172 | } |
ipoljak1 | 0:f5a0afbce297 | 173 | |
ipoljak1 | 0:f5a0afbce297 | 174 | void task1() // task function |
ipoljak1 | 0:f5a0afbce297 | 175 | { |
ipoljak1 | 0:f5a0afbce297 | 176 | speaker.period(1/500.0); // set PWM period |
ipoljak1 | 0:f5a0afbce297 | 177 | speaker=0.5; // set duty cycle |
ipoljak1 | 0:f5a0afbce297 | 178 | } |