Denver / Mbed 2 deprecated denver_train_proj

Dependencies:   mbed TextLCD

Revision:
21:e6f1649add39
Parent:
20:608ab9bf340d
Child:
22:e4153ca757dd
--- a/main.cpp	Thu Jun 07 12:58:32 2018 +0000
+++ b/main.cpp	Fri Jun 08 10:09:46 2018 +0000
@@ -7,10 +7,12 @@
  
 /******PINS AND DECLARATIONS*******/
 
-///p5
-///p6
-///p7
-///p8
+
+//SWITCHES p5 - p8
+DigitalIn switch1(p5);
+DigitalIn switch2(p6);
+DigitalIn switch3(p7);
+DigitalIn switch4(p8);
 
 //RAIL SENSORS - INT0,INT1
 //INT0 - p9
@@ -140,7 +142,49 @@
     }
 }
 
+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
+    
+    switch(switchId){
+        case 1:
+            SWBflip = 0x81;     //FLIP 1
+            break;
+        case 2:
+            SWBflip = 0x82;
+            break;
+        case 3:
+            SWBflip = 0x84;
+            break;
+        case 4:
+            SWBflip = 0x90;
+            break;
+        default:
+            break;
+            
+    }          
 
+    //Security measure not to burn the switch.
+    if(times <=5){
+        DCC_send_command(SWBaddress,SWBflip,times);
+    }
+    
+
+}
+
+void checkSwitch(){
+    if(switch)
+}
 
 //**************** MAIN PROGRAM FOR DENVER TRAIN ****************//
 
@@ -157,6 +201,21 @@
     int0.rise(&interrupt0);
     int1.rise(&interrupt1);
     
+    led2 = 1;
+    flipSwitch(1,1);
+    wait(0.2);
+    led2 = 0;
+    wait(0.2);
+    
+    led2 = 1;
+    flipSwitch(2,1);
+    wait(0.2)
+    led2 = 0;
+    
+    
+    
+    
+    
     
     //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
@@ -174,26 +233,9 @@
     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_lighton,200);  //light full again
-    led2 = 1;
     
-    //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_idle = 0x80;   //No switches -- idle
-    unsigned int SWBflip_f1 = 0x81;     //FLIP 1
-    unsigned int SWBflip_f2 = 0x82;     
-    unsigned int SWBflip_f3 = 0x84;
-    unsigned int SWBflip_f4 = 0x90;
-
-    //Send using DCC_send_command()
     
     
     //Demo for stopping at the station
@@ -205,7 +247,11 @@
             lcd.printf("Choo Choo station");
             DCC_send_command(DCCaddress,DCCinst_stop,400); // Stop train address 3 
             
-        }else{
+        }else if(switch1 == 1){
+            switchFlip(1,1);
+            
+        }
+        {
             DCC_send_command(DCCaddress,DCCinst_forward,1); // Forward half speed train address 3 
         }