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@29:559eb2164488, 2018-06-12 (annotated)
- Committer:
- carlosperales95
- Date:
- Tue Jun 12 10:43:43 2018 +0000
- Revision:
- 29:559eb2164488
- Parent:
- 28:71bd4c83c05f
- Child:
- 30:293ee760d357
Delete all switch function. No use.
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 | 27:f51397917a52 | 101 | //Array that will keep track of the position of th trains. (-1 = not in track/any other number = sensor where the train is at) |
carlosperales95 | 27:f51397917a52 | 102 | int pos_trains [2] = {-1,-1}; |
carlosperales95 | 25:a42a1ed4d8e9 | 103 | |
carlosperales95 | 25:a42a1ed4d8e9 | 104 | |
carlosperales95 | 24:1d71dd8778c4 | 105 | |
carlosperales95 | 11:021210c59a95 | 106 | //**************** FUNCTIONS FOR DENVER TRAIN ****************// |
carlosperales95 | 10:2088b1935a93 | 107 | |
mglmx | 18:aa43bb62e60f | 108 | |
mglmx | 22:e4153ca757dd | 109 | /** |
carlosperales95 | 25:a42a1ed4d8e9 | 110 | *Activates the buzzer for 0.5 seconds. |
mglmx | 22:e4153ca757dd | 111 | **/ |
mglmx | 22:e4153ca757dd | 112 | void doBuzz(){ |
carlosperales95 | 25:a42a1ed4d8e9 | 113 | |
mglmx | 22:e4153ca757dd | 114 | buzz = 1; |
mglmx | 22:e4153ca757dd | 115 | wait(0.5); |
mglmx | 22:e4153ca757dd | 116 | buzz = 0; |
mglmx | 22:e4153ca757dd | 117 | } |
mglmx | 18:aa43bb62e60f | 118 | |
carlosperales95 | 24:1d71dd8778c4 | 119 | |
mglmx | 22:e4153ca757dd | 120 | /** |
carlosperales95 | 24:1d71dd8778c4 | 121 | * |
carlosperales95 | 28:71bd4c83c05f | 122 | *Here we initialize the mcp that will be used to manage the interrupts. |
carlosperales95 | 25:a42a1ed4d8e9 | 123 | * |
carlosperales95 | 24:1d71dd8778c4 | 124 | **/ |
mglmx | 18:aa43bb62e60f | 125 | void initialize_mcp(){ |
mglmx | 18:aa43bb62e60f | 126 | mcp = new MCP23017(p28,p27,0x40); //Connect to SCL - p28 and SDA - p27 and MPC I2C address 0x40 |
mglmx | 18:aa43bb62e60f | 127 | |
mglmx | 18:aa43bb62e60f | 128 | mcp->reset(); |
mglmx | 18:aa43bb62e60f | 129 | mcp->writeRegister(0x00, (unsigned char )0xff); |
mglmx | 18:aa43bb62e60f | 130 | mcp->writeRegister(0x01, (unsigned char )0xff); |
mglmx | 18:aa43bb62e60f | 131 | mcp->writeRegister(0x02, (unsigned char )0x00); |
mglmx | 18:aa43bb62e60f | 132 | mcp->writeRegister(0x03, (unsigned char )0x00); |
mglmx | 18:aa43bb62e60f | 133 | mcp->writeRegister(0x04, (unsigned char )0xff); |
mglmx | 18:aa43bb62e60f | 134 | mcp->writeRegister(0x05, (unsigned char )0xff); |
mglmx | 18:aa43bb62e60f | 135 | mcp->writeRegister(0x06, (unsigned char )0xff); |
mglmx | 18:aa43bb62e60f | 136 | mcp->writeRegister(0x07, (unsigned char )0xff); |
mglmx | 18:aa43bb62e60f | 137 | mcp->writeRegister(0x08, (unsigned char )0xff); |
mglmx | 18:aa43bb62e60f | 138 | mcp->writeRegister(0x09, (unsigned char )0xff); |
mglmx | 18:aa43bb62e60f | 139 | mcp->writeRegister(0x0a, (unsigned char )0x42); |
mglmx | 18:aa43bb62e60f | 140 | mcp->writeRegister(0x0b, (unsigned char )0x42); |
mglmx | 18:aa43bb62e60f | 141 | mcp->writeRegister(0x0c, (unsigned char )0x00); |
mglmx | 18:aa43bb62e60f | 142 | mcp->writeRegister(0x0d, (unsigned char )0x00); |
mglmx | 18:aa43bb62e60f | 143 | } |
mglmx | 18:aa43bb62e60f | 144 | |
carlosperales95 | 24:1d71dd8778c4 | 145 | |
carlosperales95 | 24:1d71dd8778c4 | 146 | /** |
carlosperales95 | 24:1d71dd8778c4 | 147 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 148 | *Method to catch interrupts 0 |
carlosperales95 | 25:a42a1ed4d8e9 | 149 | * |
carlosperales95 | 24:1d71dd8778c4 | 150 | **/ |
mglmx | 16:2a2da0e67793 | 151 | void interrupt0(){ |
carlosperales95 | 25:a42a1ed4d8e9 | 152 | |
mglmx | 16:2a2da0e67793 | 153 | int data = mcp->readRegister(GPIO); |
mglmx | 17:0a657e338356 | 154 | lcd.cls(); |
mglmx | 22:e4153ca757dd | 155 | lcd.printf("int0 %x",data); |
mglmx | 22:e4153ca757dd | 156 | redled = 1; |
mglmx | 22:e4153ca757dd | 157 | wait(0.2); |
mglmx | 22:e4153ca757dd | 158 | redled = 0; |
mglmx | 22:e4153ca757dd | 159 | |
mglmx | 16:2a2da0e67793 | 160 | } |
mglmx | 16:2a2da0e67793 | 161 | |
carlosperales95 | 24:1d71dd8778c4 | 162 | |
carlosperales95 | 24:1d71dd8778c4 | 163 | /** |
carlosperales95 | 24:1d71dd8778c4 | 164 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 165 | *Method to catch interrupts 1 |
carlosperales95 | 25:a42a1ed4d8e9 | 166 | * |
carlosperales95 | 24:1d71dd8778c4 | 167 | **/ |
mglmx | 16:2a2da0e67793 | 168 | void interrupt1(){ |
carlosperales95 | 25:a42a1ed4d8e9 | 169 | |
mglmx | 16:2a2da0e67793 | 170 | int data = mcp->readRegister(GPIO); |
mglmx | 17:0a657e338356 | 171 | lcd.cls(); |
mglmx | 22:e4153ca757dd | 172 | lcd.printf("int1 %x",data); |
mglmx | 22:e4153ca757dd | 173 | greenled = 1; |
mglmx | 22:e4153ca757dd | 174 | wait(0.2); |
mglmx | 22:e4153ca757dd | 175 | greenled = 0; |
mglmx | 16:2a2da0e67793 | 176 | } |
mglmx | 26:5c966a0a3e8e | 177 | |
mglmx | 26:5c966a0a3e8e | 178 | |
mglmx | 26:5c966a0a3e8e | 179 | /** |
carlosperales95 | 27:f51397917a52 | 180 | * |
carlosperales95 | 28:71bd4c83c05f | 181 | *Print the positions where there is a 0 |
carlosperales95 | 27:f51397917a52 | 182 | * |
mglmx | 26:5c966a0a3e8e | 183 | **/ |
mglmx | 26:5c966a0a3e8e | 184 | |
mglmx | 26:5c966a0a3e8e | 185 | void printZeros(unsigned int number){ |
mglmx | 26:5c966a0a3e8e | 186 | |
mglmx | 26:5c966a0a3e8e | 187 | /* |
mglmx | 26:5c966a0a3e8e | 188 | bitset <sizeof(unsigned int) * 8> n (number); |
mglmx | 26:5c966a0a3e8e | 189 | |
mglmx | 26:5c966a0a3e8e | 190 | cout << "Binary: " << n << endl; |
mglmx | 26:5c966a0a3e8e | 191 | */ |
mglmx | 26:5c966a0a3e8e | 192 | |
mglmx | 26:5c966a0a3e8e | 193 | for(int i=0; i<16;i++){ |
carlosperales95 | 27:f51397917a52 | 194 | |
carlosperales95 | 27:f51397917a52 | 195 | if(~number & 1<<i){ |
carlosperales95 | 27:f51397917a52 | 196 | |
mglmx | 26:5c966a0a3e8e | 197 | cout << "There is a 0 at " << i << endl; |
carlosperales95 | 27:f51397917a52 | 198 | pos_trains[1] = pos_trains[0]; |
carlosperales95 | 27:f51397917a52 | 199 | pos_trains[0] = i; |
mglmx | 26:5c966a0a3e8e | 200 | } |
mglmx | 26:5c966a0a3e8e | 201 | } |
mglmx | 26:5c966a0a3e8e | 202 | } |
mglmx | 5:ce0f66ea12c5 | 203 | |
mglmx | 1:0ab26889af9b | 204 | |
carlosperales95 | 24:1d71dd8778c4 | 205 | /** |
carlosperales95 | 24:1d71dd8778c4 | 206 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 207 | *Method to send DCC commands to train and switches. |
carlosperales95 | 25:a42a1ed4d8e9 | 208 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 209 | *@address - (HEX)Address where the commands will be sent |
carlosperales95 | 25:a42a1ed4d8e9 | 210 | *@inst - (HEX)Number of instruction that will be commanded |
carlosperales95 | 25:a42a1ed4d8e9 | 211 | *@repeat_count - Number of times the command will be sent |
carlosperales95 | 25:a42a1ed4d8e9 | 212 | * |
carlosperales95 | 24:1d71dd8778c4 | 213 | **/ |
mglmx | 1:0ab26889af9b | 214 | void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count) |
mglmx | 1:0ab26889af9b | 215 | { |
mglmx | 1:0ab26889af9b | 216 | unsigned __int64 command = 0x0000000000000000; // __int64 is the 64-bit integer type |
mglmx | 1:0ab26889af9b | 217 | unsigned __int64 temp_command = 0x0000000000000000; |
mglmx | 1:0ab26889af9b | 218 | unsigned __int64 prefix = 0x3FFF; // 14 "1" bits needed at start |
mglmx | 1:0ab26889af9b | 219 | unsigned int error = 0x00; //error byte |
carlosperales95 | 24:1d71dd8778c4 | 220 | |
mglmx | 1:0ab26889af9b | 221 | //calculate error detection byte with xor |
mglmx | 1:0ab26889af9b | 222 | error = address ^ inst; |
carlosperales95 | 24:1d71dd8778c4 | 223 | |
mglmx | 1:0ab26889af9b | 224 | //combine packet bits in basic DCC format |
mglmx | 1:0ab26889af9b | 225 | command = (prefix<<28)|(address<<19)|(inst<<10)|((error)<<1)|0x01; |
mglmx | 1:0ab26889af9b | 226 | //printf("\n\r %llx \n\r",command); |
carlosperales95 | 24:1d71dd8778c4 | 227 | |
mglmx | 1:0ab26889af9b | 228 | int i=0; |
carlosperales95 | 24:1d71dd8778c4 | 229 | //repeat DCC command lots of times |
mglmx | 1:0ab26889af9b | 230 | while(i < repeat_count) { |
carlosperales95 | 24:1d71dd8778c4 | 231 | |
mglmx | 1:0ab26889af9b | 232 | temp_command = command; |
carlosperales95 | 24:1d71dd8778c4 | 233 | //loops through packet bits encoding and sending out digital pulses for a DCC command |
mglmx | 1:0ab26889af9b | 234 | for (int j=0; j<64; j++) { |
carlosperales95 | 24:1d71dd8778c4 | 235 | |
carlosperales95 | 24:1d71dd8778c4 | 236 | if((temp_command&0x8000000000000000)==0) { |
carlosperales95 | 24:1d71dd8778c4 | 237 | //test packet bit |
mglmx | 1:0ab26889af9b | 238 | //send data for a "0" bit |
mglmx | 1:0ab26889af9b | 239 | Track=0; |
mglmx | 1:0ab26889af9b | 240 | wait_us(100); |
mglmx | 1:0ab26889af9b | 241 | Track=1; |
mglmx | 1:0ab26889af9b | 242 | wait_us(100); |
mglmx | 1:0ab26889af9b | 243 | //printf("0011"); |
carlosperales95 | 24:1d71dd8778c4 | 244 | |
carlosperales95 | 24:1d71dd8778c4 | 245 | }else{ |
carlosperales95 | 24:1d71dd8778c4 | 246 | |
mglmx | 1:0ab26889af9b | 247 | //send data for a "1"bit |
mglmx | 1:0ab26889af9b | 248 | Track=0; |
mglmx | 1:0ab26889af9b | 249 | wait_us(58); |
mglmx | 1:0ab26889af9b | 250 | Track=1; |
mglmx | 1:0ab26889af9b | 251 | wait_us(58); |
mglmx | 1:0ab26889af9b | 252 | //printf("01"); |
mglmx | 1:0ab26889af9b | 253 | } |
mglmx | 1:0ab26889af9b | 254 | // next bit in packet |
mglmx | 1:0ab26889af9b | 255 | temp_command = temp_command<<1; |
mglmx | 1:0ab26889af9b | 256 | } |
mglmx | 1:0ab26889af9b | 257 | i++; |
mglmx | 0:4d06a6a8e785 | 258 | } |
mglmx | 0:4d06a6a8e785 | 259 | } |
carlosperales95 | 11:021210c59a95 | 260 | |
carlosperales95 | 24:1d71dd8778c4 | 261 | |
carlosperales95 | 24:1d71dd8778c4 | 262 | /** |
carlosperales95 | 24:1d71dd8778c4 | 263 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 264 | *Method to flip the switches |
carlosperales95 | 25:a42a1ed4d8e9 | 265 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 266 | *@switchId - (1-4)The ID of the switch we want to flip |
carlosperales95 | 25:a42a1ed4d8e9 | 267 | *@times - The number of times we want to send the command |
carlosperales95 | 25:a42a1ed4d8e9 | 268 | * |
carlosperales95 | 24:1d71dd8778c4 | 269 | **/ |
mglmx | 21:e6f1649add39 | 270 | void flipSwitch(int switchId, int times){ |
mglmx | 21:e6f1649add39 | 271 | |
carlosperales95 | 25:a42a1ed4d8e9 | 272 | unsigned int SWBflip = SWBidle; //IDLE - Flip last activated SW. |
mglmx | 21:e6f1649add39 | 273 | |
mglmx | 21:e6f1649add39 | 274 | switch(switchId){ |
mglmx | 21:e6f1649add39 | 275 | case 1: |
carlosperales95 | 25:a42a1ed4d8e9 | 276 | SWBflip = SWBflip_1; //FLIP SW1 |
mglmx | 21:e6f1649add39 | 277 | break; |
mglmx | 21:e6f1649add39 | 278 | case 2: |
carlosperales95 | 25:a42a1ed4d8e9 | 279 | SWBflip = SWBflip_2; //FLIP SW2 |
mglmx | 21:e6f1649add39 | 280 | break; |
mglmx | 21:e6f1649add39 | 281 | case 3: |
carlosperales95 | 25:a42a1ed4d8e9 | 282 | SWBflip = SWBflip_3; //FLIP SW3 |
mglmx | 21:e6f1649add39 | 283 | break; |
mglmx | 21:e6f1649add39 | 284 | case 4: |
carlosperales95 | 25:a42a1ed4d8e9 | 285 | SWBflip = SWBflip_4; //FLIP SW4 |
mglmx | 21:e6f1649add39 | 286 | break; |
mglmx | 21:e6f1649add39 | 287 | default: |
carlosperales95 | 24:1d71dd8778c4 | 288 | break; |
mglmx | 21:e6f1649add39 | 289 | } |
carlosperales95 | 11:021210c59a95 | 290 | |
mglmx | 21:e6f1649add39 | 291 | //Security measure not to burn the switch. |
carlosperales95 | 25:a42a1ed4d8e9 | 292 | if(times <=5){ DCC_send_command(SWBaddress,SWBflip,times); } |
mglmx | 21:e6f1649add39 | 293 | |
mglmx | 21:e6f1649add39 | 294 | } |
mglmx | 21:e6f1649add39 | 295 | |
mglmx | 22:e4153ca757dd | 296 | |
carlosperales95 | 24:1d71dd8778c4 | 297 | /** |
carlosperales95 | 24:1d71dd8778c4 | 298 | * |
carlosperales95 | 29:559eb2164488 | 299 | *Checks if any of the switches of the box has been activated. |
carlosperales95 | 29:559eb2164488 | 300 | *Calls necessary function and displays LCD text. |
carlosperales95 | 25:a42a1ed4d8e9 | 301 | * |
carlosperales95 | 24:1d71dd8778c4 | 302 | **/ |
mglmx | 21:e6f1649add39 | 303 | void checkSwitch(){ |
carlosperales95 | 24:1d71dd8778c4 | 304 | |
mglmx | 22:e4153ca757dd | 305 | if(switch1 == 1){ |
carlosperales95 | 24:1d71dd8778c4 | 306 | |
mglmx | 22:e4153ca757dd | 307 | lcd.cls(); |
mglmx | 22:e4153ca757dd | 308 | lcd.printf("Switch 1 ON - SW1"); |
carlosperales95 | 25:a42a1ed4d8e9 | 309 | flipSwitch(1,5); |
carlosperales95 | 24:1d71dd8778c4 | 310 | |
mglmx | 22:e4153ca757dd | 311 | }else if(switch2 == 1){ |
carlosperales95 | 24:1d71dd8778c4 | 312 | |
carlosperales95 | 24:1d71dd8778c4 | 313 | lcd.cls(); |
carlosperales95 | 24:1d71dd8778c4 | 314 | lcd.printf("Switch 2 ON - SW2"); |
carlosperales95 | 24:1d71dd8778c4 | 315 | flipSwitch(2,5); |
carlosperales95 | 24:1d71dd8778c4 | 316 | |
carlosperales95 | 24:1d71dd8778c4 | 317 | }else if(switch3 == 0){ |
carlosperales95 | 24:1d71dd8778c4 | 318 | |
carlosperales95 | 24:1d71dd8778c4 | 319 | lcd.cls(); |
carlosperales95 | 24:1d71dd8778c4 | 320 | lcd.printf("Switch 3 ON - SW3"); |
carlosperales95 | 24:1d71dd8778c4 | 321 | flipSwitch(3,5); |
carlosperales95 | 24:1d71dd8778c4 | 322 | |
carlosperales95 | 24:1d71dd8778c4 | 323 | }else if(switch4 == 0){ |
carlosperales95 | 24:1d71dd8778c4 | 324 | |
carlosperales95 | 24:1d71dd8778c4 | 325 | lcd.cls(); |
carlosperales95 | 24:1d71dd8778c4 | 326 | lcd.printf("Switch 4 ON - IDLE"); |
carlosperales95 | 24:1d71dd8778c4 | 327 | flipSwitch(0,5); |
carlosperales95 | 24:1d71dd8778c4 | 328 | } |
mglmx | 22:e4153ca757dd | 329 | } |
mglmx | 22:e4153ca757dd | 330 | |
carlosperales95 | 24:1d71dd8778c4 | 331 | |
carlosperales95 | 24:1d71dd8778c4 | 332 | |
carlosperales95 | 11:021210c59a95 | 333 | //**************** MAIN PROGRAM FOR DENVER TRAIN ****************// |
carlosperales95 | 11:021210c59a95 | 334 | |
mglmx | 1:0ab26889af9b | 335 | int main() |
mglmx | 1:0ab26889af9b | 336 | { |
carlosperales95 | 25:a42a1ed4d8e9 | 337 | //RISE FOR INTERRUPTS?? NOT WORKING ATM |
carlosperales95 | 25:a42a1ed4d8e9 | 338 | //int0.rise(&interrupt0); |
carlosperales95 | 25:a42a1ed4d8e9 | 339 | //int1.rise(&interrupt1); |
carlosperales95 | 25:a42a1ed4d8e9 | 340 | |
carlosperales95 | 25:a42a1ed4d8e9 | 341 | //Read and display potentiometer |
carlosperales95 | 25:a42a1ed4d8e9 | 342 | //float f = pot.read(); |
carlosperales95 | 25:a42a1ed4d8e9 | 343 | //float vin = f * 3.3; |
carlosperales95 | 25:a42a1ed4d8e9 | 344 | //lcd.printf("vin: %.4f",vin); |
carlosperales95 | 25:a42a1ed4d8e9 | 345 | |
carlosperales95 | 25:a42a1ed4d8e9 | 346 | //0xFFFC //1111111111111100 |
carlosperales95 | 25:a42a1ed4d8e9 | 347 | |
carlosperales95 | 25:a42a1ed4d8e9 | 348 | |
carlosperales95 | 25:a42a1ed4d8e9 | 349 | //Led routine to start main program |
mglmx | 2:f580707c44fa | 350 | led1 = 1; |
mglmx | 22:e4153ca757dd | 351 | wait(0.2); |
mglmx | 2:f580707c44fa | 352 | led1 = 0; |
mglmx | 22:e4153ca757dd | 353 | wait(0.2); |
mglmx | 2:f580707c44fa | 354 | led1 = 1; |
mglmx | 16:2a2da0e67793 | 355 | |
carlosperales95 | 25:a42a1ed4d8e9 | 356 | initialize_mcp(); //mcp initialization for interrupts before train running |
mglmx | 16:2a2da0e67793 | 357 | |
carlosperales95 | 25:a42a1ed4d8e9 | 358 | //Train light routine to start running |
mglmx | 1:0ab26889af9b | 359 | DCC_send_command(DCCaddress,DCC_func_lighton,200); // turn light on full |
carlosperales95 | 25:a42a1ed4d8e9 | 360 | DCC_send_command(DCCaddress,DCC_func_dimlight,400); //dim light |
mglmx | 1:0ab26889af9b | 361 | DCC_send_command(DCCaddress,DCC_func_lighton,200); //light full again |
mglmx | 22:e4153ca757dd | 362 | |
carlosperales95 | 25:a42a1ed4d8e9 | 363 | //LED3 Shows start of route + LCD notif |
mglmx | 22:e4153ca757dd | 364 | led3 = 1; // Entering the while |
mglmx | 22:e4153ca757dd | 365 | lcd.cls(); |
mglmx | 22:e4153ca757dd | 366 | lcd.printf("Ready to start"); |
carlosperales95 | 19:ff21ba3a4dc5 | 367 | |
carlosperales95 | 14:7bb998edd819 | 368 | //Demo for stopping at the station |
mglmx | 1:0ab26889af9b | 369 | while(1) { |
mglmx | 4:50879dfb82d5 | 370 | |
carlosperales95 | 25:a42a1ed4d8e9 | 371 | checkSwitch(); //Checks for switch commands everytime. |
carlosperales95 | 25:a42a1ed4d8e9 | 372 | |
carlosperales95 | 29:559eb2164488 | 373 | if(station == 1){ //If train is on the sensor at the middle of the station it stops and displays LCD text. |
carlosperales95 | 14:7bb998edd819 | 374 | |
mglmx | 4:50879dfb82d5 | 375 | lcd.cls(); |
carlosperales95 | 29:559eb2164488 | 376 | lcd.printf("All aboard\n mind the gap"); |
carlosperales95 | 25:a42a1ed4d8e9 | 377 | DCC_send_command(DCCaddress,DCCinst_stop,400); |
carlosperales95 | 29:559eb2164488 | 378 | lcd.cls(); |
mglmx | 4:50879dfb82d5 | 379 | |
mglmx | 22:e4153ca757dd | 380 | }else{ |
carlosperales95 | 14:7bb998edd819 | 381 | DCC_send_command(DCCaddress,DCCinst_forward,1); // Forward half speed train address 3 |
carlosperales95 | 25:a42a1ed4d8e9 | 382 | } |
mglmx | 1:0ab26889af9b | 383 | } |
mglmx | 3:fe7010b693a0 | 384 | } |