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