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