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@24:1d71dd8778c4, 2018-06-11 (annotated)
- Committer:
- carlosperales95
- Date:
- Mon Jun 11 12:31:56 2018 +0000
- Revision:
- 24:1d71dd8778c4
- Parent:
- 23:3fe9e76e06f2
- Child:
- 25:a42a1ed4d8e9
Fixed comments and code and some tabs
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 | |
mglmx | 21:e6f1649add39 | 11 | |
mglmx | 21:e6f1649add39 | 12 | //SWITCHES p5 - p8 |
mglmx | 21:e6f1649add39 | 13 | DigitalIn switch1(p5); |
mglmx | 21:e6f1649add39 | 14 | DigitalIn switch2(p6); |
mglmx | 21:e6f1649add39 | 15 | DigitalIn switch3(p7); |
mglmx | 21:e6f1649add39 | 16 | DigitalIn switch4(p8); |
carlosperales95 | 7:e2b8461d4f05 | 17 | |
carlosperales95 | 10:2088b1935a93 | 18 | //RAIL SENSORS - INT0,INT1 |
carlosperales95 | 7:e2b8461d4f05 | 19 | //INT0 - p9 |
mglmx | 18:aa43bb62e60f | 20 | InterruptIn int0(p9); |
carlosperales95 | 7:e2b8461d4f05 | 21 | //INT1 - p10 |
mglmx | 18:aa43bb62e60f | 22 | InterruptIn int1(p10); |
carlosperales95 | 7:e2b8461d4f05 | 23 | |
carlosperales95 | 7:e2b8461d4f05 | 24 | ///p11 |
carlosperales95 | 7:e2b8461d4f05 | 25 | ///p12 |
carlosperales95 | 12:e914ca5cd44b | 26 | |
carlosperales95 | 7:e2b8461d4f05 | 27 | //M0 - p13 |
carlosperales95 | 7:e2b8461d4f05 | 28 | DigitalIn d21(p13); |
carlosperales95 | 7:e2b8461d4f05 | 29 | //M1 - p14 |
carlosperales95 | 7:e2b8461d4f05 | 30 | DigitalIn d22(p14); |
carlosperales95 | 7:e2b8461d4f05 | 31 | //M2 - p15 |
carlosperales95 | 7:e2b8461d4f05 | 32 | DigitalIn d23(p15); |
carlosperales95 | 12:e914ca5cd44b | 33 | |
carlosperales95 | 7:e2b8461d4f05 | 34 | //p16 |
carlosperales95 | 12:e914ca5cd44b | 35 | //p17 |
carlosperales95 | 12:e914ca5cd44b | 36 | |
carlosperales95 | 11:021210c59a95 | 37 | //BUZZER - p18 |
carlosperales95 | 11:021210c59a95 | 38 | DigitalOut buzz(p18); // buzz=0 doesn't beep, buzz=1 beeps |
carlosperales95 | 7:e2b8461d4f05 | 39 | |
carlosperales95 | 7:e2b8461d4f05 | 40 | //POTENTIOMETER - p19 |
carlosperales95 | 13:dbf1ead12cee | 41 | AnalogIn pot(p19); //Gives float value pot.read(). Convert analog input to V with f*3.3 |
carlosperales95 | 7:e2b8461d4f05 | 42 | |
carlosperales95 | 7:e2b8461d4f05 | 43 | //DAT - p20 |
mglmx | 3:fe7010b693a0 | 44 | DigitalOut Track(p20); //Digital output bit used to drive track power via H-bridge |
carlosperales95 | 7:e2b8461d4f05 | 45 | |
carlosperales95 | 7:e2b8461d4f05 | 46 | //LCD SCREEN - p21, p22, p23, p24, p25, p26 |
carlosperales95 | 11:021210c59a95 | 47 | 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 | 48 | |
carlosperales95 | 7:e2b8461d4f05 | 49 | ///p27 |
carlosperales95 | 7:e2b8461d4f05 | 50 | ///p28 |
carlosperales95 | 7:e2b8461d4f05 | 51 | |
carlosperales95 | 7:e2b8461d4f05 | 52 | //LED1 - p29 |
mglmx | 3:fe7010b693a0 | 53 | DigitalOut redled(p29); |
carlosperales95 | 7:e2b8461d4f05 | 54 | //LED2 - p30 |
mglmx | 3:fe7010b693a0 | 55 | DigitalOut greenled(p30); |
carlosperales95 | 7:e2b8461d4f05 | 56 | |
carlosperales95 | 24:1d71dd8778c4 | 57 | //MBED LEDS |
mglmx | 18:aa43bb62e60f | 58 | DigitalOut led1(LED1); |
mglmx | 18:aa43bb62e60f | 59 | DigitalOut led2(LED2); |
mglmx | 18:aa43bb62e60f | 60 | DigitalOut led3(LED3); |
mglmx | 16:2a2da0e67793 | 61 | |
mglmx | 16:2a2da0e67793 | 62 | //MCP |
mglmx | 16:2a2da0e67793 | 63 | MCP23017 *mcp; |
carlosperales95 | 12:e914ca5cd44b | 64 | |
carlosperales95 | 24:1d71dd8778c4 | 65 | |
carlosperales95 | 24:1d71dd8778c4 | 66 | |
carlosperales95 | 11:021210c59a95 | 67 | //**************** FUNCTIONS FOR DENVER TRAIN ****************// |
carlosperales95 | 10:2088b1935a93 | 68 | |
mglmx | 18:aa43bb62e60f | 69 | |
mglmx | 22:e4153ca757dd | 70 | /** |
mglmx | 22:e4153ca757dd | 71 | * Activates the buzzer for 0.5 seconds. |
mglmx | 22:e4153ca757dd | 72 | **/ |
mglmx | 22:e4153ca757dd | 73 | void doBuzz(){ |
mglmx | 22:e4153ca757dd | 74 | buzz = 1; |
mglmx | 22:e4153ca757dd | 75 | wait(0.5); |
mglmx | 22:e4153ca757dd | 76 | buzz = 0; |
mglmx | 22:e4153ca757dd | 77 | } |
mglmx | 18:aa43bb62e60f | 78 | |
carlosperales95 | 24:1d71dd8778c4 | 79 | |
mglmx | 22:e4153ca757dd | 80 | /** |
carlosperales95 | 24:1d71dd8778c4 | 81 | * |
carlosperales95 | 24:1d71dd8778c4 | 82 | **/ |
mglmx | 18:aa43bb62e60f | 83 | void initialize_mcp(){ |
mglmx | 18:aa43bb62e60f | 84 | mcp = new MCP23017(p28,p27,0x40); //Connect to SCL - p28 and SDA - p27 and MPC I2C address 0x40 |
mglmx | 18:aa43bb62e60f | 85 | |
mglmx | 18:aa43bb62e60f | 86 | mcp->reset(); |
mglmx | 18:aa43bb62e60f | 87 | mcp->writeRegister(0x00, (unsigned char )0xff); |
mglmx | 18:aa43bb62e60f | 88 | mcp->writeRegister(0x01, (unsigned char )0xff); |
mglmx | 18:aa43bb62e60f | 89 | mcp->writeRegister(0x02, (unsigned char )0x00); |
mglmx | 18:aa43bb62e60f | 90 | mcp->writeRegister(0x03, (unsigned char )0x00); |
mglmx | 18:aa43bb62e60f | 91 | mcp->writeRegister(0x04, (unsigned char )0xff); |
mglmx | 18:aa43bb62e60f | 92 | mcp->writeRegister(0x05, (unsigned char )0xff); |
mglmx | 18:aa43bb62e60f | 93 | mcp->writeRegister(0x06, (unsigned char )0xff); |
mglmx | 18:aa43bb62e60f | 94 | mcp->writeRegister(0x07, (unsigned char )0xff); |
mglmx | 18:aa43bb62e60f | 95 | mcp->writeRegister(0x08, (unsigned char )0xff); |
mglmx | 18:aa43bb62e60f | 96 | mcp->writeRegister(0x09, (unsigned char )0xff); |
mglmx | 18:aa43bb62e60f | 97 | mcp->writeRegister(0x0a, (unsigned char )0x42); |
mglmx | 18:aa43bb62e60f | 98 | mcp->writeRegister(0x0b, (unsigned char )0x42); |
mglmx | 18:aa43bb62e60f | 99 | mcp->writeRegister(0x0c, (unsigned char )0x00); |
mglmx | 18:aa43bb62e60f | 100 | mcp->writeRegister(0x0d, (unsigned char )0x00); |
mglmx | 18:aa43bb62e60f | 101 | } |
mglmx | 18:aa43bb62e60f | 102 | |
carlosperales95 | 24:1d71dd8778c4 | 103 | |
carlosperales95 | 24:1d71dd8778c4 | 104 | /** |
carlosperales95 | 24:1d71dd8778c4 | 105 | * |
carlosperales95 | 24:1d71dd8778c4 | 106 | **/ |
mglmx | 16:2a2da0e67793 | 107 | void interrupt0(){ |
mglmx | 16:2a2da0e67793 | 108 | int data = mcp->readRegister(GPIO); |
mglmx | 17:0a657e338356 | 109 | lcd.cls(); |
mglmx | 22:e4153ca757dd | 110 | lcd.printf("int0 %x",data); |
mglmx | 22:e4153ca757dd | 111 | redled = 1; |
mglmx | 22:e4153ca757dd | 112 | wait(0.2); |
mglmx | 22:e4153ca757dd | 113 | redled = 0; |
mglmx | 22:e4153ca757dd | 114 | |
mglmx | 16:2a2da0e67793 | 115 | } |
mglmx | 16:2a2da0e67793 | 116 | |
carlosperales95 | 24:1d71dd8778c4 | 117 | |
carlosperales95 | 24:1d71dd8778c4 | 118 | /** |
carlosperales95 | 24:1d71dd8778c4 | 119 | * |
carlosperales95 | 24:1d71dd8778c4 | 120 | **/ |
mglmx | 16:2a2da0e67793 | 121 | void interrupt1(){ |
mglmx | 16:2a2da0e67793 | 122 | int data = mcp->readRegister(GPIO); |
mglmx | 17:0a657e338356 | 123 | lcd.cls(); |
mglmx | 22:e4153ca757dd | 124 | lcd.printf("int1 %x",data); |
mglmx | 22:e4153ca757dd | 125 | greenled = 1; |
mglmx | 22:e4153ca757dd | 126 | wait(0.2); |
mglmx | 22:e4153ca757dd | 127 | greenled = 0; |
mglmx | 16:2a2da0e67793 | 128 | } |
mglmx | 5:ce0f66ea12c5 | 129 | |
mglmx | 1:0ab26889af9b | 130 | |
carlosperales95 | 24:1d71dd8778c4 | 131 | /** |
carlosperales95 | 24:1d71dd8778c4 | 132 | * |
carlosperales95 | 24:1d71dd8778c4 | 133 | **/ |
mglmx | 1:0ab26889af9b | 134 | void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count) |
mglmx | 1:0ab26889af9b | 135 | { |
mglmx | 1:0ab26889af9b | 136 | unsigned __int64 command = 0x0000000000000000; // __int64 is the 64-bit integer type |
mglmx | 1:0ab26889af9b | 137 | unsigned __int64 temp_command = 0x0000000000000000; |
mglmx | 1:0ab26889af9b | 138 | unsigned __int64 prefix = 0x3FFF; // 14 "1" bits needed at start |
mglmx | 1:0ab26889af9b | 139 | unsigned int error = 0x00; //error byte |
carlosperales95 | 24:1d71dd8778c4 | 140 | |
mglmx | 1:0ab26889af9b | 141 | //calculate error detection byte with xor |
mglmx | 1:0ab26889af9b | 142 | error = address ^ inst; |
carlosperales95 | 24:1d71dd8778c4 | 143 | |
mglmx | 1:0ab26889af9b | 144 | //combine packet bits in basic DCC format |
mglmx | 1:0ab26889af9b | 145 | command = (prefix<<28)|(address<<19)|(inst<<10)|((error)<<1)|0x01; |
mglmx | 1:0ab26889af9b | 146 | //printf("\n\r %llx \n\r",command); |
carlosperales95 | 24:1d71dd8778c4 | 147 | |
mglmx | 1:0ab26889af9b | 148 | int i=0; |
carlosperales95 | 24:1d71dd8778c4 | 149 | //repeat DCC command lots of times |
mglmx | 1:0ab26889af9b | 150 | while(i < repeat_count) { |
carlosperales95 | 24:1d71dd8778c4 | 151 | |
mglmx | 1:0ab26889af9b | 152 | temp_command = command; |
carlosperales95 | 24:1d71dd8778c4 | 153 | //loops through packet bits encoding and sending out digital pulses for a DCC command |
mglmx | 1:0ab26889af9b | 154 | for (int j=0; j<64; j++) { |
carlosperales95 | 24:1d71dd8778c4 | 155 | |
carlosperales95 | 24:1d71dd8778c4 | 156 | if((temp_command&0x8000000000000000)==0) { |
carlosperales95 | 24:1d71dd8778c4 | 157 | //test packet bit |
mglmx | 1:0ab26889af9b | 158 | //send data for a "0" bit |
mglmx | 1:0ab26889af9b | 159 | Track=0; |
mglmx | 1:0ab26889af9b | 160 | wait_us(100); |
mglmx | 1:0ab26889af9b | 161 | Track=1; |
mglmx | 1:0ab26889af9b | 162 | wait_us(100); |
mglmx | 1:0ab26889af9b | 163 | //printf("0011"); |
carlosperales95 | 24:1d71dd8778c4 | 164 | |
carlosperales95 | 24:1d71dd8778c4 | 165 | }else{ |
carlosperales95 | 24:1d71dd8778c4 | 166 | |
mglmx | 1:0ab26889af9b | 167 | //send data for a "1"bit |
mglmx | 1:0ab26889af9b | 168 | Track=0; |
mglmx | 1:0ab26889af9b | 169 | wait_us(58); |
mglmx | 1:0ab26889af9b | 170 | Track=1; |
mglmx | 1:0ab26889af9b | 171 | wait_us(58); |
mglmx | 1:0ab26889af9b | 172 | //printf("01"); |
mglmx | 1:0ab26889af9b | 173 | } |
mglmx | 1:0ab26889af9b | 174 | // next bit in packet |
mglmx | 1:0ab26889af9b | 175 | temp_command = temp_command<<1; |
mglmx | 1:0ab26889af9b | 176 | } |
mglmx | 1:0ab26889af9b | 177 | i++; |
mglmx | 0:4d06a6a8e785 | 178 | } |
mglmx | 0:4d06a6a8e785 | 179 | } |
carlosperales95 | 11:021210c59a95 | 180 | |
carlosperales95 | 24:1d71dd8778c4 | 181 | |
carlosperales95 | 24:1d71dd8778c4 | 182 | /** |
carlosperales95 | 24:1d71dd8778c4 | 183 | * |
carlosperales95 | 24:1d71dd8778c4 | 184 | **/ |
mglmx | 21:e6f1649add39 | 185 | void flipSwitch(int switchId, int times){ |
mglmx | 21:e6f1649add39 | 186 | //Command variables for Switches using DCC |
mglmx | 21:e6f1649add39 | 187 | |
mglmx | 21:e6f1649add39 | 188 | unsigned int SWBaddress = 0x06; //address for switch box |
mglmx | 21:e6f1649add39 | 189 | |
mglmx | 21:e6f1649add39 | 190 | ////100DDDDD where DDDDD |
mglmx | 21:e6f1649add39 | 191 | ///00001 to flip the first switch SW1 (F1 active) |
mglmx | 21:e6f1649add39 | 192 | ///00010 to flip the second switch SW2 (F2 active) |
mglmx | 21:e6f1649add39 | 193 | ///00100 to flip the third switch SW3 (F3 active) |
mglmx | 21:e6f1649add39 | 194 | ///01000 to flip the fourth switch SW4 (F4 active) |
mglmx | 22:e4153ca757dd | 195 | |
carlosperales95 | 24:1d71dd8778c4 | 196 | //example - 111111 0 00000101 0 10000000 0 10000101 1 - idle |
mglmx | 21:e6f1649add39 | 197 | |
mglmx | 21:e6f1649add39 | 198 | unsigned int SWBflip = 0x80; //No switches -- idle |
mglmx | 21:e6f1649add39 | 199 | |
mglmx | 21:e6f1649add39 | 200 | switch(switchId){ |
mglmx | 21:e6f1649add39 | 201 | case 1: |
mglmx | 21:e6f1649add39 | 202 | SWBflip = 0x81; //FLIP 1 |
mglmx | 21:e6f1649add39 | 203 | break; |
mglmx | 21:e6f1649add39 | 204 | case 2: |
mglmx | 21:e6f1649add39 | 205 | SWBflip = 0x82; |
mglmx | 21:e6f1649add39 | 206 | break; |
mglmx | 21:e6f1649add39 | 207 | case 3: |
mglmx | 21:e6f1649add39 | 208 | SWBflip = 0x84; |
mglmx | 21:e6f1649add39 | 209 | break; |
mglmx | 21:e6f1649add39 | 210 | case 4: |
mglmx | 22:e4153ca757dd | 211 | SWBflip = 0x88; |
mglmx | 21:e6f1649add39 | 212 | break; |
mglmx | 21:e6f1649add39 | 213 | default: |
carlosperales95 | 24:1d71dd8778c4 | 214 | break; |
mglmx | 21:e6f1649add39 | 215 | } |
carlosperales95 | 11:021210c59a95 | 216 | |
mglmx | 21:e6f1649add39 | 217 | //Security measure not to burn the switch. |
mglmx | 21:e6f1649add39 | 218 | if(times <=5){ |
mglmx | 21:e6f1649add39 | 219 | DCC_send_command(SWBaddress,SWBflip,times); |
mglmx | 21:e6f1649add39 | 220 | } |
mglmx | 21:e6f1649add39 | 221 | |
mglmx | 21:e6f1649add39 | 222 | } |
mglmx | 21:e6f1649add39 | 223 | |
mglmx | 22:e4153ca757dd | 224 | |
mglmx | 22:e4153ca757dd | 225 | |
carlosperales95 | 24:1d71dd8778c4 | 226 | /** |
carlosperales95 | 24:1d71dd8778c4 | 227 | * Check the swithces of the box. If they have 1 value the railSiwtches flip. |
carlosperales95 | 24:1d71dd8778c4 | 228 | **/ |
mglmx | 22:e4153ca757dd | 229 | void switching(string sw1,string sw2, string sw3, string sw4){ |
mglmx | 22:e4153ca757dd | 230 | string strInst = "1000" + sw1 + sw2 + sw3 + sw4; |
mglmx | 22:e4153ca757dd | 231 | |
mglmx | 22:e4153ca757dd | 232 | //unsigned int inst = stoi(strInst); |
mglmx | 22:e4153ca757dd | 233 | |
mglmx | 22:e4153ca757dd | 234 | |
mglmx | 22:e4153ca757dd | 235 | //DCC_send_command(0x06,inst,5); |
carlosperales95 | 24:1d71dd8778c4 | 236 | |
mglmx | 22:e4153ca757dd | 237 | } |
mglmx | 22:e4153ca757dd | 238 | |
carlosperales95 | 24:1d71dd8778c4 | 239 | |
carlosperales95 | 24:1d71dd8778c4 | 240 | /** |
carlosperales95 | 24:1d71dd8778c4 | 241 | * |
carlosperales95 | 24:1d71dd8778c4 | 242 | **/ |
mglmx | 21:e6f1649add39 | 243 | void checkSwitch(){ |
carlosperales95 | 24:1d71dd8778c4 | 244 | |
mglmx | 22:e4153ca757dd | 245 | if(switch1 == 1){ |
carlosperales95 | 24:1d71dd8778c4 | 246 | |
mglmx | 22:e4153ca757dd | 247 | lcd.cls(); |
mglmx | 22:e4153ca757dd | 248 | lcd.printf("Switch 1 ON - SW1"); |
mglmx | 22:e4153ca757dd | 249 | flipSwitch(1,5); //IDLE |
carlosperales95 | 24:1d71dd8778c4 | 250 | |
mglmx | 22:e4153ca757dd | 251 | }else if(switch2 == 1){ |
carlosperales95 | 24:1d71dd8778c4 | 252 | |
carlosperales95 | 24:1d71dd8778c4 | 253 | lcd.cls(); |
carlosperales95 | 24:1d71dd8778c4 | 254 | lcd.printf("Switch 2 ON - SW2"); |
carlosperales95 | 24:1d71dd8778c4 | 255 | flipSwitch(2,5); |
carlosperales95 | 24:1d71dd8778c4 | 256 | |
carlosperales95 | 24:1d71dd8778c4 | 257 | }else if(switch3 == 0){ |
carlosperales95 | 24:1d71dd8778c4 | 258 | |
carlosperales95 | 24:1d71dd8778c4 | 259 | lcd.cls(); |
carlosperales95 | 24:1d71dd8778c4 | 260 | lcd.printf("Switch 3 ON - SW3"); |
carlosperales95 | 24:1d71dd8778c4 | 261 | flipSwitch(3,5); |
carlosperales95 | 24:1d71dd8778c4 | 262 | |
carlosperales95 | 24:1d71dd8778c4 | 263 | }else if(switch4 == 0){ |
carlosperales95 | 24:1d71dd8778c4 | 264 | |
carlosperales95 | 24:1d71dd8778c4 | 265 | lcd.cls(); |
carlosperales95 | 24:1d71dd8778c4 | 266 | lcd.printf("Switch 4 ON - IDLE"); |
carlosperales95 | 24:1d71dd8778c4 | 267 | flipSwitch(0,5); |
carlosperales95 | 24:1d71dd8778c4 | 268 | } |
mglmx | 22:e4153ca757dd | 269 | } |
mglmx | 22:e4153ca757dd | 270 | |
carlosperales95 | 24:1d71dd8778c4 | 271 | |
mglmx | 22:e4153ca757dd | 272 | /* |
mglmx | 22:e4153ca757dd | 273 | void checkSwitch(){ |
mglmx | 22:e4153ca757dd | 274 | int switches[] = [0,0,0,0] |
mglmx | 22:e4153ca757dd | 275 | bool changed = false; |
mglmx | 22:e4153ca757dd | 276 | if(switch1 == 1){ |
mglmx | 22:e4153ca757dd | 277 | changed = true; |
mglmx | 22:e4153ca757dd | 278 | switches[0]=1; |
mglmx | 22:e4153ca757dd | 279 | } |
mglmx | 22:e4153ca757dd | 280 | if(switch2 == 1){ |
mglmx | 22:e4153ca757dd | 281 | changed = true; |
mglmx | 22:e4153ca757dd | 282 | switches[1]=1; |
mglmx | 22:e4153ca757dd | 283 | } |
mglmx | 22:e4153ca757dd | 284 | if(switch3 == 1){ |
mglmx | 22:e4153ca757dd | 285 | changed = true; |
mglmx | 22:e4153ca757dd | 286 | switches[2]=1; |
mglmx | 22:e4153ca757dd | 287 | } |
mglmx | 22:e4153ca757dd | 288 | if(switch4 == 1){ |
mglmx | 22:e4153ca757dd | 289 | changed = true; |
mglmx | 22:e4153ca757dd | 290 | switches[3]=1; |
mglmx | 22:e4153ca757dd | 291 | |
mglmx | 22:e4153ca757dd | 292 | } |
mglmx | 22:e4153ca757dd | 293 | |
mglmx | 22:e4153ca757dd | 294 | if(changed){ |
mglmx | 22:e4153ca757dd | 295 | switching(switches[0],switches[) |
mglmx | 22:e4153ca757dd | 296 | } |
mglmx | 22:e4153ca757dd | 297 | |
mglmx | 22:e4153ca757dd | 298 | |
mglmx | 22:e4153ca757dd | 299 | } |
mglmx | 22:e4153ca757dd | 300 | |
mglmx | 22:e4153ca757dd | 301 | */ |
carlosperales95 | 12:e914ca5cd44b | 302 | |
carlosperales95 | 24:1d71dd8778c4 | 303 | |
carlosperales95 | 24:1d71dd8778c4 | 304 | |
carlosperales95 | 11:021210c59a95 | 305 | //**************** MAIN PROGRAM FOR DENVER TRAIN ****************// |
carlosperales95 | 11:021210c59a95 | 306 | |
mglmx | 1:0ab26889af9b | 307 | int main() |
mglmx | 1:0ab26889af9b | 308 | { |
mglmx | 2:f580707c44fa | 309 | led1 = 1; |
mglmx | 22:e4153ca757dd | 310 | wait(0.2); |
mglmx | 2:f580707c44fa | 311 | led1 = 0; |
mglmx | 22:e4153ca757dd | 312 | wait(0.2); |
mglmx | 2:f580707c44fa | 313 | led1 = 1; |
mglmx | 16:2a2da0e67793 | 314 | |
mglmx | 23:3fe9e76e06f2 | 315 | |
mglmx | 16:2a2da0e67793 | 316 | initialize_mcp(); |
mglmx | 16:2a2da0e67793 | 317 | |
mglmx | 22:e4153ca757dd | 318 | //int0.rise(&interrupt0); |
mglmx | 22:e4153ca757dd | 319 | //int1.rise(&interrupt1); |
mglmx | 22:e4153ca757dd | 320 | |
mglmx | 22:e4153ca757dd | 321 | |
mglmx | 1:0ab26889af9b | 322 | //typical out of box default engine DCC address is 3 (at least for Bachmann trains) |
mglmx | 1:0ab26889af9b | 323 | //Note: A DCC controller can reprogram the address whenever needed |
mglmx | 1:0ab26889af9b | 324 | unsigned int DCCaddress = 0x01; |
mglmx | 1:0ab26889af9b | 325 | //see http://www.nmra.org/standards/DCC/standards_rps/RP-921%202006%20Aug%2021.pdf |
mglmx | 1:0ab26889af9b | 326 | //01DCSSSS for speed, D is direction (fwd=1 and rev=0), C is speed(SSSSC) LSB |
mglmx | 1:0ab26889af9b | 327 | unsigned int DCCinst_forward = 0x68; //forward half speed |
mglmx | 1:0ab26889af9b | 328 | unsigned int DCCinst_reverse = 0x48; //reverse half speed |
mglmx | 4:50879dfb82d5 | 329 | unsigned int DCCinst_stop = 0x50; |
mglmx | 1:0ab26889af9b | 330 | //100DDDDD for basic headlight functions |
mglmx | 1:0ab26889af9b | 331 | unsigned int DCC_func_lighton = 0x90; //F0 turns on headlight function |
mglmx | 1:0ab26889af9b | 332 | unsigned int DCC_func_dimlight = 0x91; //F0 + F1 dims headlight |
mglmx | 1:0ab26889af9b | 333 | // |
mglmx | 1:0ab26889af9b | 334 | //Basic DCC Demo Commands |
mglmx | 1:0ab26889af9b | 335 | DCC_send_command(DCCaddress,DCC_func_lighton,200); // turn light on full |
mglmx | 1:0ab26889af9b | 336 | DCC_send_command(DCCaddress,DCC_func_dimlight,200); //dim light |
mglmx | 1:0ab26889af9b | 337 | DCC_send_command(DCCaddress,DCC_func_lighton,200); //light full again |
mglmx | 22:e4153ca757dd | 338 | |
mglmx | 22:e4153ca757dd | 339 | |
mglmx | 22:e4153ca757dd | 340 | //DCC_send_command(0x06,0x8F,5); ALL SWITCHES |
carlosperales95 | 19:ff21ba3a4dc5 | 341 | |
mglmx | 22:e4153ca757dd | 342 | led3 = 1; // Entering the while |
carlosperales95 | 19:ff21ba3a4dc5 | 343 | |
mglmx | 22:e4153ca757dd | 344 | lcd.cls(); |
mglmx | 22:e4153ca757dd | 345 | lcd.printf("Ready to start"); |
carlosperales95 | 19:ff21ba3a4dc5 | 346 | |
carlosperales95 | 14:7bb998edd819 | 347 | //Demo for stopping at the station |
mglmx | 1:0ab26889af9b | 348 | while(1) { |
mglmx | 4:50879dfb82d5 | 349 | |
mglmx | 4:50879dfb82d5 | 350 | if(d21 == 1 || d22 == 1 || d23 == 1){ |
carlosperales95 | 14:7bb998edd819 | 351 | |
mglmx | 4:50879dfb82d5 | 352 | lcd.cls(); |
mglmx | 4:50879dfb82d5 | 353 | lcd.printf("Choo Choo station"); |
carlosperales95 | 14:7bb998edd819 | 354 | DCC_send_command(DCCaddress,DCCinst_stop,400); // Stop train address 3 |
mglmx | 22:e4153ca757dd | 355 | checkSwitch(); |
mglmx | 4:50879dfb82d5 | 356 | |
mglmx | 22:e4153ca757dd | 357 | }else{ |
mglmx | 22:e4153ca757dd | 358 | checkSwitch(); |
carlosperales95 | 14:7bb998edd819 | 359 | DCC_send_command(DCCaddress,DCCinst_forward,1); // Forward half speed train address 3 |
mglmx | 4:50879dfb82d5 | 360 | } |
mglmx | 4:50879dfb82d5 | 361 | |
mglmx | 4:50879dfb82d5 | 362 | |
mglmx | 4:50879dfb82d5 | 363 | /* |
mglmx | 3:fe7010b693a0 | 364 | float f = pot.read(); |
mglmx | 3:fe7010b693a0 | 365 | float vin = f * 3.3; |
mglmx | 3:fe7010b693a0 | 366 | |
mglmx | 2:f580707c44fa | 367 | led3 = 1; |
mglmx | 3:fe7010b693a0 | 368 | if(switch1 == 1){ |
mglmx | 3:fe7010b693a0 | 369 | lcd.cls(); |
mglmx | 3:fe7010b693a0 | 370 | lcd.printf("Going forward"); |
mglmx | 3:fe7010b693a0 | 371 | greenled = 0; |
mglmx | 3:fe7010b693a0 | 372 | redled = 1; |
mglmx | 3:fe7010b693a0 | 373 | DCC_send_command(DCCaddress,DCCinst_forward,400); // forward half speed train address 3 |
mglmx | 3:fe7010b693a0 | 374 | lcd.cls(); |
mglmx | 3:fe7010b693a0 | 375 | |
mglmx | 3:fe7010b693a0 | 376 | lcd.printf("vin: %.4f",vin); |
mglmx | 3:fe7010b693a0 | 377 | wait(0.2); |
mglmx | 3:fe7010b693a0 | 378 | |
mglmx | 3:fe7010b693a0 | 379 | }else{ |
mglmx | 3:fe7010b693a0 | 380 | lcd.cls(); |
mglmx | 3:fe7010b693a0 | 381 | lcd.printf("Going backwards"); |
mglmx | 3:fe7010b693a0 | 382 | greenled = 1; |
mglmx | 3:fe7010b693a0 | 383 | redled = 0; |
mglmx | 3:fe7010b693a0 | 384 | DCC_send_command(DCCaddress,DCCinst_reverse,400); // reverse half speed train address 3 |
mglmx | 3:fe7010b693a0 | 385 | lcd.cls(); |
mglmx | 3:fe7010b693a0 | 386 | |
mglmx | 3:fe7010b693a0 | 387 | lcd.printf("vin: %.4f",vin); |
mglmx | 3:fe7010b693a0 | 388 | wait(0.2); |
mglmx | 3:fe7010b693a0 | 389 | |
mglmx | 3:fe7010b693a0 | 390 | } |
mglmx | 4:50879dfb82d5 | 391 | */ |
mglmx | 1:0ab26889af9b | 392 | } |
mglmx | 3:fe7010b693a0 | 393 | } |