Denver
/
denver_train_proj
Denver trai project
main.cpp@33:24ce12dec157, 2018-06-13 (annotated)
- Committer:
- mglmx
- Date:
- Wed Jun 13 12:02:48 2018 +0000
- Revision:
- 33:24ce12dec157
- Parent:
- 32:e5b732fb8e65
- Child:
- 34:c9ab2a987734
New interrupts programmed
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 | 18:aa43bb62e60f | 6 | |
mglmx | 1:0ab26889af9b | 7 | //mbed DCC Model Train Demo |
mglmx | 1:0ab26889af9b | 8 | |
carlosperales95 | 7:e2b8461d4f05 | 9 | /******PINS AND DECLARATIONS*******/ |
carlosperales95 | 7:e2b8461d4f05 | 10 | |
carlosperales95 | 25:a42a1ed4d8e9 | 11 | //------PINS |
mglmx | 21:e6f1649add39 | 12 | |
mglmx | 21:e6f1649add39 | 13 | //SWITCHES p5 - p8 |
mglmx | 21:e6f1649add39 | 14 | DigitalIn switch1(p5); |
mglmx | 21:e6f1649add39 | 15 | DigitalIn switch2(p6); |
mglmx | 21:e6f1649add39 | 16 | DigitalIn switch3(p7); |
mglmx | 21:e6f1649add39 | 17 | DigitalIn switch4(p8); |
carlosperales95 | 7:e2b8461d4f05 | 18 | |
carlosperales95 | 10:2088b1935a93 | 19 | //RAIL SENSORS - INT0,INT1 |
carlosperales95 | 7:e2b8461d4f05 | 20 | //INT0 - p9 |
mglmx | 18:aa43bb62e60f | 21 | InterruptIn int0(p9); |
carlosperales95 | 7:e2b8461d4f05 | 22 | //INT1 - p10 |
mglmx | 18:aa43bb62e60f | 23 | InterruptIn int1(p10); |
carlosperales95 | 7:e2b8461d4f05 | 24 | |
carlosperales95 | 7:e2b8461d4f05 | 25 | ///p11 |
carlosperales95 | 7:e2b8461d4f05 | 26 | ///p12 |
carlosperales95 | 12:e914ca5cd44b | 27 | |
carlosperales95 | 7:e2b8461d4f05 | 28 | //M0 - p13 |
carlosperales95 | 29:559eb2164488 | 29 | DigitalIn d21(p13); //Sensor right of the station |
carlosperales95 | 7:e2b8461d4f05 | 30 | //M1 - p14 |
carlosperales95 | 29:559eb2164488 | 31 | DigitalIn d22(p14); //Sensor left of the station |
carlosperales95 | 7:e2b8461d4f05 | 32 | //M2 - p15 |
carlosperales95 | 29:559eb2164488 | 33 | DigitalIn station(p15); //Sensor in the middle of the station |
carlosperales95 | 12:e914ca5cd44b | 34 | |
carlosperales95 | 7:e2b8461d4f05 | 35 | //p16 |
carlosperales95 | 12:e914ca5cd44b | 36 | //p17 |
carlosperales95 | 12:e914ca5cd44b | 37 | |
carlosperales95 | 11:021210c59a95 | 38 | //BUZZER - p18 |
carlosperales95 | 11:021210c59a95 | 39 | DigitalOut buzz(p18); // buzz=0 doesn't beep, buzz=1 beeps |
carlosperales95 | 7:e2b8461d4f05 | 40 | |
carlosperales95 | 7:e2b8461d4f05 | 41 | //POTENTIOMETER - p19 |
carlosperales95 | 13:dbf1ead12cee | 42 | AnalogIn pot(p19); //Gives float value pot.read(). Convert analog input to V with f*3.3 |
carlosperales95 | 7:e2b8461d4f05 | 43 | |
carlosperales95 | 7:e2b8461d4f05 | 44 | //DAT - p20 |
mglmx | 3:fe7010b693a0 | 45 | DigitalOut Track(p20); //Digital output bit used to drive track power via H-bridge |
carlosperales95 | 7:e2b8461d4f05 | 46 | |
carlosperales95 | 7:e2b8461d4f05 | 47 | //LCD SCREEN - p21, p22, p23, p24, p25, p26 |
carlosperales95 | 11:021210c59a95 | 48 | 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 | 49 | |
carlosperales95 | 7:e2b8461d4f05 | 50 | ///p27 |
carlosperales95 | 7:e2b8461d4f05 | 51 | ///p28 |
mglmx | 33:24ce12dec157 | 52 | I2C i2c(p28,p27); |
carlosperales95 | 7:e2b8461d4f05 | 53 | |
carlosperales95 | 7:e2b8461d4f05 | 54 | //LED1 - p29 |
mglmx | 3:fe7010b693a0 | 55 | DigitalOut redled(p29); |
carlosperales95 | 7:e2b8461d4f05 | 56 | //LED2 - p30 |
mglmx | 3:fe7010b693a0 | 57 | DigitalOut greenled(p30); |
carlosperales95 | 7:e2b8461d4f05 | 58 | |
carlosperales95 | 24:1d71dd8778c4 | 59 | //MBED LEDS |
mglmx | 18:aa43bb62e60f | 60 | DigitalOut led1(LED1); |
mglmx | 18:aa43bb62e60f | 61 | DigitalOut led2(LED2); |
mglmx | 18:aa43bb62e60f | 62 | DigitalOut led3(LED3); |
mglmx | 16:2a2da0e67793 | 63 | |
mglmx | 16:2a2da0e67793 | 64 | //MCP |
mglmx | 16:2a2da0e67793 | 65 | MCP23017 *mcp; |
carlosperales95 | 12:e914ca5cd44b | 66 | |
carlosperales95 | 24:1d71dd8778c4 | 67 | |
carlosperales95 | 25:a42a1ed4d8e9 | 68 | //------GLOBAL VARS |
carlosperales95 | 25:a42a1ed4d8e9 | 69 | |
carlosperales95 | 25:a42a1ed4d8e9 | 70 | |
carlosperales95 | 25:a42a1ed4d8e9 | 71 | //.....DCC TRAIN COMMAND VARS |
carlosperales95 | 25:a42a1ed4d8e9 | 72 | |
carlosperales95 | 25:a42a1ed4d8e9 | 73 | //typical out of box default engine DCC address is 3 (at least for Bachmann trains) |
carlosperales95 | 25:a42a1ed4d8e9 | 74 | //Note: A DCC controller can reprogram the address whenever needed |
mglmx | 32:e5b732fb8e65 | 75 | const unsigned int DCCaddressDR = 0x01; //Address for train 1 DARK-RED |
mglmx | 32:e5b732fb8e65 | 76 | const unsigned int DCCaddressLR = 0x03; //Address for train 3 LIGHT-RED |
carlosperales95 | 25:a42a1ed4d8e9 | 77 | |
carlosperales95 | 25:a42a1ed4d8e9 | 78 | //01DCSSSS for speed, D is direction (fwd=1 and rev=0), C is speed(SSSSC) LSB |
carlosperales95 | 25:a42a1ed4d8e9 | 79 | const unsigned int DCCinst_forward = 0x68; //forward half speed |
mglmx | 33:24ce12dec157 | 80 | const unsigned int DCCinst_forward_slow = 0x66; //forward half speed |
carlosperales95 | 25:a42a1ed4d8e9 | 81 | const unsigned int DCCinst_reverse = 0x48; //reverse half speed |
carlosperales95 | 25:a42a1ed4d8e9 | 82 | const unsigned int DCCinst_stop = 0x50; //stop the train |
carlosperales95 | 25:a42a1ed4d8e9 | 83 | |
carlosperales95 | 25:a42a1ed4d8e9 | 84 | //100DDDDD for basic headlight functions |
carlosperales95 | 25:a42a1ed4d8e9 | 85 | const unsigned int DCC_func_lighton = 0x90; //F0 turns on headlight function |
carlosperales95 | 25:a42a1ed4d8e9 | 86 | const unsigned int DCC_func_dimlight = 0x91; //F0 + F1 dims headlight |
carlosperales95 | 25:a42a1ed4d8e9 | 87 | |
carlosperales95 | 25:a42a1ed4d8e9 | 88 | |
carlosperales95 | 25:a42a1ed4d8e9 | 89 | //.....SWITCH COMMAND VARS |
carlosperales95 | 25:a42a1ed4d8e9 | 90 | |
carlosperales95 | 25:a42a1ed4d8e9 | 91 | const unsigned int SWBaddress = 0x06; //Address for switch box |
carlosperales95 | 25:a42a1ed4d8e9 | 92 | |
carlosperales95 | 25:a42a1ed4d8e9 | 93 | //100DDDDD where DDDDD is the switch command and 100 is constant: |
carlosperales95 | 25:a42a1ed4d8e9 | 94 | |
carlosperales95 | 25:a42a1ed4d8e9 | 95 | //00001(F1 active)-00010(F2 active)-00100(F3 active)-01000(F4 active) |
carlosperales95 | 25:a42a1ed4d8e9 | 96 | //Example - 111111 0 00000101 0 10000000 0 10000101 1 - idle |
carlosperales95 | 25:a42a1ed4d8e9 | 97 | |
carlosperales95 | 25:a42a1ed4d8e9 | 98 | const unsigned int SWBidle = 0x80; //IDLE - Flip last activated SW. |
carlosperales95 | 25:a42a1ed4d8e9 | 99 | const unsigned int SWBflip_1 = 0x81; //Flip SW1 |
carlosperales95 | 25:a42a1ed4d8e9 | 100 | const unsigned int SWBflip_2 = 0x82; //Flip SW2 |
carlosperales95 | 25:a42a1ed4d8e9 | 101 | const unsigned int SWBflip_3 = 0x84; //Flip SW3 |
carlosperales95 | 25:a42a1ed4d8e9 | 102 | const unsigned int SWBflip_4 = 0x88; //Flip SW4 |
carlosperales95 | 25:a42a1ed4d8e9 | 103 | |
carlosperales95 | 31:7637861f4dc7 | 104 | //Arrays that will keep track of the position and direction of th trains. |
carlosperales95 | 31:7637861f4dc7 | 105 | //[pos_trains] -1 = not in track / any other number = sensor where the train is at (sensors 1-16) |
carlosperales95 | 31:7637861f4dc7 | 106 | //[dir_trains] -1 = left / 1 = right |
carlosperales95 | 27:f51397917a52 | 107 | int pos_trains [2] = {-1,-1}; |
carlosperales95 | 31:7637861f4dc7 | 108 | int dir_trains [2] = {-1,-1}; |
carlosperales95 | 31:7637861f4dc7 | 109 | |
carlosperales95 | 31:7637861f4dc7 | 110 | |
carlosperales95 | 25:a42a1ed4d8e9 | 111 | |
carlosperales95 | 25:a42a1ed4d8e9 | 112 | |
carlosperales95 | 24:1d71dd8778c4 | 113 | |
carlosperales95 | 11:021210c59a95 | 114 | //**************** FUNCTIONS FOR DENVER TRAIN ****************// |
carlosperales95 | 10:2088b1935a93 | 115 | |
mglmx | 18:aa43bb62e60f | 116 | |
mglmx | 22:e4153ca757dd | 117 | /** |
carlosperales95 | 25:a42a1ed4d8e9 | 118 | *Activates the buzzer for 0.5 seconds. |
mglmx | 22:e4153ca757dd | 119 | **/ |
mglmx | 22:e4153ca757dd | 120 | void doBuzz(){ |
carlosperales95 | 25:a42a1ed4d8e9 | 121 | |
mglmx | 22:e4153ca757dd | 122 | buzz = 1; |
mglmx | 22:e4153ca757dd | 123 | wait(0.5); |
mglmx | 22:e4153ca757dd | 124 | buzz = 0; |
mglmx | 22:e4153ca757dd | 125 | } |
mglmx | 18:aa43bb62e60f | 126 | |
carlosperales95 | 24:1d71dd8778c4 | 127 | |
mglmx | 22:e4153ca757dd | 128 | /** |
carlosperales95 | 24:1d71dd8778c4 | 129 | * |
carlosperales95 | 28:71bd4c83c05f | 130 | *Here we initialize the mcp that will be used to manage the interrupts. |
carlosperales95 | 25:a42a1ed4d8e9 | 131 | * |
carlosperales95 | 24:1d71dd8778c4 | 132 | **/ |
mglmx | 18:aa43bb62e60f | 133 | void initialize_mcp(){ |
mglmx | 33:24ce12dec157 | 134 | mcp = new MCP23017(i2c,0x40); //Connect to SCL - p28 and SDA - p27 and MPC I2C address 0x40 |
mglmx | 18:aa43bb62e60f | 135 | |
mglmx | 33:24ce12dec157 | 136 | mcp->_write(IODIRA, (unsigned char )0xff); |
mglmx | 33:24ce12dec157 | 137 | mcp->_write(IODIRB, (unsigned char )0xff); |
mglmx | 33:24ce12dec157 | 138 | mcp->_write(IPOLA, (unsigned char )0x00); |
mglmx | 33:24ce12dec157 | 139 | mcp->_write(IPOLB, (unsigned char )0x00); |
mglmx | 33:24ce12dec157 | 140 | mcp->_write(DEFVALA, (unsigned char )0xff); |
mglmx | 33:24ce12dec157 | 141 | mcp->_write(DEFVALB, (unsigned char )0xff); |
mglmx | 33:24ce12dec157 | 142 | mcp->_write(INTCONA, (unsigned char )0xff); |
mglmx | 33:24ce12dec157 | 143 | mcp->_write(INTCONB, (unsigned char )0xff); |
mglmx | 33:24ce12dec157 | 144 | mcp->_write(IOCONA, (unsigned char )0x2); |
mglmx | 33:24ce12dec157 | 145 | mcp->_write(IOCONB, (unsigned char )0x2); |
mglmx | 33:24ce12dec157 | 146 | mcp->_write(GPPUA, (unsigned char )0xff); |
mglmx | 33:24ce12dec157 | 147 | mcp->_write(GPPUB, (unsigned char )0xff); |
mglmx | 33:24ce12dec157 | 148 | |
mglmx | 18:aa43bb62e60f | 149 | } |
mglmx | 18:aa43bb62e60f | 150 | |
carlosperales95 | 24:1d71dd8778c4 | 151 | /** |
carlosperales95 | 24:1d71dd8778c4 | 152 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 153 | *Method to catch interrupts 0 |
carlosperales95 | 25:a42a1ed4d8e9 | 154 | * |
carlosperales95 | 24:1d71dd8778c4 | 155 | **/ |
mglmx | 33:24ce12dec157 | 156 | void on_int0_change(){ |
carlosperales95 | 25:a42a1ed4d8e9 | 157 | |
mglmx | 33:24ce12dec157 | 158 | wait_us(2000); |
mglmx | 33:24ce12dec157 | 159 | int sensor_data = mcp->_read(INTCAPA); |
mglmx | 17:0a657e338356 | 160 | lcd.cls(); |
mglmx | 33:24ce12dec157 | 161 | lcd.printf("int0 0x%x",sensor_data); |
mglmx | 22:e4153ca757dd | 162 | |
mglmx | 16:2a2da0e67793 | 163 | } |
mglmx | 16:2a2da0e67793 | 164 | |
carlosperales95 | 24:1d71dd8778c4 | 165 | |
carlosperales95 | 24:1d71dd8778c4 | 166 | /** |
carlosperales95 | 24:1d71dd8778c4 | 167 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 168 | *Method to catch interrupts 1 |
carlosperales95 | 25:a42a1ed4d8e9 | 169 | * |
carlosperales95 | 24:1d71dd8778c4 | 170 | **/ |
mglmx | 33:24ce12dec157 | 171 | void on_int1_change(){ |
carlosperales95 | 25:a42a1ed4d8e9 | 172 | |
mglmx | 33:24ce12dec157 | 173 | wait_us(2000); |
mglmx | 33:24ce12dec157 | 174 | int sensor_data = mcp->_read(INTCAPB); |
mglmx | 33:24ce12dec157 | 175 | lcd.cls(); |
mglmx | 33:24ce12dec157 | 176 | lcd.printf("int1 0x%x",sensor_data); |
mglmx | 16:2a2da0e67793 | 177 | } |
mglmx | 26:5c966a0a3e8e | 178 | |
mglmx | 26:5c966a0a3e8e | 179 | |
mglmx | 33:24ce12dec157 | 180 | void init() { // Clear current interrupts |
mglmx | 33:24ce12dec157 | 181 | mcp->_read(GPIOA); |
mglmx | 33:24ce12dec157 | 182 | mcp->_read(GPIOB); // Register callbacks |
mglmx | 33:24ce12dec157 | 183 | int0.fall(&on_int0_change); |
mglmx | 33:24ce12dec157 | 184 | int1.fall(&on_int1_change); // Enable interrupts on MCP |
mglmx | 33:24ce12dec157 | 185 | mcp->_write(GPINTENA, (unsigned char )0xff); |
mglmx | 33:24ce12dec157 | 186 | mcp->_write(GPINTENB, (unsigned char )0xff); // Ready to go! |
mglmx | 33:24ce12dec157 | 187 | } |
mglmx | 33:24ce12dec157 | 188 | |
mglmx | 33:24ce12dec157 | 189 | |
mglmx | 33:24ce12dec157 | 190 | |
mglmx | 33:24ce12dec157 | 191 | |
mglmx | 26:5c966a0a3e8e | 192 | /** |
carlosperales95 | 27:f51397917a52 | 193 | * |
carlosperales95 | 28:71bd4c83c05f | 194 | *Print the positions where there is a 0 |
carlosperales95 | 27:f51397917a52 | 195 | * |
mglmx | 26:5c966a0a3e8e | 196 | **/ |
mglmx | 26:5c966a0a3e8e | 197 | |
carlosperales95 | 31:7637861f4dc7 | 198 | void train_pos_update(unsigned int number){ |
mglmx | 26:5c966a0a3e8e | 199 | |
mglmx | 26:5c966a0a3e8e | 200 | /* |
mglmx | 26:5c966a0a3e8e | 201 | bitset <sizeof(unsigned int) * 8> n (number); |
mglmx | 26:5c966a0a3e8e | 202 | |
mglmx | 26:5c966a0a3e8e | 203 | cout << "Binary: " << n << endl; |
mglmx | 26:5c966a0a3e8e | 204 | */ |
mglmx | 26:5c966a0a3e8e | 205 | |
mglmx | 26:5c966a0a3e8e | 206 | for(int i=0; i<16;i++){ |
carlosperales95 | 27:f51397917a52 | 207 | |
carlosperales95 | 27:f51397917a52 | 208 | if(~number & 1<<i){ |
carlosperales95 | 27:f51397917a52 | 209 | |
mglmx | 26:5c966a0a3e8e | 210 | cout << "There is a 0 at " << i << endl; |
carlosperales95 | 27:f51397917a52 | 211 | pos_trains[1] = pos_trains[0]; |
carlosperales95 | 27:f51397917a52 | 212 | pos_trains[0] = i; |
mglmx | 26:5c966a0a3e8e | 213 | } |
mglmx | 26:5c966a0a3e8e | 214 | } |
mglmx | 26:5c966a0a3e8e | 215 | } |
mglmx | 5:ce0f66ea12c5 | 216 | |
mglmx | 1:0ab26889af9b | 217 | |
carlosperales95 | 24:1d71dd8778c4 | 218 | /** |
carlosperales95 | 24:1d71dd8778c4 | 219 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 220 | *Method to send DCC commands to train and switches. |
carlosperales95 | 25:a42a1ed4d8e9 | 221 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 222 | *@address - (HEX)Address where the commands will be sent |
carlosperales95 | 25:a42a1ed4d8e9 | 223 | *@inst - (HEX)Number of instruction that will be commanded |
carlosperales95 | 25:a42a1ed4d8e9 | 224 | *@repeat_count - Number of times the command will be sent |
carlosperales95 | 25:a42a1ed4d8e9 | 225 | * |
carlosperales95 | 24:1d71dd8778c4 | 226 | **/ |
mglmx | 1:0ab26889af9b | 227 | void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count) |
mglmx | 1:0ab26889af9b | 228 | { |
mglmx | 1:0ab26889af9b | 229 | unsigned __int64 command = 0x0000000000000000; // __int64 is the 64-bit integer type |
mglmx | 1:0ab26889af9b | 230 | unsigned __int64 temp_command = 0x0000000000000000; |
mglmx | 1:0ab26889af9b | 231 | unsigned __int64 prefix = 0x3FFF; // 14 "1" bits needed at start |
mglmx | 1:0ab26889af9b | 232 | unsigned int error = 0x00; //error byte |
carlosperales95 | 24:1d71dd8778c4 | 233 | |
mglmx | 1:0ab26889af9b | 234 | //calculate error detection byte with xor |
mglmx | 1:0ab26889af9b | 235 | error = address ^ inst; |
carlosperales95 | 24:1d71dd8778c4 | 236 | |
mglmx | 1:0ab26889af9b | 237 | //combine packet bits in basic DCC format |
mglmx | 1:0ab26889af9b | 238 | command = (prefix<<28)|(address<<19)|(inst<<10)|((error)<<1)|0x01; |
mglmx | 1:0ab26889af9b | 239 | //printf("\n\r %llx \n\r",command); |
carlosperales95 | 24:1d71dd8778c4 | 240 | |
mglmx | 1:0ab26889af9b | 241 | int i=0; |
carlosperales95 | 24:1d71dd8778c4 | 242 | //repeat DCC command lots of times |
mglmx | 1:0ab26889af9b | 243 | while(i < repeat_count) { |
carlosperales95 | 24:1d71dd8778c4 | 244 | |
mglmx | 1:0ab26889af9b | 245 | temp_command = command; |
carlosperales95 | 24:1d71dd8778c4 | 246 | //loops through packet bits encoding and sending out digital pulses for a DCC command |
mglmx | 1:0ab26889af9b | 247 | for (int j=0; j<64; j++) { |
carlosperales95 | 24:1d71dd8778c4 | 248 | |
carlosperales95 | 24:1d71dd8778c4 | 249 | if((temp_command&0x8000000000000000)==0) { |
carlosperales95 | 24:1d71dd8778c4 | 250 | //test packet bit |
mglmx | 1:0ab26889af9b | 251 | //send data for a "0" bit |
mglmx | 1:0ab26889af9b | 252 | Track=0; |
mglmx | 1:0ab26889af9b | 253 | wait_us(100); |
mglmx | 1:0ab26889af9b | 254 | Track=1; |
mglmx | 1:0ab26889af9b | 255 | wait_us(100); |
mglmx | 1:0ab26889af9b | 256 | //printf("0011"); |
carlosperales95 | 24:1d71dd8778c4 | 257 | |
carlosperales95 | 24:1d71dd8778c4 | 258 | }else{ |
carlosperales95 | 24:1d71dd8778c4 | 259 | |
mglmx | 1:0ab26889af9b | 260 | //send data for a "1"bit |
mglmx | 1:0ab26889af9b | 261 | Track=0; |
mglmx | 1:0ab26889af9b | 262 | wait_us(58); |
mglmx | 1:0ab26889af9b | 263 | Track=1; |
mglmx | 1:0ab26889af9b | 264 | wait_us(58); |
mglmx | 1:0ab26889af9b | 265 | //printf("01"); |
mglmx | 1:0ab26889af9b | 266 | } |
mglmx | 1:0ab26889af9b | 267 | // next bit in packet |
mglmx | 1:0ab26889af9b | 268 | temp_command = temp_command<<1; |
mglmx | 1:0ab26889af9b | 269 | } |
mglmx | 1:0ab26889af9b | 270 | i++; |
mglmx | 0:4d06a6a8e785 | 271 | } |
mglmx | 0:4d06a6a8e785 | 272 | } |
carlosperales95 | 11:021210c59a95 | 273 | |
carlosperales95 | 24:1d71dd8778c4 | 274 | |
carlosperales95 | 24:1d71dd8778c4 | 275 | /** |
carlosperales95 | 24:1d71dd8778c4 | 276 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 277 | *Method to flip the switches |
carlosperales95 | 25:a42a1ed4d8e9 | 278 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 279 | *@switchId - (1-4)The ID of the switch we want to flip |
carlosperales95 | 25:a42a1ed4d8e9 | 280 | *@times - The number of times we want to send the command |
mglmx | 30:293ee760d357 | 281 | *@activate - True if the switch is going to be activated. False if it needs to go back to rest position. |
carlosperales95 | 25:a42a1ed4d8e9 | 282 | * |
carlosperales95 | 24:1d71dd8778c4 | 283 | **/ |
mglmx | 30:293ee760d357 | 284 | void flipSwitch(int switchId, int times, bool activate=true){ |
mglmx | 21:e6f1649add39 | 285 | |
carlosperales95 | 25:a42a1ed4d8e9 | 286 | unsigned int SWBflip = SWBidle; //IDLE - Flip last activated SW. |
mglmx | 21:e6f1649add39 | 287 | |
mglmx | 21:e6f1649add39 | 288 | switch(switchId){ |
mglmx | 21:e6f1649add39 | 289 | case 1: |
carlosperales95 | 25:a42a1ed4d8e9 | 290 | SWBflip = SWBflip_1; //FLIP SW1 |
mglmx | 21:e6f1649add39 | 291 | break; |
mglmx | 21:e6f1649add39 | 292 | case 2: |
carlosperales95 | 25:a42a1ed4d8e9 | 293 | SWBflip = SWBflip_2; //FLIP SW2 |
mglmx | 21:e6f1649add39 | 294 | break; |
mglmx | 21:e6f1649add39 | 295 | case 3: |
carlosperales95 | 25:a42a1ed4d8e9 | 296 | SWBflip = SWBflip_3; //FLIP SW3 |
mglmx | 21:e6f1649add39 | 297 | break; |
mglmx | 21:e6f1649add39 | 298 | case 4: |
carlosperales95 | 25:a42a1ed4d8e9 | 299 | SWBflip = SWBflip_4; //FLIP SW4 |
mglmx | 21:e6f1649add39 | 300 | break; |
mglmx | 21:e6f1649add39 | 301 | default: |
carlosperales95 | 24:1d71dd8778c4 | 302 | break; |
mglmx | 21:e6f1649add39 | 303 | } |
carlosperales95 | 11:021210c59a95 | 304 | |
mglmx | 21:e6f1649add39 | 305 | //Security measure not to burn the switch. |
mglmx | 30:293ee760d357 | 306 | if(times <=5){ |
mglmx | 30:293ee760d357 | 307 | DCC_send_command(SWBaddress,SWBflip,times); //Activating switch |
mglmx | 30:293ee760d357 | 308 | if(!activate){ |
mglmx | 30:293ee760d357 | 309 | DCC_send_command(SWBaddress,SWBidle,times); //Sending IDLE to flip back. |
mglmx | 30:293ee760d357 | 310 | } |
mglmx | 30:293ee760d357 | 311 | } |
mglmx | 21:e6f1649add39 | 312 | |
mglmx | 21:e6f1649add39 | 313 | } |
mglmx | 21:e6f1649add39 | 314 | |
mglmx | 22:e4153ca757dd | 315 | |
carlosperales95 | 24:1d71dd8778c4 | 316 | /** |
carlosperales95 | 24:1d71dd8778c4 | 317 | * |
carlosperales95 | 29:559eb2164488 | 318 | *Checks if any of the switches of the box has been activated. |
carlosperales95 | 29:559eb2164488 | 319 | *Calls necessary function and displays LCD text. |
carlosperales95 | 25:a42a1ed4d8e9 | 320 | * |
carlosperales95 | 24:1d71dd8778c4 | 321 | **/ |
mglmx | 21:e6f1649add39 | 322 | void checkSwitch(){ |
carlosperales95 | 24:1d71dd8778c4 | 323 | |
mglmx | 22:e4153ca757dd | 324 | if(switch1 == 1){ |
carlosperales95 | 24:1d71dd8778c4 | 325 | |
mglmx | 22:e4153ca757dd | 326 | lcd.cls(); |
mglmx | 22:e4153ca757dd | 327 | lcd.printf("Switch 1 ON - SW1"); |
carlosperales95 | 25:a42a1ed4d8e9 | 328 | flipSwitch(1,5); |
carlosperales95 | 24:1d71dd8778c4 | 329 | |
mglmx | 22:e4153ca757dd | 330 | }else if(switch2 == 1){ |
carlosperales95 | 24:1d71dd8778c4 | 331 | |
carlosperales95 | 24:1d71dd8778c4 | 332 | lcd.cls(); |
carlosperales95 | 24:1d71dd8778c4 | 333 | lcd.printf("Switch 2 ON - SW2"); |
carlosperales95 | 24:1d71dd8778c4 | 334 | flipSwitch(2,5); |
carlosperales95 | 24:1d71dd8778c4 | 335 | |
carlosperales95 | 24:1d71dd8778c4 | 336 | }else if(switch3 == 0){ |
carlosperales95 | 24:1d71dd8778c4 | 337 | |
carlosperales95 | 24:1d71dd8778c4 | 338 | lcd.cls(); |
carlosperales95 | 24:1d71dd8778c4 | 339 | lcd.printf("Switch 3 ON - SW3"); |
carlosperales95 | 24:1d71dd8778c4 | 340 | flipSwitch(3,5); |
carlosperales95 | 24:1d71dd8778c4 | 341 | |
carlosperales95 | 24:1d71dd8778c4 | 342 | }else if(switch4 == 0){ |
carlosperales95 | 24:1d71dd8778c4 | 343 | |
carlosperales95 | 24:1d71dd8778c4 | 344 | lcd.cls(); |
carlosperales95 | 24:1d71dd8778c4 | 345 | lcd.printf("Switch 4 ON - IDLE"); |
carlosperales95 | 24:1d71dd8778c4 | 346 | flipSwitch(0,5); |
carlosperales95 | 24:1d71dd8778c4 | 347 | } |
mglmx | 22:e4153ca757dd | 348 | } |
mglmx | 22:e4153ca757dd | 349 | |
carlosperales95 | 24:1d71dd8778c4 | 350 | |
carlosperales95 | 24:1d71dd8778c4 | 351 | |
carlosperales95 | 11:021210c59a95 | 352 | //**************** MAIN PROGRAM FOR DENVER TRAIN ****************// |
carlosperales95 | 11:021210c59a95 | 353 | |
mglmx | 1:0ab26889af9b | 354 | int main() |
mglmx | 1:0ab26889af9b | 355 | { |
carlosperales95 | 25:a42a1ed4d8e9 | 356 | //RISE FOR INTERRUPTS?? NOT WORKING ATM |
carlosperales95 | 25:a42a1ed4d8e9 | 357 | //int0.rise(&interrupt0); |
carlosperales95 | 25:a42a1ed4d8e9 | 358 | //int1.rise(&interrupt1); |
carlosperales95 | 25:a42a1ed4d8e9 | 359 | |
carlosperales95 | 25:a42a1ed4d8e9 | 360 | //Read and display potentiometer |
carlosperales95 | 25:a42a1ed4d8e9 | 361 | //float f = pot.read(); |
carlosperales95 | 25:a42a1ed4d8e9 | 362 | //float vin = f * 3.3; |
carlosperales95 | 25:a42a1ed4d8e9 | 363 | //lcd.printf("vin: %.4f",vin); |
carlosperales95 | 25:a42a1ed4d8e9 | 364 | |
carlosperales95 | 25:a42a1ed4d8e9 | 365 | //0xFFFC //1111111111111100 |
carlosperales95 | 25:a42a1ed4d8e9 | 366 | |
carlosperales95 | 25:a42a1ed4d8e9 | 367 | |
carlosperales95 | 25:a42a1ed4d8e9 | 368 | //Led routine to start main program |
mglmx | 2:f580707c44fa | 369 | led1 = 1; |
mglmx | 22:e4153ca757dd | 370 | wait(0.2); |
mglmx | 2:f580707c44fa | 371 | led1 = 0; |
mglmx | 22:e4153ca757dd | 372 | wait(0.2); |
mglmx | 2:f580707c44fa | 373 | led1 = 1; |
mglmx | 16:2a2da0e67793 | 374 | |
carlosperales95 | 25:a42a1ed4d8e9 | 375 | initialize_mcp(); //mcp initialization for interrupts before train running |
mglmx | 33:24ce12dec157 | 376 | init(); |
mglmx | 16:2a2da0e67793 | 377 | |
carlosperales95 | 25:a42a1ed4d8e9 | 378 | //Train light routine to start running |
mglmx | 32:e5b732fb8e65 | 379 | DCC_send_command(DCCaddressDR,DCC_func_lighton,200); // turn light on full |
mglmx | 32:e5b732fb8e65 | 380 | DCC_send_command(DCCaddressDR,DCC_func_dimlight,400); //dim light |
mglmx | 32:e5b732fb8e65 | 381 | DCC_send_command(DCCaddressDR,DCC_func_lighton,200); //light full again |
mglmx | 22:e4153ca757dd | 382 | |
carlosperales95 | 25:a42a1ed4d8e9 | 383 | //LED3 Shows start of route + LCD notif |
mglmx | 22:e4153ca757dd | 384 | led3 = 1; // Entering the while |
mglmx | 22:e4153ca757dd | 385 | lcd.cls(); |
mglmx | 22:e4153ca757dd | 386 | lcd.printf("Ready to start"); |
carlosperales95 | 19:ff21ba3a4dc5 | 387 | |
carlosperales95 | 14:7bb998edd819 | 388 | //Demo for stopping at the station |
mglmx | 1:0ab26889af9b | 389 | while(1) { |
mglmx | 4:50879dfb82d5 | 390 | |
carlosperales95 | 25:a42a1ed4d8e9 | 391 | checkSwitch(); //Checks for switch commands everytime. |
carlosperales95 | 25:a42a1ed4d8e9 | 392 | |
mglmx | 33:24ce12dec157 | 393 | if(1==0){ |
mglmx | 33:24ce12dec157 | 394 | //if(station == 1){ //If train is on the sensor at the middle of the station it stops and displays LCD text. |
carlosperales95 | 14:7bb998edd819 | 395 | |
mglmx | 4:50879dfb82d5 | 396 | lcd.cls(); |
carlosperales95 | 29:559eb2164488 | 397 | lcd.printf("All aboard\n mind the gap"); |
mglmx | 32:e5b732fb8e65 | 398 | DCC_send_command(DCCaddressDR,DCCinst_stop,400); |
carlosperales95 | 29:559eb2164488 | 399 | lcd.cls(); |
mglmx | 4:50879dfb82d5 | 400 | |
mglmx | 22:e4153ca757dd | 401 | }else{ |
mglmx | 33:24ce12dec157 | 402 | DCC_send_command(DCCaddressDR,DCCinst_forward_slow,1); // Forward half speed train addres DARK-RED |
mglmx | 32:e5b732fb8e65 | 403 | DCC_send_command(DCCaddressLR,DCCinst_forward,1); // Forward half speed train address LIGHT-RED |
carlosperales95 | 25:a42a1ed4d8e9 | 404 | } |
mglmx | 1:0ab26889af9b | 405 | } |
mglmx | 3:fe7010b693a0 | 406 | } |