De stoplicht is als object in een class gedefineerd en wordt 2 keer aangemaakt.
Dependencies: mbed
main.cpp@0:2bd43f5c8891, 2018-03-17 (annotated)
- Committer:
- lennartgroen
- Date:
- Sat Mar 17 15:59:32 2018 +0000
- Revision:
- 0:2bd43f5c8891
First release;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lennartgroen | 0:2bd43f5c8891 | 1 | /* |
lennartgroen | 0:2bd43f5c8891 | 2 | Namen: Lennart Groen |
lennartgroen | 0:2bd43f5c8891 | 3 | Klas: EMT1PSa |
lennartgroen | 0:2bd43f5c8891 | 4 | Opdracht: MBED - Stoplicht |
lennartgroen | 0:2bd43f5c8891 | 5 | Samengewerkt met: ... |
lennartgroen | 0:2bd43f5c8891 | 6 | Datum: 17-03-2018 |
lennartgroen | 0:2bd43f5c8891 | 7 | Inspiratie uit: https://os.mbed.com/users/4180_1/notebook/rgb-leds/ |
lennartgroen | 0:2bd43f5c8891 | 8 | */ |
lennartgroen | 0:2bd43f5c8891 | 9 | |
lennartgroen | 0:2bd43f5c8891 | 10 | #include <mbed.h> |
lennartgroen | 0:2bd43f5c8891 | 11 | |
lennartgroen | 0:2bd43f5c8891 | 12 | struct lichtkleur{ // Structure for |
lennartgroen | 0:2bd43f5c8891 | 13 | float rood, groen, blauw; |
lennartgroen | 0:2bd43f5c8891 | 14 | }; |
lennartgroen | 0:2bd43f5c8891 | 15 | |
lennartgroen | 0:2bd43f5c8891 | 16 | // Programmed outside class because mbed won't accept it |
lennartgroen | 0:2bd43f5c8891 | 17 | const lichtkleur rood = { 0.0,1.0,1.0}; |
lennartgroen | 0:2bd43f5c8891 | 18 | const lichtkleur geel = { 0.5,0.0,1.0}; |
lennartgroen | 0:2bd43f5c8891 | 19 | const lichtkleur groen = { 1.0,0.0,1.0}; |
lennartgroen | 0:2bd43f5c8891 | 20 | |
lennartgroen | 0:2bd43f5c8891 | 21 | |
lennartgroen | 0:2bd43f5c8891 | 22 | class stoplicht{ |
lennartgroen | 0:2bd43f5c8891 | 23 | public: |
lennartgroen | 0:2bd43f5c8891 | 24 | stoplicht(PinName p_rood, PinName p_groen, PinName p_blauw); |
lennartgroen | 0:2bd43f5c8891 | 25 | void set_rood(); |
lennartgroen | 0:2bd43f5c8891 | 26 | void set_geel(); |
lennartgroen | 0:2bd43f5c8891 | 27 | void set_groen(); |
lennartgroen | 0:2bd43f5c8891 | 28 | private: |
lennartgroen | 0:2bd43f5c8891 | 29 | PwmOut pin_rood; |
lennartgroen | 0:2bd43f5c8891 | 30 | PwmOut pin_groen; |
lennartgroen | 0:2bd43f5c8891 | 31 | PwmOut pin_blauw; |
lennartgroen | 0:2bd43f5c8891 | 32 | }; |
lennartgroen | 0:2bd43f5c8891 | 33 | |
lennartgroen | 0:2bd43f5c8891 | 34 | int main() |
lennartgroen | 0:2bd43f5c8891 | 35 | { |
lennartgroen | 0:2bd43f5c8891 | 36 | stoplicht stoplicht_nz(D11, D10, D9); |
lennartgroen | 0:2bd43f5c8891 | 37 | stoplicht stoplicht_ow(D6, D5, D3); |
lennartgroen | 0:2bd43f5c8891 | 38 | Timer timer1; |
lennartgroen | 0:2bd43f5c8891 | 39 | timer1.start(); |
lennartgroen | 0:2bd43f5c8891 | 40 | int state = 0; |
lennartgroen | 0:2bd43f5c8891 | 41 | |
lennartgroen | 0:2bd43f5c8891 | 42 | while(1){ |
lennartgroen | 0:2bd43f5c8891 | 43 | |
lennartgroen | 0:2bd43f5c8891 | 44 | switch(state){ |
lennartgroen | 0:2bd43f5c8891 | 45 | case 0: |
lennartgroen | 0:2bd43f5c8891 | 46 | stoplicht_nz.set_groen(); |
lennartgroen | 0:2bd43f5c8891 | 47 | break; |
lennartgroen | 0:2bd43f5c8891 | 48 | case 1: |
lennartgroen | 0:2bd43f5c8891 | 49 | stoplicht_ow.set_groen(); |
lennartgroen | 0:2bd43f5c8891 | 50 | break; |
lennartgroen | 0:2bd43f5c8891 | 51 | } |
lennartgroen | 0:2bd43f5c8891 | 52 | |
lennartgroen | 0:2bd43f5c8891 | 53 | |
lennartgroen | 0:2bd43f5c8891 | 54 | if(timer1.read() > 5){ |
lennartgroen | 0:2bd43f5c8891 | 55 | switch(state){ |
lennartgroen | 0:2bd43f5c8891 | 56 | case 0: |
lennartgroen | 0:2bd43f5c8891 | 57 | stoplicht_nz.set_rood(); |
lennartgroen | 0:2bd43f5c8891 | 58 | wait(1); |
lennartgroen | 0:2bd43f5c8891 | 59 | state = 1; |
lennartgroen | 0:2bd43f5c8891 | 60 | break; |
lennartgroen | 0:2bd43f5c8891 | 61 | case 1: |
lennartgroen | 0:2bd43f5c8891 | 62 | stoplicht_ow.set_rood(); |
lennartgroen | 0:2bd43f5c8891 | 63 | wait(1); |
lennartgroen | 0:2bd43f5c8891 | 64 | state = 0; |
lennartgroen | 0:2bd43f5c8891 | 65 | break; |
lennartgroen | 0:2bd43f5c8891 | 66 | } |
lennartgroen | 0:2bd43f5c8891 | 67 | timer1.reset(); |
lennartgroen | 0:2bd43f5c8891 | 68 | } |
lennartgroen | 0:2bd43f5c8891 | 69 | } |
lennartgroen | 0:2bd43f5c8891 | 70 | } |
lennartgroen | 0:2bd43f5c8891 | 71 | |
lennartgroen | 0:2bd43f5c8891 | 72 | stoplicht::stoplicht(PinName p_rood, PinName p_groen, PinName p_blauw) |
lennartgroen | 0:2bd43f5c8891 | 73 | :pin_rood(p_rood), pin_groen(p_groen), pin_blauw(p_blauw) |
lennartgroen | 0:2bd43f5c8891 | 74 | { |
lennartgroen | 0:2bd43f5c8891 | 75 | |
lennartgroen | 0:2bd43f5c8891 | 76 | } |
lennartgroen | 0:2bd43f5c8891 | 77 | |
lennartgroen | 0:2bd43f5c8891 | 78 | // Functies voor stoplicht class // |
lennartgroen | 0:2bd43f5c8891 | 79 | void stoplicht::set_groen() |
lennartgroen | 0:2bd43f5c8891 | 80 | { |
lennartgroen | 0:2bd43f5c8891 | 81 | pin_rood = groen.rood; |
lennartgroen | 0:2bd43f5c8891 | 82 | pin_groen = groen.groen; |
lennartgroen | 0:2bd43f5c8891 | 83 | pin_blauw = groen.blauw; |
lennartgroen | 0:2bd43f5c8891 | 84 | } |
lennartgroen | 0:2bd43f5c8891 | 85 | |
lennartgroen | 0:2bd43f5c8891 | 86 | |
lennartgroen | 0:2bd43f5c8891 | 87 | void stoplicht::set_geel() |
lennartgroen | 0:2bd43f5c8891 | 88 | { |
lennartgroen | 0:2bd43f5c8891 | 89 | pin_rood = geel.rood; |
lennartgroen | 0:2bd43f5c8891 | 90 | pin_groen = geel.groen; |
lennartgroen | 0:2bd43f5c8891 | 91 | pin_blauw = geel.blauw; |
lennartgroen | 0:2bd43f5c8891 | 92 | } |
lennartgroen | 0:2bd43f5c8891 | 93 | |
lennartgroen | 0:2bd43f5c8891 | 94 | |
lennartgroen | 0:2bd43f5c8891 | 95 | void stoplicht::set_rood() |
lennartgroen | 0:2bd43f5c8891 | 96 | { |
lennartgroen | 0:2bd43f5c8891 | 97 | pin_rood = geel.rood; |
lennartgroen | 0:2bd43f5c8891 | 98 | pin_groen = geel.groen; |
lennartgroen | 0:2bd43f5c8891 | 99 | pin_blauw = geel.blauw; |
lennartgroen | 0:2bd43f5c8891 | 100 | wait(1); |
lennartgroen | 0:2bd43f5c8891 | 101 | pin_rood = rood.rood; |
lennartgroen | 0:2bd43f5c8891 | 102 | pin_groen = rood.groen; |
lennartgroen | 0:2bd43f5c8891 | 103 | pin_blauw = rood.blauw; |
lennartgroen | 0:2bd43f5c8891 | 104 | } |
lennartgroen | 0:2bd43f5c8891 | 105 |