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@26:5c966a0a3e8e, 2018-06-11 (annotated)
- Committer:
- mglmx
- Date:
- Mon Jun 11 14:34:34 2018 +0000
- Revision:
- 26:5c966a0a3e8e
- Parent:
- 25:a42a1ed4d8e9
- Child:
- 27:f51397917a52
Added function to check position of 0s
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 | 26:5c966a0a3e8e | 175 | |
mglmx | 26:5c966a0a3e8e | 176 | |
mglmx | 26:5c966a0a3e8e | 177 | /** |
mglmx | 26:5c966a0a3e8e | 178 | Prints the positions where there is a 0 |
mglmx | 26:5c966a0a3e8e | 179 | **/ |
mglmx | 26:5c966a0a3e8e | 180 | |
mglmx | 26:5c966a0a3e8e | 181 | void printZeros(unsigned int number){ |
mglmx | 26:5c966a0a3e8e | 182 | |
mglmx | 26:5c966a0a3e8e | 183 | /* |
mglmx | 26:5c966a0a3e8e | 184 | bitset <sizeof(unsigned int) * 8> n (number); |
mglmx | 26:5c966a0a3e8e | 185 | |
mglmx | 26:5c966a0a3e8e | 186 | cout << "Binary: " << n << endl; |
mglmx | 26:5c966a0a3e8e | 187 | */ |
mglmx | 26:5c966a0a3e8e | 188 | |
mglmx | 26:5c966a0a3e8e | 189 | for(int i=0; i<16;i++){ |
mglmx | 26:5c966a0a3e8e | 190 | if(~n2 & 1<<i){ |
mglmx | 26:5c966a0a3e8e | 191 | cout << "There is a 0 at " << i << endl; |
mglmx | 26:5c966a0a3e8e | 192 | } |
mglmx | 26:5c966a0a3e8e | 193 | } |
mglmx | 26:5c966a0a3e8e | 194 | |
mglmx | 26:5c966a0a3e8e | 195 | } |
mglmx | 5:ce0f66ea12c5 | 196 | |
mglmx | 1:0ab26889af9b | 197 | |
carlosperales95 | 24:1d71dd8778c4 | 198 | /** |
carlosperales95 | 24:1d71dd8778c4 | 199 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 200 | *Method to send DCC commands to train and switches. |
carlosperales95 | 25:a42a1ed4d8e9 | 201 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 202 | *@address - (HEX)Address where the commands will be sent |
carlosperales95 | 25:a42a1ed4d8e9 | 203 | *@inst - (HEX)Number of instruction that will be commanded |
carlosperales95 | 25:a42a1ed4d8e9 | 204 | *@repeat_count - Number of times the command will be sent |
carlosperales95 | 25:a42a1ed4d8e9 | 205 | * |
carlosperales95 | 24:1d71dd8778c4 | 206 | **/ |
mglmx | 1:0ab26889af9b | 207 | void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count) |
mglmx | 1:0ab26889af9b | 208 | { |
mglmx | 1:0ab26889af9b | 209 | unsigned __int64 command = 0x0000000000000000; // __int64 is the 64-bit integer type |
mglmx | 1:0ab26889af9b | 210 | unsigned __int64 temp_command = 0x0000000000000000; |
mglmx | 1:0ab26889af9b | 211 | unsigned __int64 prefix = 0x3FFF; // 14 "1" bits needed at start |
mglmx | 1:0ab26889af9b | 212 | unsigned int error = 0x00; //error byte |
carlosperales95 | 24:1d71dd8778c4 | 213 | |
mglmx | 1:0ab26889af9b | 214 | //calculate error detection byte with xor |
mglmx | 1:0ab26889af9b | 215 | error = address ^ inst; |
carlosperales95 | 24:1d71dd8778c4 | 216 | |
mglmx | 1:0ab26889af9b | 217 | //combine packet bits in basic DCC format |
mglmx | 1:0ab26889af9b | 218 | command = (prefix<<28)|(address<<19)|(inst<<10)|((error)<<1)|0x01; |
mglmx | 1:0ab26889af9b | 219 | //printf("\n\r %llx \n\r",command); |
carlosperales95 | 24:1d71dd8778c4 | 220 | |
mglmx | 1:0ab26889af9b | 221 | int i=0; |
carlosperales95 | 24:1d71dd8778c4 | 222 | //repeat DCC command lots of times |
mglmx | 1:0ab26889af9b | 223 | while(i < repeat_count) { |
carlosperales95 | 24:1d71dd8778c4 | 224 | |
mglmx | 1:0ab26889af9b | 225 | temp_command = command; |
carlosperales95 | 24:1d71dd8778c4 | 226 | //loops through packet bits encoding and sending out digital pulses for a DCC command |
mglmx | 1:0ab26889af9b | 227 | for (int j=0; j<64; j++) { |
carlosperales95 | 24:1d71dd8778c4 | 228 | |
carlosperales95 | 24:1d71dd8778c4 | 229 | if((temp_command&0x8000000000000000)==0) { |
carlosperales95 | 24:1d71dd8778c4 | 230 | //test packet bit |
mglmx | 1:0ab26889af9b | 231 | //send data for a "0" bit |
mglmx | 1:0ab26889af9b | 232 | Track=0; |
mglmx | 1:0ab26889af9b | 233 | wait_us(100); |
mglmx | 1:0ab26889af9b | 234 | Track=1; |
mglmx | 1:0ab26889af9b | 235 | wait_us(100); |
mglmx | 1:0ab26889af9b | 236 | //printf("0011"); |
carlosperales95 | 24:1d71dd8778c4 | 237 | |
carlosperales95 | 24:1d71dd8778c4 | 238 | }else{ |
carlosperales95 | 24:1d71dd8778c4 | 239 | |
mglmx | 1:0ab26889af9b | 240 | //send data for a "1"bit |
mglmx | 1:0ab26889af9b | 241 | Track=0; |
mglmx | 1:0ab26889af9b | 242 | wait_us(58); |
mglmx | 1:0ab26889af9b | 243 | Track=1; |
mglmx | 1:0ab26889af9b | 244 | wait_us(58); |
mglmx | 1:0ab26889af9b | 245 | //printf("01"); |
mglmx | 1:0ab26889af9b | 246 | } |
mglmx | 1:0ab26889af9b | 247 | // next bit in packet |
mglmx | 1:0ab26889af9b | 248 | temp_command = temp_command<<1; |
mglmx | 1:0ab26889af9b | 249 | } |
mglmx | 1:0ab26889af9b | 250 | i++; |
mglmx | 0:4d06a6a8e785 | 251 | } |
mglmx | 0:4d06a6a8e785 | 252 | } |
carlosperales95 | 11:021210c59a95 | 253 | |
carlosperales95 | 24:1d71dd8778c4 | 254 | |
carlosperales95 | 24:1d71dd8778c4 | 255 | /** |
carlosperales95 | 24:1d71dd8778c4 | 256 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 257 | *Method to flip the switches |
carlosperales95 | 25:a42a1ed4d8e9 | 258 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 259 | *@switchId - (1-4)The ID of the switch we want to flip |
carlosperales95 | 25:a42a1ed4d8e9 | 260 | *@times - The number of times we want to send the command |
carlosperales95 | 25:a42a1ed4d8e9 | 261 | * |
carlosperales95 | 24:1d71dd8778c4 | 262 | **/ |
mglmx | 21:e6f1649add39 | 263 | void flipSwitch(int switchId, int times){ |
mglmx | 21:e6f1649add39 | 264 | |
carlosperales95 | 25:a42a1ed4d8e9 | 265 | unsigned int SWBflip = SWBidle; //IDLE - Flip last activated SW. |
mglmx | 21:e6f1649add39 | 266 | |
mglmx | 21:e6f1649add39 | 267 | switch(switchId){ |
mglmx | 21:e6f1649add39 | 268 | case 1: |
carlosperales95 | 25:a42a1ed4d8e9 | 269 | SWBflip = SWBflip_1; //FLIP SW1 |
mglmx | 21:e6f1649add39 | 270 | break; |
mglmx | 21:e6f1649add39 | 271 | case 2: |
carlosperales95 | 25:a42a1ed4d8e9 | 272 | SWBflip = SWBflip_2; //FLIP SW2 |
mglmx | 21:e6f1649add39 | 273 | break; |
mglmx | 21:e6f1649add39 | 274 | case 3: |
carlosperales95 | 25:a42a1ed4d8e9 | 275 | SWBflip = SWBflip_3; //FLIP SW3 |
mglmx | 21:e6f1649add39 | 276 | break; |
mglmx | 21:e6f1649add39 | 277 | case 4: |
carlosperales95 | 25:a42a1ed4d8e9 | 278 | SWBflip = SWBflip_4; //FLIP SW4 |
mglmx | 21:e6f1649add39 | 279 | break; |
mglmx | 21:e6f1649add39 | 280 | default: |
carlosperales95 | 24:1d71dd8778c4 | 281 | break; |
mglmx | 21:e6f1649add39 | 282 | } |
carlosperales95 | 11:021210c59a95 | 283 | |
mglmx | 21:e6f1649add39 | 284 | //Security measure not to burn the switch. |
carlosperales95 | 25:a42a1ed4d8e9 | 285 | if(times <=5){ DCC_send_command(SWBaddress,SWBflip,times); } |
mglmx | 21:e6f1649add39 | 286 | |
mglmx | 21:e6f1649add39 | 287 | } |
mglmx | 21:e6f1649add39 | 288 | |
mglmx | 22:e4153ca757dd | 289 | |
carlosperales95 | 24:1d71dd8778c4 | 290 | /** |
carlosperales95 | 25:a42a1ed4d8e9 | 291 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 292 | *Check the swithces of the box. If they have 1 value the railSiwtches flip. |
carlosperales95 | 25:a42a1ed4d8e9 | 293 | * |
carlosperales95 | 24:1d71dd8778c4 | 294 | **/ |
mglmx | 22:e4153ca757dd | 295 | void switching(string sw1,string sw2, string sw3, string sw4){ |
mglmx | 22:e4153ca757dd | 296 | string strInst = "1000" + sw1 + sw2 + sw3 + sw4; |
mglmx | 22:e4153ca757dd | 297 | |
mglmx | 22:e4153ca757dd | 298 | //unsigned int inst = stoi(strInst); |
mglmx | 22:e4153ca757dd | 299 | //DCC_send_command(0x06,inst,5); |
carlosperales95 | 24:1d71dd8778c4 | 300 | |
mglmx | 22:e4153ca757dd | 301 | } |
mglmx | 22:e4153ca757dd | 302 | |
carlosperales95 | 24:1d71dd8778c4 | 303 | |
carlosperales95 | 24:1d71dd8778c4 | 304 | /** |
carlosperales95 | 24:1d71dd8778c4 | 305 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 306 | * |
carlosperales95 | 25:a42a1ed4d8e9 | 307 | * |
carlosperales95 | 24:1d71dd8778c4 | 308 | **/ |
mglmx | 21:e6f1649add39 | 309 | void checkSwitch(){ |
carlosperales95 | 24:1d71dd8778c4 | 310 | |
mglmx | 22:e4153ca757dd | 311 | if(switch1 == 1){ |
carlosperales95 | 24:1d71dd8778c4 | 312 | |
mglmx | 22:e4153ca757dd | 313 | lcd.cls(); |
mglmx | 22:e4153ca757dd | 314 | lcd.printf("Switch 1 ON - SW1"); |
carlosperales95 | 25:a42a1ed4d8e9 | 315 | flipSwitch(1,5); |
carlosperales95 | 24:1d71dd8778c4 | 316 | |
mglmx | 22:e4153ca757dd | 317 | }else if(switch2 == 1){ |
carlosperales95 | 24:1d71dd8778c4 | 318 | |
carlosperales95 | 24:1d71dd8778c4 | 319 | lcd.cls(); |
carlosperales95 | 24:1d71dd8778c4 | 320 | lcd.printf("Switch 2 ON - SW2"); |
carlosperales95 | 24:1d71dd8778c4 | 321 | flipSwitch(2,5); |
carlosperales95 | 24:1d71dd8778c4 | 322 | |
carlosperales95 | 24:1d71dd8778c4 | 323 | }else if(switch3 == 0){ |
carlosperales95 | 24:1d71dd8778c4 | 324 | |
carlosperales95 | 24:1d71dd8778c4 | 325 | lcd.cls(); |
carlosperales95 | 24:1d71dd8778c4 | 326 | lcd.printf("Switch 3 ON - SW3"); |
carlosperales95 | 24:1d71dd8778c4 | 327 | flipSwitch(3,5); |
carlosperales95 | 24:1d71dd8778c4 | 328 | |
carlosperales95 | 24:1d71dd8778c4 | 329 | }else if(switch4 == 0){ |
carlosperales95 | 24:1d71dd8778c4 | 330 | |
carlosperales95 | 24:1d71dd8778c4 | 331 | lcd.cls(); |
carlosperales95 | 24:1d71dd8778c4 | 332 | lcd.printf("Switch 4 ON - IDLE"); |
carlosperales95 | 24:1d71dd8778c4 | 333 | flipSwitch(0,5); |
carlosperales95 | 24:1d71dd8778c4 | 334 | } |
mglmx | 22:e4153ca757dd | 335 | } |
mglmx | 22:e4153ca757dd | 336 | |
carlosperales95 | 24:1d71dd8778c4 | 337 | |
carlosperales95 | 24:1d71dd8778c4 | 338 | |
carlosperales95 | 11:021210c59a95 | 339 | //**************** MAIN PROGRAM FOR DENVER TRAIN ****************// |
carlosperales95 | 11:021210c59a95 | 340 | |
mglmx | 1:0ab26889af9b | 341 | int main() |
mglmx | 1:0ab26889af9b | 342 | { |
carlosperales95 | 25:a42a1ed4d8e9 | 343 | //RISE FOR INTERRUPTS?? NOT WORKING ATM |
carlosperales95 | 25:a42a1ed4d8e9 | 344 | //int0.rise(&interrupt0); |
carlosperales95 | 25:a42a1ed4d8e9 | 345 | //int1.rise(&interrupt1); |
carlosperales95 | 25:a42a1ed4d8e9 | 346 | |
carlosperales95 | 25:a42a1ed4d8e9 | 347 | //Read and display potentiometer |
carlosperales95 | 25:a42a1ed4d8e9 | 348 | //float f = pot.read(); |
carlosperales95 | 25:a42a1ed4d8e9 | 349 | //float vin = f * 3.3; |
carlosperales95 | 25:a42a1ed4d8e9 | 350 | //lcd.printf("vin: %.4f",vin); |
carlosperales95 | 25:a42a1ed4d8e9 | 351 | |
carlosperales95 | 25:a42a1ed4d8e9 | 352 | //0xFFFC //1111111111111100 |
carlosperales95 | 25:a42a1ed4d8e9 | 353 | |
carlosperales95 | 25:a42a1ed4d8e9 | 354 | |
carlosperales95 | 25:a42a1ed4d8e9 | 355 | //Led routine to start main program |
mglmx | 2:f580707c44fa | 356 | led1 = 1; |
mglmx | 22:e4153ca757dd | 357 | wait(0.2); |
mglmx | 2:f580707c44fa | 358 | led1 = 0; |
mglmx | 22:e4153ca757dd | 359 | wait(0.2); |
mglmx | 2:f580707c44fa | 360 | led1 = 1; |
mglmx | 16:2a2da0e67793 | 361 | |
carlosperales95 | 25:a42a1ed4d8e9 | 362 | initialize_mcp(); //mcp initialization for interrupts before train running |
mglmx | 16:2a2da0e67793 | 363 | |
carlosperales95 | 25:a42a1ed4d8e9 | 364 | //Train light routine to start running |
mglmx | 1:0ab26889af9b | 365 | DCC_send_command(DCCaddress,DCC_func_lighton,200); // turn light on full |
carlosperales95 | 25:a42a1ed4d8e9 | 366 | DCC_send_command(DCCaddress,DCC_func_dimlight,400); //dim light |
mglmx | 1:0ab26889af9b | 367 | DCC_send_command(DCCaddress,DCC_func_lighton,200); //light full again |
mglmx | 22:e4153ca757dd | 368 | |
carlosperales95 | 25:a42a1ed4d8e9 | 369 | //LED3 Shows start of route + LCD notif |
mglmx | 22:e4153ca757dd | 370 | led3 = 1; // Entering the while |
mglmx | 22:e4153ca757dd | 371 | lcd.cls(); |
mglmx | 22:e4153ca757dd | 372 | lcd.printf("Ready to start"); |
carlosperales95 | 19:ff21ba3a4dc5 | 373 | |
carlosperales95 | 14:7bb998edd819 | 374 | //Demo for stopping at the station |
mglmx | 1:0ab26889af9b | 375 | while(1) { |
mglmx | 4:50879dfb82d5 | 376 | |
carlosperales95 | 25:a42a1ed4d8e9 | 377 | checkSwitch(); //Checks for switch commands everytime. |
carlosperales95 | 25:a42a1ed4d8e9 | 378 | |
carlosperales95 | 25:a42a1ed4d8e9 | 379 | if(d21 == 1 || d22 == 1 || d23 == 1){ //If train in any of the 3 sensors of station it stops. |
carlosperales95 | 14:7bb998edd819 | 380 | |
mglmx | 4:50879dfb82d5 | 381 | lcd.cls(); |
mglmx | 4:50879dfb82d5 | 382 | lcd.printf("Choo Choo station"); |
carlosperales95 | 25:a42a1ed4d8e9 | 383 | DCC_send_command(DCCaddress,DCCinst_stop,400); |
mglmx | 4:50879dfb82d5 | 384 | |
mglmx | 22:e4153ca757dd | 385 | }else{ |
carlosperales95 | 14:7bb998edd819 | 386 | DCC_send_command(DCCaddress,DCCinst_forward,1); // Forward half speed train address 3 |
carlosperales95 | 25:a42a1ed4d8e9 | 387 | } |
mglmx | 1:0ab26889af9b | 388 | } |
mglmx | 3:fe7010b693a0 | 389 | } |