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.
main.cpp@40:9acc1341456a, 2018-06-14 (annotated)
- Committer:
- carlosperales95
- Date:
- Thu Jun 14 13:19:52 2018 +0000
- Revision:
- 40:9acc1341456a
- Parent:
- 39:9cce8f2c50b6
- Child:
- 41:4fa6aa29d1ed
mergepush finally?
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mglmx | 0:4d06a6a8e785 | 1 | #include "mbed.h" |
mglmx | 3:fe7010b693a0 | 2 | #include "TextLCD.h" |
mglmx | 18:aa43bb62e60f | 3 | #include "MCP23017.h" |
mglmx | 22:e4153ca757dd | 4 | #include <string> |
mglmx | 22:e4153ca757dd | 5 | #include <iostream> |
mglmx | 35:cfcfeccb959e | 6 | #include <vector> |
mglmx | 35:cfcfeccb959e | 7 | |
mglmx | 35:cfcfeccb959e | 8 | using namespace std; |
mglmx | 18:aa43bb62e60f | 9 | |
mglmx | 1:0ab26889af9b | 10 | |
carlosperales95 | 7:e2b8461d4f05 | 11 | /******PINS AND DECLARATIONS*******/ |
carlosperales95 | 7:e2b8461d4f05 | 12 | |
carlosperales95 | 25:a42a1ed4d8e9 | 13 | //------PINS |
mglmx | 21:e6f1649add39 | 14 | |
mglmx | 21:e6f1649add39 | 15 | //SWITCHES p5 - p8 |
mglmx | 21:e6f1649add39 | 16 | DigitalIn switch1(p5); |
mglmx | 21:e6f1649add39 | 17 | DigitalIn switch2(p6); |
mglmx | 21:e6f1649add39 | 18 | DigitalIn switch3(p7); |
mglmx | 21:e6f1649add39 | 19 | DigitalIn switch4(p8); |
carlosperales95 | 7:e2b8461d4f05 | 20 | |
carlosperales95 | 10:2088b1935a93 | 21 | //RAIL SENSORS - INT0,INT1 |
carlosperales95 | 7:e2b8461d4f05 | 22 | //INT0 - p9 |
mglmx | 18:aa43bb62e60f | 23 | InterruptIn int0(p9); |
carlosperales95 | 7:e2b8461d4f05 | 24 | //INT1 - p10 |
mglmx | 18:aa43bb62e60f | 25 | InterruptIn int1(p10); |
carlosperales95 | 7:e2b8461d4f05 | 26 | |
carlosperales95 | 7:e2b8461d4f05 | 27 | ///p11 |
carlosperales95 | 7:e2b8461d4f05 | 28 | ///p12 |
carlosperales95 | 12:e914ca5cd44b | 29 | |
carlosperales95 | 7:e2b8461d4f05 | 30 | //M0 - p13 |
mglmx | 35:cfcfeccb959e | 31 | DigitalIn d21stat(p13); //Sensor right of the station |
carlosperales95 | 7:e2b8461d4f05 | 32 | //M1 - p14 |
mglmx | 35:cfcfeccb959e | 33 | DigitalIn d22stat(p14); //Sensor left of the station |
carlosperales95 | 7:e2b8461d4f05 | 34 | //M2 - p15 |
carlosperales95 | 29:559eb2164488 | 35 | DigitalIn station(p15); //Sensor in the middle of the station |
carlosperales95 | 12:e914ca5cd44b | 36 | |
carlosperales95 | 7:e2b8461d4f05 | 37 | //p16 |
carlosperales95 | 12:e914ca5cd44b | 38 | //p17 |
carlosperales95 | 12:e914ca5cd44b | 39 | |
carlosperales95 | 11:021210c59a95 | 40 | //BUZZER - p18 |
carlosperales95 | 11:021210c59a95 | 41 | DigitalOut buzz(p18); // buzz=0 doesn't beep, buzz=1 beeps |
carlosperales95 | 7:e2b8461d4f05 | 42 | |
carlosperales95 | 7:e2b8461d4f05 | 43 | //POTENTIOMETER - p19 |
carlosperales95 | 13:dbf1ead12cee | 44 | AnalogIn pot(p19); //Gives float value pot.read(). Convert analog input to V with f*3.3 |
carlosperales95 | 7:e2b8461d4f05 | 45 | |
carlosperales95 | 7:e2b8461d4f05 | 46 | //DAT - p20 |
mglmx | 3:fe7010b693a0 | 47 | DigitalOut Track(p20); //Digital output bit used to drive track power via H-bridge |
carlosperales95 | 7:e2b8461d4f05 | 48 | |
carlosperales95 | 7:e2b8461d4f05 | 49 | //LCD SCREEN - p21, p22, p23, p24, p25, p26 |
carlosperales95 | 11:021210c59a95 | 50 | TextLCD lcd(p22,p21,p23,p24,p25,p26); // RS, E, A4, A5, A6, A7 // ldc.cls() to clear and printf(String up to 16char) |
carlosperales95 | 7:e2b8461d4f05 | 51 | |
carlosperales95 | 7:e2b8461d4f05 | 52 | ///p27 |
carlosperales95 | 7:e2b8461d4f05 | 53 | ///p28 |
mglmx | 33:24ce12dec157 | 54 | I2C i2c(p28,p27); |
carlosperales95 | 7:e2b8461d4f05 | 55 | |
carlosperales95 | 7:e2b8461d4f05 | 56 | //LED1 - p29 |
mglmx | 3:fe7010b693a0 | 57 | DigitalOut redled(p29); |
carlosperales95 | 7:e2b8461d4f05 | 58 | //LED2 - p30 |
mglmx | 3:fe7010b693a0 | 59 | DigitalOut greenled(p30); |
carlosperales95 | 7:e2b8461d4f05 | 60 | |
carlosperales95 | 24:1d71dd8778c4 | 61 | //MBED LEDS |
mglmx | 18:aa43bb62e60f | 62 | DigitalOut led1(LED1); |
mglmx | 18:aa43bb62e60f | 63 | DigitalOut led2(LED2); |
mglmx | 18:aa43bb62e60f | 64 | DigitalOut led3(LED3); |
mglmx | 16:2a2da0e67793 | 65 | |
mglmx | 16:2a2da0e67793 | 66 | //MCP |
mglmx | 16:2a2da0e67793 | 67 | MCP23017 *mcp; |
carlosperales95 | 12:e914ca5cd44b | 68 | |
carlosperales95 | 24:1d71dd8778c4 | 69 | |
carlosperales95 | 25:a42a1ed4d8e9 | 70 | //------GLOBAL VARS |
carlosperales95 | 25:a42a1ed4d8e9 | 71 | |
carlosperales95 | 38:b9aba3715682 | 72 | //......SENSOR POSITION VARS |
carlosperales95 | 38:b9aba3715682 | 73 | |
mglmx | 34:c9ab2a987734 | 74 | #define D0 0 |
mglmx | 34:c9ab2a987734 | 75 | #define D1 1 |
mglmx | 34:c9ab2a987734 | 76 | #define D2 2 |
mglmx | 34:c9ab2a987734 | 77 | #define D3 3 |
mglmx | 34:c9ab2a987734 | 78 | #define D4 4 |
mglmx | 34:c9ab2a987734 | 79 | #define D5 5 |
mglmx | 34:c9ab2a987734 | 80 | #define D6 6 |
mglmx | 34:c9ab2a987734 | 81 | #define D7 7 |
mglmx | 34:c9ab2a987734 | 82 | #define D8 8 |
mglmx | 34:c9ab2a987734 | 83 | #define D9 9 |
mglmx | 34:c9ab2a987734 | 84 | #define D10 10 |
mglmx | 34:c9ab2a987734 | 85 | #define D11 11 |
mglmx | 34:c9ab2a987734 | 86 | #define D12 12 |
mglmx | 34:c9ab2a987734 | 87 | #define D13 13 |
mglmx | 34:c9ab2a987734 | 88 | #define D21 14 |
mglmx | 34:c9ab2a987734 | 89 | #define D22 15 |
mglmx | 34:c9ab2a987734 | 90 | |
carlosperales95 | 38:b9aba3715682 | 91 | /** |
carlosperales95 | 38:b9aba3715682 | 92 | * |
carlosperales95 | 38:b9aba3715682 | 93 | *Position class. |
carlosperales95 | 38:b9aba3715682 | 94 | * |
carlosperales95 | 38:b9aba3715682 | 95 | *@position - |
carlosperales95 | 38:b9aba3715682 | 96 | *@previous_cw - |
carlosperales95 | 38:b9aba3715682 | 97 | *@previous_ccw - |
carlosperales95 | 38:b9aba3715682 | 98 | * |
carlosperales95 | 38:b9aba3715682 | 99 | *Position(int) - |
carlosperales95 | 38:b9aba3715682 | 100 | * |
carlosperales95 | 38:b9aba3715682 | 101 | *get_pos() - |
carlosperales95 | 38:b9aba3715682 | 102 | *get_prev_cw() - |
carlosperales95 | 38:b9aba3715682 | 103 | *get_ccw() - |
carlosperales95 | 38:b9aba3715682 | 104 | *add_prev_cw() - |
carlosperales95 | 38:b9aba3715682 | 105 | *add_ccw() - |
carlosperales95 | 38:b9aba3715682 | 106 | * |
carlosperales95 | 38:b9aba3715682 | 107 | **/ |
mglmx | 35:cfcfeccb959e | 108 | class Position{ |
mglmx | 35:cfcfeccb959e | 109 | private: |
mglmx | 35:cfcfeccb959e | 110 | int position; |
mglmx | 35:cfcfeccb959e | 111 | vector <int> previous_cw; |
mglmx | 35:cfcfeccb959e | 112 | vector <int> previous_ccw; |
mglmx | 35:cfcfeccb959e | 113 | public: |
mglmx | 35:cfcfeccb959e | 114 | Position(int p){ |
mglmx | 35:cfcfeccb959e | 115 | position = p; |
mglmx | 35:cfcfeccb959e | 116 | } |
mglmx | 36:9428c72bdd58 | 117 | |
mglmx | 36:9428c72bdd58 | 118 | int get_pos(){ |
mglmx | 36:9428c72bdd58 | 119 | return position; |
mglmx | 36:9428c72bdd58 | 120 | } |
carlosperales95 | 25:a42a1ed4d8e9 | 121 | |
mglmx | 35:cfcfeccb959e | 122 | vector <int> get_prev_cw(){ |
mglmx | 35:cfcfeccb959e | 123 | return previous_cw; |
mglmx | 35:cfcfeccb959e | 124 | } |
mglmx | 35:cfcfeccb959e | 125 | |
mglmx | 35:cfcfeccb959e | 126 | vector <int> get_prev_ccw(){ |
mglmx | 35:cfcfeccb959e | 127 | return previous_ccw; |
mglmx | 35:cfcfeccb959e | 128 | } |
mglmx | 35:cfcfeccb959e | 129 | |
mglmx | 35:cfcfeccb959e | 130 | void add_prev_cw(int pos){ |
mglmx | 35:cfcfeccb959e | 131 | previous_cw.push_back(pos); |
mglmx | 35:cfcfeccb959e | 132 | }; |
mglmx | 35:cfcfeccb959e | 133 | |
mglmx | 35:cfcfeccb959e | 134 | void add_prev_ccw(int pos){ |
mglmx | 35:cfcfeccb959e | 135 | previous_ccw.push_back(pos); |
mglmx | 35:cfcfeccb959e | 136 | }; |
mglmx | 35:cfcfeccb959e | 137 | }; |
mglmx | 35:cfcfeccb959e | 138 | |
carlosperales95 | 38:b9aba3715682 | 139 | //Creation of all the positions. One for every sensor on the table - Position name(mapping) |
carlosperales95 | 38:b9aba3715682 | 140 | |
mglmx | 35:cfcfeccb959e | 141 | Position d0(D0); |
mglmx | 35:cfcfeccb959e | 142 | Position d1(D1); |
mglmx | 35:cfcfeccb959e | 143 | Position d2(D2); |
mglmx | 35:cfcfeccb959e | 144 | Position d3(D3); |
mglmx | 35:cfcfeccb959e | 145 | Position d4(D4); |
mglmx | 35:cfcfeccb959e | 146 | Position d5(D5); |
mglmx | 35:cfcfeccb959e | 147 | Position d6(D6); |
mglmx | 35:cfcfeccb959e | 148 | Position d7(D7); |
mglmx | 35:cfcfeccb959e | 149 | Position d8(D8); |
mglmx | 35:cfcfeccb959e | 150 | Position d9(D9); |
mglmx | 35:cfcfeccb959e | 151 | Position d10(D10); |
mglmx | 35:cfcfeccb959e | 152 | Position d11(D11); |
mglmx | 35:cfcfeccb959e | 153 | Position d12(D12); |
mglmx | 35:cfcfeccb959e | 154 | Position d13(D13); |
mglmx | 35:cfcfeccb959e | 155 | Position d21(D21); |
mglmx | 35:cfcfeccb959e | 156 | Position d22(D22); |
mglmx | 35:cfcfeccb959e | 157 | |
carlosperales95 | 38:b9aba3715682 | 158 | //Creating a vector with all the positions. |
mglmx | 35:cfcfeccb959e | 159 | vector<Position> positions; |
mglmx | 35:cfcfeccb959e | 160 | |
carlosperales95 | 38:b9aba3715682 | 161 | |
carlosperales95 | 25:a42a1ed4d8e9 | 162 | //.....DCC TRAIN COMMAND VARS |
carlosperales95 | 25:a42a1ed4d8e9 | 163 | |
carlosperales95 | 25:a42a1ed4d8e9 | 164 | //typical out of box default engine DCC address is 3 (at least for Bachmann trains) |
carlosperales95 | 25:a42a1ed4d8e9 | 165 | //Note: A DCC controller can reprogram the address whenever needed |
mglmx | 32:e5b732fb8e65 | 166 | const unsigned int DCCaddressDR = 0x01; //Address for train 1 DARK-RED |
mglmx | 32:e5b732fb8e65 | 167 | const unsigned int DCCaddressLR = 0x03; //Address for train 3 LIGHT-RED |
carlosperales95 | 25:a42a1ed4d8e9 | 168 | |
carlosperales95 | 25:a42a1ed4d8e9 | 169 | //01DCSSSS for speed, D is direction (fwd=1 and rev=0), C is speed(SSSSC) LSB |
carlosperales95 | 25:a42a1ed4d8e9 | 170 | const unsigned int DCCinst_forward = 0x68; //forward half speed |
mglmx | 33:24ce12dec157 | 171 | const unsigned int DCCinst_forward_slow = 0x66; //forward half speed |
carlosperales95 | 25:a42a1ed4d8e9 | 172 | const unsigned int DCCinst_reverse = 0x48; //reverse half speed |
carlosperales95 | 25:a42a1ed4d8e9 | 173 | const unsigned int DCCinst_stop = 0x50; //stop the train |
carlosperales95 | 25:a42a1ed4d8e9 | 174 | |
carlosperales95 | 25:a42a1ed4d8e9 | 175 | //100DDDDD for basic headlight functions |
carlosperales95 | 25:a42a1ed4d8e9 | 176 | const unsigned int DCC_func_lighton = 0x90; //F0 turns on headlight function |
carlosperales95 | 25:a42a1ed4d8e9 | 177 | const unsigned int DCC_func_dimlight = 0x91; //F0 + F1 dims headlight |
carlosperales95 | 25:a42a1ed4d8e9 | 178 | |
carlosperales95 | 25:a42a1ed4d8e9 | 179 | |
carlosperales95 | 25:a42a1ed4d8e9 | 180 | //.....SWITCH COMMAND VARS |
carlosperales95 | 25:a42a1ed4d8e9 | 181 | |
carlosperales95 | 25:a42a1ed4d8e9 | 182 | const unsigned int SWBaddress = 0x06; //Address for switch box |
carlosperales95 | 25:a42a1ed4d8e9 | 183 | |
carlosperales95 | 25:a42a1ed4d8e9 | 184 | //100DDDDD where DDDDD is the switch command and 100 is constant: |
carlosperales95 | 25:a42a1ed4d8e9 | 185 | |
carlosperales95 | 25:a42a1ed4d8e9 | 186 | //00001(F1 active)-00010(F2 active)-00100(F3 active)-01000(F4 active) |
carlosperales95 | 25:a42a1ed4d8e9 | 187 | //Example - 111111 0 00000101 0 10000000 0 10000101 1 - idle |
carlosperales95 | 25:a42a1ed4d8e9 | 188 | const unsigned int SWBidle = 0x80; //IDLE - Flip last activated SW. |
carlosperales95 | 25:a42a1ed4d8e9 | 189 | const unsigned int SWBflip_1 = 0x81; //Flip SW1 |
carlosperales95 | 25:a42a1ed4d8e9 | 190 | const unsigned int SWBflip_2 = 0x82; //Flip SW2 |
carlosperales95 | 25:a42a1ed4d8e9 | 191 | const unsigned int SWBflip_3 = 0x84; //Flip SW3 |
carlosperales95 | 25:a42a1ed4d8e9 | 192 | const unsigned int SWBflip_4 = 0x88; //Flip SW4 |
carlosperales95 | 25:a42a1ed4d8e9 | 193 | |
carlosperales95 | 40:9acc1341456a | 194 | |
mglmx | 35:cfcfeccb959e | 195 | //Starting position and orientation of the trains |
mglmx | 35:cfcfeccb959e | 196 | int DR_train_pos = D4; |
mglmx | 35:cfcfeccb959e | 197 | bool DR_cw = true; |
mglmx | 35:cfcfeccb959e | 198 | |
mglmx | 35:cfcfeccb959e | 199 | int LR_train_pos= D10; |
mglmx | 35:cfcfeccb959e | 200 | bool LR_cw = true; |
mglmx | 35:cfcfeccb959e | 201 | |
mglmx | 18:aa43bb62e60f | 202 | |
carlosperales95 | 38:b9aba3715682 | 203 | |
carlosperales95 | 38:b9aba3715682 | 204 | //**************** FUNCTIONS FOR DENVER TRAIN ****************// |
carlosperales95 | 38:b9aba3715682 | 205 | |
carlosperales95 | 38:b9aba3715682 | 206 | |
mglmx | 22:e4153ca757dd | 207 | /** |
carlosperales95 | 25:a42a1ed4d8e9 | 208 | *Activates the buzzer for 0.5 seconds. |
mglmx | 22:e4153ca757dd | 209 | **/ |
mglmx | 22:e4153ca757dd | 210 | void doBuzz(){ |
carlosperales95 | 25:a42a1ed4d8e9 | 211 | |
mglmx | 22:e4153ca757dd | 212 | buzz = 1; |
mglmx | 22:e4153ca757dd | 213 | wait(0.5); |
mglmx | 22:e4153ca757dd | 214 | buzz = 0; |
mglmx | 22:e4153ca757dd | 215 | } |
mglmx | 18:aa43bb62e60f | 216 | |
mglmx | 35:cfcfeccb959e | 217 | void init_positions(){ |
mglmx | 36:9428c72bdd58 | 218 | |
mglmx | 35:cfcfeccb959e | 219 | |
mglmx | 35:cfcfeccb959e | 220 | d0.add_prev_cw(D1); |
mglmx | 35:cfcfeccb959e | 221 | d0.add_prev_ccw(D13); |
mglmx | 35:cfcfeccb959e | 222 | |
mglmx | 35:cfcfeccb959e | 223 | d1.add_prev_cw(D22); |
mglmx | 35:cfcfeccb959e | 224 | d1.add_prev_ccw(D0); |
mglmx | 35:cfcfeccb959e | 225 | |
mglmx | 35:cfcfeccb959e | 226 | d22.add_prev_cw(D2); |
mglmx | 35:cfcfeccb959e | 227 | d22.add_prev_ccw(D1); |
mglmx | 35:cfcfeccb959e | 228 | |
mglmx | 35:cfcfeccb959e | 229 | d2.add_prev_cw(D21); |
mglmx | 35:cfcfeccb959e | 230 | d2.add_prev_ccw(D22); |
mglmx | 35:cfcfeccb959e | 231 | |
mglmx | 35:cfcfeccb959e | 232 | d21.add_prev_cw(D3); |
mglmx | 35:cfcfeccb959e | 233 | d21.add_prev_cw(D4); |
mglmx | 35:cfcfeccb959e | 234 | d21.add_prev_ccw(D2); |
mglmx | 35:cfcfeccb959e | 235 | |
mglmx | 35:cfcfeccb959e | 236 | d3.add_prev_cw(D9); |
mglmx | 35:cfcfeccb959e | 237 | d3.add_prev_ccw(D21); |
mglmx | 35:cfcfeccb959e | 238 | |
mglmx | 35:cfcfeccb959e | 239 | d4.add_prev_cw(D6); |
mglmx | 35:cfcfeccb959e | 240 | d4.add_prev_ccw(D21); |
mglmx | 35:cfcfeccb959e | 241 | |
mglmx | 35:cfcfeccb959e | 242 | d5.add_prev_cw(D6); |
mglmx | 35:cfcfeccb959e | 243 | d5.add_prev_ccw(D11); |
mglmx | 35:cfcfeccb959e | 244 | |
mglmx | 35:cfcfeccb959e | 245 | d6.add_prev_cw(D7); |
mglmx | 35:cfcfeccb959e | 246 | d6.add_prev_ccw(D4); |
mglmx | 35:cfcfeccb959e | 247 | d6.add_prev_ccw(D5); |
mglmx | 35:cfcfeccb959e | 248 | |
mglmx | 35:cfcfeccb959e | 249 | d7.add_prev_cw(D8); |
mglmx | 35:cfcfeccb959e | 250 | d7.add_prev_ccw(D6); |
mglmx | 35:cfcfeccb959e | 251 | |
mglmx | 36:9428c72bdd58 | 252 | d8.add_prev_cw(D9); |
mglmx | 36:9428c72bdd58 | 253 | d8.add_prev_cw(D10); |
mglmx | 36:9428c72bdd58 | 254 | d8.add_prev_ccw(D7); |
mglmx | 36:9428c72bdd58 | 255 | |
mglmx | 35:cfcfeccb959e | 256 | d9.add_prev_cw(D3); |
mglmx | 35:cfcfeccb959e | 257 | d9.add_prev_ccw(D8); |
mglmx | 35:cfcfeccb959e | 258 | |
mglmx | 35:cfcfeccb959e | 259 | d10.add_prev_cw(D12); |
mglmx | 35:cfcfeccb959e | 260 | d10.add_prev_ccw(D8); |
mglmx | 35:cfcfeccb959e | 261 | |
mglmx | 35:cfcfeccb959e | 262 | d11.add_prev_cw(D12); |
mglmx | 35:cfcfeccb959e | 263 | d11.add_prev_ccw(D5); |
mglmx | 35:cfcfeccb959e | 264 | |
mglmx | 35:cfcfeccb959e | 265 | d12.add_prev_cw(D13); |
mglmx | 35:cfcfeccb959e | 266 | d12.add_prev_ccw(D10); |
mglmx | 35:cfcfeccb959e | 267 | d12.add_prev_ccw(D11); |
mglmx | 35:cfcfeccb959e | 268 | |
mglmx | 35:cfcfeccb959e | 269 | d13.add_prev_cw(D0); |
mglmx | 35:cfcfeccb959e | 270 | d13.add_prev_ccw(D12); |
mglmx | 36:9428c72bdd58 | 271 | |
mglmx | 36:9428c72bdd58 | 272 | //Initialize array with positions |
mglmx | 36:9428c72bdd58 | 273 | positions.push_back(d0); |
mglmx | 36:9428c72bdd58 | 274 | positions.push_back(d1); |
mglmx | 36:9428c72bdd58 | 275 | positions.push_back(d2); |
mglmx | 36:9428c72bdd58 | 276 | positions.push_back(d3); |
mglmx | 36:9428c72bdd58 | 277 | positions.push_back(d4); |
mglmx | 36:9428c72bdd58 | 278 | positions.push_back(d5); |
mglmx | 36:9428c72bdd58 | 279 | positions.push_back(d6); |
mglmx | 36:9428c72bdd58 | 280 | positions.push_back(d7); |
mglmx | 36:9428c72bdd58 | 281 | positions.push_back(d8); |
mglmx | 36:9428c72bdd58 | 282 | positions.push_back(d9); |
mglmx | 36:9428c72bdd58 | 283 | positions.push_back(d10); |
mglmx | 36:9428c72bdd58 | 284 | positions.push_back(d11); |
mglmx | 36:9428c72bdd58 | 285 | positions.push_back(d12); |
mglmx | 36:9428c72bdd58 | 286 | positions.push_back(d13); |
mglmx | 36:9428c72bdd58 | 287 | positions.push_back(d21); |
mglmx | 36:9428c72bdd58 | 288 | positions.push_back(d22); |
mglmx | 35:cfcfeccb959e | 289 | } |
carlosperales95 | 24:1d71dd8778c4 | 290 | |
mglmx | 22:e4153ca757dd | 291 | /** |
carlosperales95 | 24:1d71dd8778c4 | 292 | * |
carlosperales95 | 28:71bd4c83c05f | 293 | *Here we initialize the mcp that will be used to manage the interrupts. |
carlosperales95 | 25:a42a1ed4d8e9 | 294 | * |
carlosperales95 | 24:1d71dd8778c4 | 295 | **/ |
mglmx | 18:aa43bb62e60f | 296 | void initialize_mcp(){ |
mglmx | 33:24ce12dec157 | 297 | mcp = new MCP23017(i2c,0x40); //Connect to SCL - p28 and SDA - p27 and MPC I2C address 0x40 |
mglmx | 18:aa43bb62e60f | 298 | |
mglmx | 33:24ce12dec157 | 299 | mcp->_write(IODIRA, (unsigned char )0xff); |
mglmx | 33:24ce12dec157 | 300 | mcp->_write(IODIRB, (unsigned char )0xff); |
mglmx | 33:24ce12dec157 | 301 | mcp->_write(IPOLA, (unsigned char )0x00); |
mglmx | 33:24ce12dec157 | 302 | mcp->_write(IPOLB, (unsigned char )0x00); |
mglmx | 33:24ce12dec157 | 303 | mcp->_write(DEFVALA, (unsigned char )0xff); |
mglmx | 33:24ce12dec157 | 304 | mcp->_write(DEFVALB, (unsigned char )0xff); |
mglmx | 33:24ce12dec157 | 305 | mcp->_write(INTCONA, (unsigned char )0xff); |
mglmx | 33:24ce12dec157 | 306 | mcp->_write(INTCONB, (unsigned char )0xff); |
mglmx | 33:24ce12dec157 | 307 | mcp->_write(IOCONA, (unsigned char )0x2); |
mglmx | 33:24ce12dec157 | 308 | mcp->_write(IOCONB, (unsigned char )0x2); |
mglmx | 33:24ce12dec157 | 309 | mcp->_write(GPPUA, (unsigned char )0xff); |
mglmx | 33:24ce12dec157 | 310 | mcp->_write(GPPUB, (unsigned char )0xff); |
mglmx | 33:24ce12dec157 | 311 | |
mglmx | 18:aa43bb62e60f | 312 | } |
mglmx | 18:aa43bb62e60f | 313 | |
carlosperales95 | 24:1d71dd8778c4 | 314 | /** |
carlosperales95 | 24:1d71dd8778c4 | 315 | * |
carlosperales95 | 37:bb15bea420a3 | 316 | *Returns the number of the sensor where the train was detected. |
carlosperales95 | 37:bb15bea420a3 | 317 | * |
carlosperales95 | 37:bb15bea420a3 | 318 | **/ |
mglmx | 34:c9ab2a987734 | 319 | int get_sensor(unsigned int number,int interrupt){ |
mglmx | 34:c9ab2a987734 | 320 | int sensor = -1; |
mglmx | 34:c9ab2a987734 | 321 | |
carlosperales95 | 37:bb15bea420a3 | 322 | for(int i=0; i<8; i++){ |
mglmx | 34:c9ab2a987734 | 323 | |
mglmx | 34:c9ab2a987734 | 324 | if(~number & 1<<i){ |
mglmx | 34:c9ab2a987734 | 325 | sensor = i; |
mglmx | 34:c9ab2a987734 | 326 | } |
mglmx | 34:c9ab2a987734 | 327 | } |
mglmx | 34:c9ab2a987734 | 328 | |
mglmx | 34:c9ab2a987734 | 329 | if(interrupt == 1){ |
carlosperales95 | 37:bb15bea420a3 | 330 | sensor+= 8; // Sensors caught by interreupt1 are identified from 8 to 15. |
mglmx | 34:c9ab2a987734 | 331 | } |
mglmx | 34:c9ab2a987734 | 332 | return sensor; |
mglmx | 34:c9ab2a987734 | 333 | } |
mglmx | 34:c9ab2a987734 | 334 | |
mglmx | 35:cfcfeccb959e | 335 | void update_train_pos(int sensor){ |
mglmx | 35:cfcfeccb959e | 336 | bool found_DR = false; |
mglmx | 36:9428c72bdd58 | 337 | bool found_LR = false; |
mglmx | 36:9428c72bdd58 | 338 | |
carlosperales95 | 37:bb15bea420a3 | 339 | Position pos = positions[sensor]; |
carlosperales95 | 37:bb15bea420a3 | 340 | for(int i = 0; i<pos.get_prev_cw().size();i++){ |
mglmx | 35:cfcfeccb959e | 341 | int prev = pos.get_prev_cw()[i]; |
carlosperales95 | 37:bb15bea420a3 | 342 | |
mglmx | 35:cfcfeccb959e | 343 | if(DR_train_pos == prev){ |
mglmx | 36:9428c72bdd58 | 344 | |
mglmx | 35:cfcfeccb959e | 345 | found_DR = true; |
mglmx | 36:9428c72bdd58 | 346 | DR_train_pos = sensor; //We update the position of the train |
mglmx | 35:cfcfeccb959e | 347 | } |
carlosperales95 | 37:bb15bea420a3 | 348 | |
mglmx | 35:cfcfeccb959e | 349 | if(LR_train_pos == prev){ |
mglmx | 35:cfcfeccb959e | 350 | found_LR = true; |
mglmx | 36:9428c72bdd58 | 351 | LR_train_pos = sensor; //We update the position of the train |
mglmx | 35:cfcfeccb959e | 352 | } |
mglmx | 35:cfcfeccb959e | 353 | } |
mglmx | 35:cfcfeccb959e | 354 | |
mglmx | 35:cfcfeccb959e | 355 | if(found_DR){ |
mglmx | 36:9428c72bdd58 | 356 | //doBuzz(); |
mglmx | 35:cfcfeccb959e | 357 | lcd.cls(); |
mglmx | 35:cfcfeccb959e | 358 | lcd.printf("DR is at D%d",DR_train_pos); |
mglmx | 36:9428c72bdd58 | 359 | |
mglmx | 35:cfcfeccb959e | 360 | } |
mglmx | 35:cfcfeccb959e | 361 | if(found_LR){ |
carlosperales95 | 40:9acc1341456a | 362 | |
carlosperales95 | 37:bb15bea420a3 | 363 | lcd.cls(); |
mglmx | 35:cfcfeccb959e | 364 | lcd.printf("LR is at D%d",LR_train_pos); |
mglmx | 35:cfcfeccb959e | 365 | } |
mglmx | 35:cfcfeccb959e | 366 | if(!found_DR && !found_LR){ |
mglmx | 35:cfcfeccb959e | 367 | lcd.cls(); |
mglmx | 35:cfcfeccb959e | 368 | lcd.printf("No train before :("); |
mglmx | 35:cfcfeccb959e | 369 | } |
mglmx | 35:cfcfeccb959e | 370 | } |
mglmx | 35:cfcfeccb959e | 371 | |
carlosperales95 | 37:bb15bea420a3 | 372 | |
mglmx | 34:c9ab2a987734 | 373 | /** |
mglmx | 34:c9ab2a987734 | 374 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 375 | *Method to catch interrupts 0 |
carlosperales95 | 25:a42a1ed4d8e9 | 376 | * |
carlosperales95 | 24:1d71dd8778c4 | 377 | **/ |
mglmx | 33:24ce12dec157 | 378 | void on_int0_change(){ |
carlosperales95 | 25:a42a1ed4d8e9 | 379 | |
mglmx | 33:24ce12dec157 | 380 | wait_us(2000); |
mglmx | 33:24ce12dec157 | 381 | int sensor_data = mcp->_read(INTCAPA); |
mglmx | 34:c9ab2a987734 | 382 | int sensor = get_sensor(sensor_data,0); |
mglmx | 17:0a657e338356 | 383 | lcd.cls(); |
mglmx | 34:c9ab2a987734 | 384 | lcd.printf("int0 0x%x \n Sensor: %d",sensor_data,sensor); |
mglmx | 22:e4153ca757dd | 385 | |
mglmx | 36:9428c72bdd58 | 386 | |
mglmx | 36:9428c72bdd58 | 387 | |
mglmx | 35:cfcfeccb959e | 388 | update_train_pos(sensor); |
mglmx | 16:2a2da0e67793 | 389 | } |
mglmx | 16:2a2da0e67793 | 390 | |
mglmx | 35:cfcfeccb959e | 391 | |
mglmx | 35:cfcfeccb959e | 392 | |
carlosperales95 | 24:1d71dd8778c4 | 393 | /** |
carlosperales95 | 24:1d71dd8778c4 | 394 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 395 | *Method to catch interrupts 1 |
carlosperales95 | 25:a42a1ed4d8e9 | 396 | * |
carlosperales95 | 24:1d71dd8778c4 | 397 | **/ |
mglmx | 33:24ce12dec157 | 398 | void on_int1_change(){ |
carlosperales95 | 25:a42a1ed4d8e9 | 399 | |
mglmx | 33:24ce12dec157 | 400 | wait_us(2000); |
mglmx | 33:24ce12dec157 | 401 | int sensor_data = mcp->_read(INTCAPB); |
mglmx | 34:c9ab2a987734 | 402 | int sensor = get_sensor(sensor_data,1); |
mglmx | 33:24ce12dec157 | 403 | lcd.cls(); |
mglmx | 34:c9ab2a987734 | 404 | lcd.printf("int1 0x%x \n Sensor: %d",sensor_data,sensor); |
mglmx | 35:cfcfeccb959e | 405 | |
mglmx | 35:cfcfeccb959e | 406 | update_train_pos(sensor); |
mglmx | 16:2a2da0e67793 | 407 | } |
mglmx | 26:5c966a0a3e8e | 408 | |
mglmx | 26:5c966a0a3e8e | 409 | |
mglmx | 33:24ce12dec157 | 410 | void init() { // Clear current interrupts |
mglmx | 33:24ce12dec157 | 411 | mcp->_read(GPIOA); |
mglmx | 33:24ce12dec157 | 412 | mcp->_read(GPIOB); // Register callbacks |
mglmx | 33:24ce12dec157 | 413 | int0.fall(&on_int0_change); |
mglmx | 33:24ce12dec157 | 414 | int1.fall(&on_int1_change); // Enable interrupts on MCP |
mglmx | 33:24ce12dec157 | 415 | mcp->_write(GPINTENA, (unsigned char )0xff); |
mglmx | 33:24ce12dec157 | 416 | mcp->_write(GPINTENB, (unsigned char )0xff); // Ready to go! |
mglmx | 33:24ce12dec157 | 417 | } |
mglmx | 33:24ce12dec157 | 418 | |
mglmx | 33:24ce12dec157 | 419 | |
carlosperales95 | 24:1d71dd8778c4 | 420 | /** |
carlosperales95 | 24:1d71dd8778c4 | 421 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 422 | *Method to send DCC commands to train and switches. |
carlosperales95 | 25:a42a1ed4d8e9 | 423 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 424 | *@address - (HEX)Address where the commands will be sent |
carlosperales95 | 25:a42a1ed4d8e9 | 425 | *@inst - (HEX)Number of instruction that will be commanded |
carlosperales95 | 25:a42a1ed4d8e9 | 426 | *@repeat_count - Number of times the command will be sent |
carlosperales95 | 25:a42a1ed4d8e9 | 427 | * |
carlosperales95 | 24:1d71dd8778c4 | 428 | **/ |
mglmx | 1:0ab26889af9b | 429 | void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count) |
mglmx | 1:0ab26889af9b | 430 | { |
mglmx | 1:0ab26889af9b | 431 | unsigned __int64 command = 0x0000000000000000; // __int64 is the 64-bit integer type |
mglmx | 1:0ab26889af9b | 432 | unsigned __int64 temp_command = 0x0000000000000000; |
mglmx | 1:0ab26889af9b | 433 | unsigned __int64 prefix = 0x3FFF; // 14 "1" bits needed at start |
mglmx | 1:0ab26889af9b | 434 | unsigned int error = 0x00; //error byte |
carlosperales95 | 24:1d71dd8778c4 | 435 | |
mglmx | 1:0ab26889af9b | 436 | //calculate error detection byte with xor |
mglmx | 1:0ab26889af9b | 437 | error = address ^ inst; |
carlosperales95 | 24:1d71dd8778c4 | 438 | |
mglmx | 1:0ab26889af9b | 439 | //combine packet bits in basic DCC format |
mglmx | 1:0ab26889af9b | 440 | command = (prefix<<28)|(address<<19)|(inst<<10)|((error)<<1)|0x01; |
mglmx | 1:0ab26889af9b | 441 | //printf("\n\r %llx \n\r",command); |
carlosperales95 | 24:1d71dd8778c4 | 442 | |
mglmx | 1:0ab26889af9b | 443 | int i=0; |
carlosperales95 | 24:1d71dd8778c4 | 444 | //repeat DCC command lots of times |
mglmx | 1:0ab26889af9b | 445 | while(i < repeat_count) { |
carlosperales95 | 24:1d71dd8778c4 | 446 | |
mglmx | 1:0ab26889af9b | 447 | temp_command = command; |
carlosperales95 | 24:1d71dd8778c4 | 448 | //loops through packet bits encoding and sending out digital pulses for a DCC command |
mglmx | 1:0ab26889af9b | 449 | for (int j=0; j<64; j++) { |
carlosperales95 | 24:1d71dd8778c4 | 450 | |
carlosperales95 | 24:1d71dd8778c4 | 451 | if((temp_command&0x8000000000000000)==0) { |
carlosperales95 | 24:1d71dd8778c4 | 452 | //test packet bit |
mglmx | 1:0ab26889af9b | 453 | //send data for a "0" bit |
mglmx | 1:0ab26889af9b | 454 | Track=0; |
mglmx | 1:0ab26889af9b | 455 | wait_us(100); |
mglmx | 1:0ab26889af9b | 456 | Track=1; |
mglmx | 1:0ab26889af9b | 457 | wait_us(100); |
mglmx | 1:0ab26889af9b | 458 | //printf("0011"); |
carlosperales95 | 24:1d71dd8778c4 | 459 | |
carlosperales95 | 24:1d71dd8778c4 | 460 | }else{ |
carlosperales95 | 24:1d71dd8778c4 | 461 | |
mglmx | 1:0ab26889af9b | 462 | //send data for a "1"bit |
mglmx | 1:0ab26889af9b | 463 | Track=0; |
mglmx | 1:0ab26889af9b | 464 | wait_us(58); |
mglmx | 1:0ab26889af9b | 465 | Track=1; |
mglmx | 1:0ab26889af9b | 466 | wait_us(58); |
mglmx | 1:0ab26889af9b | 467 | //printf("01"); |
mglmx | 1:0ab26889af9b | 468 | } |
mglmx | 1:0ab26889af9b | 469 | // next bit in packet |
mglmx | 1:0ab26889af9b | 470 | temp_command = temp_command<<1; |
mglmx | 1:0ab26889af9b | 471 | } |
mglmx | 1:0ab26889af9b | 472 | i++; |
mglmx | 0:4d06a6a8e785 | 473 | } |
mglmx | 0:4d06a6a8e785 | 474 | } |
carlosperales95 | 11:021210c59a95 | 475 | |
carlosperales95 | 24:1d71dd8778c4 | 476 | |
carlosperales95 | 24:1d71dd8778c4 | 477 | /** |
carlosperales95 | 24:1d71dd8778c4 | 478 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 479 | *Method to flip the switches |
carlosperales95 | 25:a42a1ed4d8e9 | 480 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 481 | *@switchId - (1-4)The ID of the switch we want to flip |
carlosperales95 | 25:a42a1ed4d8e9 | 482 | *@times - The number of times we want to send the command |
mglmx | 30:293ee760d357 | 483 | *@activate - True if the switch is going to be activated. False if it needs to go back to rest position. |
carlosperales95 | 25:a42a1ed4d8e9 | 484 | * |
carlosperales95 | 24:1d71dd8778c4 | 485 | **/ |
mglmx | 30:293ee760d357 | 486 | void flipSwitch(int switchId, int times, bool activate=true){ |
mglmx | 21:e6f1649add39 | 487 | |
carlosperales95 | 25:a42a1ed4d8e9 | 488 | unsigned int SWBflip = SWBidle; //IDLE - Flip last activated SW. |
mglmx | 21:e6f1649add39 | 489 | |
mglmx | 21:e6f1649add39 | 490 | switch(switchId){ |
mglmx | 21:e6f1649add39 | 491 | case 1: |
carlosperales95 | 25:a42a1ed4d8e9 | 492 | SWBflip = SWBflip_1; //FLIP SW1 |
mglmx | 21:e6f1649add39 | 493 | break; |
mglmx | 21:e6f1649add39 | 494 | case 2: |
carlosperales95 | 25:a42a1ed4d8e9 | 495 | SWBflip = SWBflip_2; //FLIP SW2 |
mglmx | 21:e6f1649add39 | 496 | break; |
mglmx | 21:e6f1649add39 | 497 | case 3: |
carlosperales95 | 25:a42a1ed4d8e9 | 498 | SWBflip = SWBflip_3; //FLIP SW3 |
mglmx | 21:e6f1649add39 | 499 | break; |
mglmx | 21:e6f1649add39 | 500 | case 4: |
carlosperales95 | 25:a42a1ed4d8e9 | 501 | SWBflip = SWBflip_4; //FLIP SW4 |
mglmx | 21:e6f1649add39 | 502 | break; |
mglmx | 21:e6f1649add39 | 503 | default: |
carlosperales95 | 24:1d71dd8778c4 | 504 | break; |
mglmx | 21:e6f1649add39 | 505 | } |
carlosperales95 | 11:021210c59a95 | 506 | |
mglmx | 21:e6f1649add39 | 507 | //Security measure not to burn the switch. |
mglmx | 30:293ee760d357 | 508 | if(times <=5){ |
mglmx | 30:293ee760d357 | 509 | DCC_send_command(SWBaddress,SWBflip,times); //Activating switch |
mglmx | 30:293ee760d357 | 510 | if(!activate){ |
mglmx | 30:293ee760d357 | 511 | DCC_send_command(SWBaddress,SWBidle,times); //Sending IDLE to flip back. |
mglmx | 30:293ee760d357 | 512 | } |
mglmx | 30:293ee760d357 | 513 | } |
mglmx | 21:e6f1649add39 | 514 | |
mglmx | 21:e6f1649add39 | 515 | } |
mglmx | 21:e6f1649add39 | 516 | |
mglmx | 22:e4153ca757dd | 517 | |
carlosperales95 | 24:1d71dd8778c4 | 518 | /** |
carlosperales95 | 24:1d71dd8778c4 | 519 | * |
carlosperales95 | 29:559eb2164488 | 520 | *Checks if any of the switches of the box has been activated. |
carlosperales95 | 29:559eb2164488 | 521 | *Calls necessary function and displays LCD text. |
carlosperales95 | 25:a42a1ed4d8e9 | 522 | * |
carlosperales95 | 24:1d71dd8778c4 | 523 | **/ |
mglmx | 21:e6f1649add39 | 524 | void checkSwitch(){ |
carlosperales95 | 24:1d71dd8778c4 | 525 | |
mglmx | 22:e4153ca757dd | 526 | if(switch1 == 1){ |
carlosperales95 | 24:1d71dd8778c4 | 527 | |
mglmx | 22:e4153ca757dd | 528 | lcd.cls(); |
mglmx | 22:e4153ca757dd | 529 | lcd.printf("Switch 1 ON - SW1"); |
carlosperales95 | 25:a42a1ed4d8e9 | 530 | flipSwitch(1,5); |
carlosperales95 | 24:1d71dd8778c4 | 531 | |
mglmx | 22:e4153ca757dd | 532 | }else if(switch2 == 1){ |
carlosperales95 | 24:1d71dd8778c4 | 533 | |
carlosperales95 | 24:1d71dd8778c4 | 534 | lcd.cls(); |
carlosperales95 | 24:1d71dd8778c4 | 535 | lcd.printf("Switch 2 ON - SW2"); |
carlosperales95 | 24:1d71dd8778c4 | 536 | flipSwitch(2,5); |
carlosperales95 | 24:1d71dd8778c4 | 537 | |
carlosperales95 | 24:1d71dd8778c4 | 538 | }else if(switch3 == 0){ |
carlosperales95 | 24:1d71dd8778c4 | 539 | |
carlosperales95 | 24:1d71dd8778c4 | 540 | lcd.cls(); |
carlosperales95 | 24:1d71dd8778c4 | 541 | lcd.printf("Switch 3 ON - SW3"); |
carlosperales95 | 24:1d71dd8778c4 | 542 | flipSwitch(3,5); |
carlosperales95 | 24:1d71dd8778c4 | 543 | |
carlosperales95 | 24:1d71dd8778c4 | 544 | }else if(switch4 == 0){ |
carlosperales95 | 24:1d71dd8778c4 | 545 | |
carlosperales95 | 24:1d71dd8778c4 | 546 | lcd.cls(); |
carlosperales95 | 24:1d71dd8778c4 | 547 | lcd.printf("Switch 4 ON - IDLE"); |
carlosperales95 | 24:1d71dd8778c4 | 548 | flipSwitch(0,5); |
carlosperales95 | 24:1d71dd8778c4 | 549 | } |
mglmx | 22:e4153ca757dd | 550 | } |
mglmx | 22:e4153ca757dd | 551 | |
carlosperales95 | 24:1d71dd8778c4 | 552 | |
carlosperales95 | 24:1d71dd8778c4 | 553 | |
carlosperales95 | 11:021210c59a95 | 554 | //**************** MAIN PROGRAM FOR DENVER TRAIN ****************// |
carlosperales95 | 11:021210c59a95 | 555 | |
mglmx | 1:0ab26889af9b | 556 | int main() |
mglmx | 1:0ab26889af9b | 557 | { |
carlosperales95 | 25:a42a1ed4d8e9 | 558 | //RISE FOR INTERRUPTS?? NOT WORKING ATM |
carlosperales95 | 25:a42a1ed4d8e9 | 559 | //int0.rise(&interrupt0); |
carlosperales95 | 25:a42a1ed4d8e9 | 560 | //int1.rise(&interrupt1); |
carlosperales95 | 25:a42a1ed4d8e9 | 561 | |
carlosperales95 | 25:a42a1ed4d8e9 | 562 | //Read and display potentiometer |
carlosperales95 | 25:a42a1ed4d8e9 | 563 | //float f = pot.read(); |
carlosperales95 | 25:a42a1ed4d8e9 | 564 | //float vin = f * 3.3; |
carlosperales95 | 25:a42a1ed4d8e9 | 565 | //lcd.printf("vin: %.4f",vin); |
carlosperales95 | 25:a42a1ed4d8e9 | 566 | |
carlosperales95 | 25:a42a1ed4d8e9 | 567 | //0xFFFC //1111111111111100 |
carlosperales95 | 25:a42a1ed4d8e9 | 568 | |
carlosperales95 | 25:a42a1ed4d8e9 | 569 | |
carlosperales95 | 25:a42a1ed4d8e9 | 570 | //Led routine to start main program |
mglmx | 2:f580707c44fa | 571 | led1 = 1; |
mglmx | 22:e4153ca757dd | 572 | wait(0.2); |
mglmx | 2:f580707c44fa | 573 | led1 = 0; |
mglmx | 22:e4153ca757dd | 574 | wait(0.2); |
mglmx | 2:f580707c44fa | 575 | led1 = 1; |
mglmx | 16:2a2da0e67793 | 576 | |
carlosperales95 | 25:a42a1ed4d8e9 | 577 | initialize_mcp(); //mcp initialization for interrupts before train running |
mglmx | 33:24ce12dec157 | 578 | init(); |
mglmx | 35:cfcfeccb959e | 579 | init_positions(); |
mglmx | 16:2a2da0e67793 | 580 | |
carlosperales95 | 25:a42a1ed4d8e9 | 581 | //Train light routine to start running |
mglmx | 32:e5b732fb8e65 | 582 | DCC_send_command(DCCaddressDR,DCC_func_lighton,200); // turn light on full |
mglmx | 32:e5b732fb8e65 | 583 | DCC_send_command(DCCaddressDR,DCC_func_dimlight,400); //dim light |
mglmx | 32:e5b732fb8e65 | 584 | DCC_send_command(DCCaddressDR,DCC_func_lighton,200); //light full again |
mglmx | 22:e4153ca757dd | 585 | |
carlosperales95 | 25:a42a1ed4d8e9 | 586 | //LED3 Shows start of route + LCD notif |
mglmx | 22:e4153ca757dd | 587 | led3 = 1; // Entering the while |
mglmx | 22:e4153ca757dd | 588 | lcd.cls(); |
mglmx | 22:e4153ca757dd | 589 | lcd.printf("Ready to start"); |
carlosperales95 | 19:ff21ba3a4dc5 | 590 | |
carlosperales95 | 14:7bb998edd819 | 591 | //Demo for stopping at the station |
mglmx | 1:0ab26889af9b | 592 | while(1) { |
mglmx | 4:50879dfb82d5 | 593 | |
carlosperales95 | 25:a42a1ed4d8e9 | 594 | checkSwitch(); //Checks for switch commands everytime. |
carlosperales95 | 25:a42a1ed4d8e9 | 595 | |
mglmx | 33:24ce12dec157 | 596 | if(1==0){ |
mglmx | 33:24ce12dec157 | 597 | //if(station == 1){ //If train is on the sensor at the middle of the station it stops and displays LCD text. |
carlosperales95 | 14:7bb998edd819 | 598 | |
mglmx | 4:50879dfb82d5 | 599 | lcd.cls(); |
carlosperales95 | 29:559eb2164488 | 600 | lcd.printf("All aboard\n mind the gap"); |
mglmx | 32:e5b732fb8e65 | 601 | DCC_send_command(DCCaddressDR,DCCinst_stop,400); |
carlosperales95 | 29:559eb2164488 | 602 | lcd.cls(); |
mglmx | 4:50879dfb82d5 | 603 | |
mglmx | 22:e4153ca757dd | 604 | }else{ |
mglmx | 34:c9ab2a987734 | 605 | DCC_send_command(DCCaddressDR,DCCinst_forward,1); // Forward half speed train addres DARK-RED |
mglmx | 32:e5b732fb8e65 | 606 | DCC_send_command(DCCaddressLR,DCCinst_forward,1); // Forward half speed train address LIGHT-RED |
carlosperales95 | 25:a42a1ed4d8e9 | 607 | } |
mglmx | 1:0ab26889af9b | 608 | } |
mglmx | 3:fe7010b693a0 | 609 | } |