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.
Diff: main.cpp
- Revision:
- 25:a42a1ed4d8e9
- Parent:
- 24:1d71dd8778c4
- Child:
- 26:5c966a0a3e8e
--- a/main.cpp Mon Jun 11 12:31:56 2018 +0000 +++ b/main.cpp Mon Jun 11 14:19:05 2018 +0000 @@ -8,6 +8,7 @@ /******PINS AND DECLARATIONS*******/ +//------PINS //SWITCHES p5 - p8 DigitalIn switch1(p5); @@ -63,14 +64,51 @@ MCP23017 *mcp; +//------GLOBAL VARS + + +//.....DCC TRAIN COMMAND VARS + +//typical out of box default engine DCC address is 3 (at least for Bachmann trains) +//Note: A DCC controller can reprogram the address whenever needed +const unsigned int DCCaddress = 0x01; //Address for train 1 + +//01DCSSSS for speed, D is direction (fwd=1 and rev=0), C is speed(SSSSC) LSB +const unsigned int DCCinst_forward = 0x68; //forward half speed +const unsigned int DCCinst_reverse = 0x48; //reverse half speed +const unsigned int DCCinst_stop = 0x50; //stop the train + +//100DDDDD for basic headlight functions +const unsigned int DCC_func_lighton = 0x90; //F0 turns on headlight function +const unsigned int DCC_func_dimlight = 0x91; //F0 + F1 dims headlight + + +//.....SWITCH COMMAND VARS + +const unsigned int SWBaddress = 0x06; //Address for switch box + +//100DDDDD where DDDDD is the switch command and 100 is constant: + +//00001(F1 active)-00010(F2 active)-00100(F3 active)-01000(F4 active) +//Example - 111111 0 00000101 0 10000000 0 10000101 1 - idle + +const unsigned int SWBidle = 0x80; //IDLE - Flip last activated SW. +const unsigned int SWBflip_1 = 0x81; //Flip SW1 +const unsigned int SWBflip_2 = 0x82; //Flip SW2 +const unsigned int SWBflip_3 = 0x84; //Flip SW3 +const unsigned int SWBflip_4 = 0x88; //Flip SW4 + + + //**************** FUNCTIONS FOR DENVER TRAIN ****************// /** -* Activates the buzzer for 0.5 seconds. +*Activates the buzzer for 0.5 seconds. **/ void doBuzz(){ + buzz = 1; wait(0.5); buzz = 0; @@ -79,6 +117,8 @@ /** * +* ere we initialize the mcp that will be used to manage the interrupts. +* **/ void initialize_mcp(){ mcp = new MCP23017(p28,p27,0x40); //Connect to SCL - p28 and SDA - p27 and MPC I2C address 0x40 @@ -103,8 +143,11 @@ /** * +*Method to catch interrupts 0 +* **/ void interrupt0(){ + int data = mcp->readRegister(GPIO); lcd.cls(); lcd.printf("int0 %x",data); @@ -117,8 +160,11 @@ /** * +*Method to catch interrupts 1 +* **/ void interrupt1(){ + int data = mcp->readRegister(GPIO); lcd.cls(); lcd.printf("int1 %x",data); @@ -130,6 +176,12 @@ /** * +*Method to send DCC commands to train and switches. +* +*@address - (HEX)Address where the commands will be sent +*@inst - (HEX)Number of instruction that will be commanded +*@repeat_count - Number of times the command will be sent +* **/ void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count) { @@ -181,57 +233,48 @@ /** * +*Method to flip the switches +* +*@switchId - (1-4)The ID of the switch we want to flip +*@times - The number of times we want to send the command +* **/ void flipSwitch(int switchId, int times){ - //Command variables for Switches using DCC - unsigned int SWBaddress = 0x06; //address for switch box - - ////100DDDDD where DDDDD - ///00001 to flip the first switch SW1 (F1 active) - ///00010 to flip the second switch SW2 (F2 active) - ///00100 to flip the third switch SW3 (F3 active) - ///01000 to flip the fourth switch SW4 (F4 active) - - //example - 111111 0 00000101 0 10000000 0 10000101 1 - idle - - unsigned int SWBflip = 0x80; //No switches -- idle + unsigned int SWBflip = SWBidle; //IDLE - Flip last activated SW. switch(switchId){ case 1: - SWBflip = 0x81; //FLIP 1 + SWBflip = SWBflip_1; //FLIP SW1 break; case 2: - SWBflip = 0x82; + SWBflip = SWBflip_2; //FLIP SW2 break; case 3: - SWBflip = 0x84; + SWBflip = SWBflip_3; //FLIP SW3 break; case 4: - SWBflip = 0x88; + SWBflip = SWBflip_4; //FLIP SW4 break; default: break; } //Security measure not to burn the switch. - if(times <=5){ - DCC_send_command(SWBaddress,SWBflip,times); - } + if(times <=5){ DCC_send_command(SWBaddress,SWBflip,times); } } - /** -* Check the swithces of the box. If they have 1 value the railSiwtches flip. +* +*Check the swithces of the box. If they have 1 value the railSiwtches flip. +* **/ void switching(string sw1,string sw2, string sw3, string sw4){ string strInst = "1000" + sw1 + sw2 + sw3 + sw4; //unsigned int inst = stoi(strInst); - - //DCC_send_command(0x06,inst,5); } @@ -239,6 +282,8 @@ /** * +* +* **/ void checkSwitch(){ @@ -246,7 +291,7 @@ lcd.cls(); lcd.printf("Switch 1 ON - SW1"); - flipSwitch(1,5); //IDLE + flipSwitch(1,5); }else if(switch2 == 1){ @@ -269,125 +314,55 @@ } -/* -void checkSwitch(){ - int switches[] = [0,0,0,0] - bool changed = false; - if(switch1 == 1){ - changed = true; - switches[0]=1; - } - if(switch2 == 1){ - changed = true; - switches[1]=1; - } - if(switch3 == 1){ - changed = true; - switches[2]=1; - } - if(switch4 == 1){ - changed = true; - switches[3]=1; - - } - - if(changed){ - switching(switches[0],switches[) - } - - -} - -*/ - - //**************** MAIN PROGRAM FOR DENVER TRAIN ****************// int main() { + //RISE FOR INTERRUPTS?? NOT WORKING ATM + //int0.rise(&interrupt0); + //int1.rise(&interrupt1); + + //Read and display potentiometer + //float f = pot.read(); + //float vin = f * 3.3; + //lcd.printf("vin: %.4f",vin); + + //0xFFFC //1111111111111100 + + + //Led routine to start main program led1 = 1; wait(0.2); led1 = 0; wait(0.2); led1 = 1; - - initialize_mcp(); + initialize_mcp(); //mcp initialization for interrupts before train running - //int0.rise(&interrupt0); - //int1.rise(&interrupt1); - - - //typical out of box default engine DCC address is 3 (at least for Bachmann trains) - //Note: A DCC controller can reprogram the address whenever needed - unsigned int DCCaddress = 0x01; - //see http://www.nmra.org/standards/DCC/standards_rps/RP-921%202006%20Aug%2021.pdf - //01DCSSSS for speed, D is direction (fwd=1 and rev=0), C is speed(SSSSC) LSB - unsigned int DCCinst_forward = 0x68; //forward half speed - unsigned int DCCinst_reverse = 0x48; //reverse half speed - unsigned int DCCinst_stop = 0x50; - //100DDDDD for basic headlight functions - unsigned int DCC_func_lighton = 0x90; //F0 turns on headlight function - unsigned int DCC_func_dimlight = 0x91; //F0 + F1 dims headlight - // - //Basic DCC Demo Commands + //Train light routine to start running DCC_send_command(DCCaddress,DCC_func_lighton,200); // turn light on full - DCC_send_command(DCCaddress,DCC_func_dimlight,200); //dim light + DCC_send_command(DCCaddress,DCC_func_dimlight,400); //dim light DCC_send_command(DCCaddress,DCC_func_lighton,200); //light full again - - //DCC_send_command(0x06,0x8F,5); ALL SWITCHES - + //LED3 Shows start of route + LCD notif led3 = 1; // Entering the while - lcd.cls(); lcd.printf("Ready to start"); //Demo for stopping at the station while(1) { - if(d21 == 1 || d22 == 1 || d23 == 1){ + checkSwitch(); //Checks for switch commands everytime. + + if(d21 == 1 || d22 == 1 || d23 == 1){ //If train in any of the 3 sensors of station it stops. lcd.cls(); lcd.printf("Choo Choo station"); - DCC_send_command(DCCaddress,DCCinst_stop,400); // Stop train address 3 - checkSwitch(); + DCC_send_command(DCCaddress,DCCinst_stop,400); }else{ - checkSwitch(); DCC_send_command(DCCaddress,DCCinst_forward,1); // Forward half speed train address 3 - } - - - /* - float f = pot.read(); - float vin = f * 3.3; - - led3 = 1; - if(switch1 == 1){ - lcd.cls(); - lcd.printf("Going forward"); - greenled = 0; - redled = 1; - DCC_send_command(DCCaddress,DCCinst_forward,400); // forward half speed train address 3 - lcd.cls(); - - lcd.printf("vin: %.4f",vin); - wait(0.2); - - }else{ - lcd.cls(); - lcd.printf("Going backwards"); - greenled = 1; - redled = 0; - DCC_send_command(DCCaddress,DCCinst_reverse,400); // reverse half speed train address 3 - lcd.cls(); - - lcd.printf("vin: %.4f",vin); - wait(0.2); - - } - */ + } } }