Jasmine Karlsson / Mbed 2 deprecated train_rail

Dependencies:   mbed TextLCD

Revision:
24:418711ed8c52
Parent:
23:bb57966cb776
Child:
25:90f7a34c253a
--- a/main.cpp	Wed Jun 13 20:50:26 2018 +0000
+++ b/main.cpp	Thu Jun 14 14:47:16 2018 +0000
@@ -5,7 +5,7 @@
 #include "Switch.h"
 #include "Track.h"
 #include "Detector.h"
-#include "MCP23017.h"
+#include <cstdlib>
 
 //Board 1
 /*----------------------------------------------------------------------------
@@ -39,7 +39,7 @@
 
 //speed dial forward
 const unsigned int DCCinst_step2 = 0x72; //step 2
-//const unsigned int DCCinst_step4 = 0x73; //step 4
+const unsigned int DCCinst_step4 = 0x73; //step 4
 //const unsigned int DCCinst_step6 = 0x68; //step 6 1/4 speed
 const unsigned int DCCinst_step13 = 0x78; //step 13 1/2 speed
 //const unsigned int DCCinst_step20 = 0x75; //step 20 3/4 speed
@@ -57,9 +57,11 @@
 *----------------------------------------------------------------------------*/
 bool readSwitch(DigitalIn theSwitch);
 int convertHextoDec(int pos, int intnr);
-bool checkDetector(int inter);
+void checkDetector(int inter);
 void init_mcp();
 void init_interrupt();
+void init_trains();
+void executeCase(int pos);
 Train& assignTrain(int pos);
 
 //Trains
@@ -67,7 +69,7 @@
 Train silverTrain(DCCaddress_silver,DCCinst_step13, 2);
 //Switches
 Switch switch1(DCCaddress_switch,DCCinst_switch1);
-//Switch switch2(DCCaddress_switch,DCCinst_switch2);
+Switch switch2(DCCaddress_switch,DCCinst_switch2);
 Switch switch3(DCCaddress_switch,DCCinst_switch3);
 Switch switch4(DCCaddress_switch,DCCinst_switch4);
 //Detectors
@@ -97,59 +99,27 @@
     lcd.printf("Start journey");
     init_mcp();
     init_interrupt();
-    
-
-    vector<int> res;
-    res.push_back(2);
-    res.push_back(4);
-    dect.reserveDetector(res); //Run trains counterclockwise and reserve the 
-    vector<int> res2;
-    res2.push_back(12);
-    res2.push_back(6);
-    dect.reserveDetector(res2);//detectors in front 
-    redTrain.Stop();
-    silverTrain.Stop();
-    
+    init_trains();
+    wait(2);
     while(1){ 
         redTrain.sendCommand();
         silverTrain.sendCommand();
-        /*if(readSwitch(sw1)){ //Control switches
-        switch1.switchOn();    
-        lcd.printf("Switch 1 on");
-        //switch2.switchOn();  // Need to turn it off immediately!!!
-        switch3.switchOn();
-        switch4.switchOn();
         
-    
-        }*/
-        /*else{
-        //DCC_send_command(DCCaddress_switch,DCCinst_deactive_switch,10);
-        switch1.switchOff();
-        //switch2.switchOff();
-        switch3.switchOff();
-        switch4.switchOff();
-        lcd.printf("Switch 1 off");    
-            
-        }*/
-            
-        /*if(readSwitch(sw2)){ //run train
-            redTrain.goForward();
-            silverTrain.goForward();
-        }
-        else 
-        {
-            redTrain.Stop();
-            silverTrain.Stop();
-        }*/
         if(!readSwitch(sw3)){ //Change speed
             lcd.printf("Go forward");
             redTrain.changeSpeed(DCCinst_step13);
             silverTrain.changeSpeed(DCCinst_step13);
-        
         }
         
-        //TODO: Check if train is not moving and then call function checkdetector
-        
+        if(redTrain.checkStop()){                       //check if Train stopped
+            lcd.cls();
+            lcd.printf("in stop");
+            wait(2);
+            executeCase(redTrain.getPosition());
+        }
+        else if(silverTrain.checkStop()){
+            executeCase(silverTrain.getPosition());
+        }        
     }
 }
 
@@ -204,13 +174,396 @@
     else if(silverTrain.checkInterupt(pos)){
         silverTrain.setPosition(pos);
         //lcd.cls();   
-        //lcd.printf("Silver train!"); 
+        lcd.printf("Silver train!"); 
         return silverTrain;
     }
-  
 }
 
-bool checkDetector(int inter){
+void executeCase(int pos){
+    Train* currTrain = &assignTrain(pos);
+    
+    switch(pos){
+    case 0:
+        lcd.printf("at 0");
+        break;
+    case 1:
+        lcd.printf("at 1");
+        currTrain->changeSpeed(DCCinst_step4);//Slow down
+        lcd.printf("slow down");
+        break;
+    case 2:
+       /*
+       Stop everytime at 2
+       
+       
+       
+       */
+       
+        lcd.printf("at 2");        
+        if(currTrain->isClockwise())        //IF C goes towards 1
+        {
+            switch2.switchOff();                                             //Disable switch4
+            int arr [3] = {3,4,9};                                          
+            vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+            dect.clearReservation(detectors);                                //Free 3,4,9
+            currTrain->changeSpeed(DCCinst_step13);                          //Move forward
+        }
+       else{                                                               //IF CC                        
+           if(!dect.checkReservation(4) && !dect.checkReservation(3)){     //Check if 4 or 3 is reserved
+               int v1 = rand() % 100;                                       //IF NOT randomize which way to go
+               if (v1 < 25){                                                //0.25 chance to enable the switch2,  reserve 3,9
+                    switch2.switchOn();
+                    int arr [2] = {3,9};
+                    vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+                    dect.makeReservation(detectors);
+                } 
+                else {                                                      //0.75 chance to disable switch2, reserve 4
+                   switch2.switchOff(); 
+                    vector<int> detectors(4);
+                    dect.makeReservation(detectors);
+                }
+            
+            }else if(dect.checkReservation(10)){                             //IF 4 is reserved
+                switch2.switchOn();                                        //Enable switch2, reserve 3,9
+                int arr [2] = {3,9};
+                vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+                dect.makeReservation(detectors);
+                    
+            }else{                                                          //IF 3 reserved 
+                switch2.switchOff();                                         //Disable switch2, reserve 4
+                vector<int> detectors(4);
+                dect.makeReservation(detectors);
+            }
+            
+            currTrain->changeSpeed(DCCinst_step13);                          //Go forward
+        }         
+        break;
+    case 3:
+        lcd.printf("at 3");
+        if(currTrain->isClockwise()){   //Clock: Goes from 3 to 2 
+            if(!dect.checkReservation(2)){
+                switch2.switchOn();         //Enable switch2
+                int arr [5] = {0,1,2,12,13};
+                vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+                dect.makeReservation(detectors);                                 //Reserve 0,1,2, 12, 13
+                currTrain->changeSpeed(DCCinst_step13);         //go forward
+            }
+            else{
+                currTrain->Stop();       
+            }
+        }
+        else{                                           //Other Clock: Goes from 3->9
+            currTrain->changeDirection();               //change direction  
+            switch2.switchOff();                        //Disable switch2
+            int arr [5] = {0,1,2,12,13};
+            vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+            dect.clearReservation(detectors);              //free nr 0,1,2,12,13    
+            currTrain->changeSpeed(DCCinst_step13);         //go forward                                                                 
+        }
+        
+        break;
+    case 4:
+        lcd.printf("At 4");
+        if(currTrain->isClockwise())        //IF Clock goes towards 2
+        {
+            int arr [4] = {5,6,7,8};
+            vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+            dect.clearReservation(detectors); //free nr 5,6, 7, 8
+            switch2.switchOff();          //Turn of switch2
+            if(dect.checkReservation(2)){   //Check if 2 is reserved
+                currTrain->Stop();           //IF yes STOP
+            }
+            else{                           
+                int arr [5] = {0,1,2,12,13};//else reserve 12,13,0,1,2 
+                vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+                dect.makeReservation(detectors);
+                currTrain->changeSpeed(DCCinst_step13);      //go forward
+            }    
+        }                     
+        else{ //IF CC :goes towards 6
+            int arr [5] = {0,1,2,12,13};
+            vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+            dect.clearReservation(detectors);   //free nr 0,1,2,12,13
+            switch3.switchOn();                 //Turn on switch3
+            dect.showReservation();
+            wait(2);
+            if(dect.checkReservation(6)){   //Check if 6 is reserved
+                lcd.printf("stop, 6 is reserved");
+                currTrain->Stop();           //IF yes STOP
+                lcd.cls();
+                lcd.printf("Currtrain: ");
+                lcd.printf("%d", currTrain->getSpeed());
+                lcd.printf("\n Silver: ");
+                lcd.printf("%d", silverTrain.getSpeed());
+                
+            }
+            else{
+                int arr [3] = {6,7,8};
+                vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+                dect.makeReservation(detectors);  //else reserve 6,7,8 and go forward
+                currTrain->changeSpeed(DCCinst_step13);
+            }
+            
+        }   
+        break;      
+    case 5:
+        lcd.printf("at 5");
+        
+        if(!currTrain->isClockwise()){   //CC: Goes from 5 to 6
+            if(!dect.checkReservation(2)){
+                switch3.switchOff();         //Disable switch3
+                int arr [3] = {6,7,8};
+                vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+                dect.makeReservation(detectors);                                 //Reserve 6,7,8
+                currTrain->changeSpeed(DCCinst_step13);         //go forward
+            }
+            else{
+                currTrain->Stop();       
+            }
+        }
+        else{                                           //Other Clock: Goes from 5->11
+            currTrain->changeDirection();               //change direction  
+            switch3.switchOn();                        //Enable switch3
+            int arr [3] = {6,7,8};
+            vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+            dect.clearReservation(detectors);              //free nr 6,7,8
+            currTrain->changeSpeed(DCCinst_step13);          //go forward                                                               
+        }
+        break;
+    case 6:
+        lcd.printf("At 6");
+        if(!currTrain->isClockwise())        //IF CC goes towards 7
+        {
+            switch3.switchOn();                                             //Enable switch3
+            int arr [3] = {4,5,11};
+            vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+            dect.clearReservation(detectors);                               //free nr 4,5,11
+            currTrain->changeSpeed(DCCinst_step13);                          //Move forward
+        }
+        else{                           
+           if(!dect.checkReservation(4) && !dect.checkReservation(5)){      //Check if 4 or 5 is reserved
+               int v1 = rand() % 100;                                       //IF NOT randomize which way to go
+               if (v1 < 25){                                                //0.25 chance to disable the switch3, reserve 5,11
+                    switch3.switchOff();
+                    int arr [2] = {5,11};
+                    vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+                    dect.makeReservation(detectors);
+                } 
+                else {                                                      //0.75 chance to Enable switch3, reserve 4
+                   switch3.switchOn(); 
+                    vector<int> detectors(4);
+                    dect.makeReservation(detectors);
+                }
+            
+            }else if(dect.checkReservation(4)){                             //IF 4 is reserved
+                switch3.switchOff();                                        //Disable switch3, reserve 5,11
+                int arr [2] = {5, 11};
+                vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+                dect.makeReservation(detectors);
+                    
+            }else{                                                          //IF 5 reserved
+                switch3.switchOn();                                         //Enable switch3, reserve 4
+                vector<int> detectors(4);
+                dect.makeReservation(detectors);
+            }
+            
+            currTrain->changeSpeed(DCCinst_step13);                          //Go forward
+        }         
+        break;
+    case 7:
+        lcd.printf("At 7yhyh");
+        break;
+    case 8:
+          lcd.printf("At 8");
+        if(currTrain->isClockwise())        //IF C goes towards 7
+        {
+            switch4.switchOff();                                             //Disable switch4
+            int arr [3] = {3,9,10};                                          
+            vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+            dect.clearReservation(detectors);                                //Free 3,9,10
+            currTrain->changeSpeed(DCCinst_step13);                          //Move forward
+        }
+       else{                                                               //IF CC                        
+           if(!dect.checkReservation(9) && !dect.checkReservation(10)){     //Check if 9 or 10 is reserved
+               int v1 = rand() % 100;                                       //IF NOT randomize which way to go
+               if (v1 < 25){                                                //0.25 chance to enable the switch4,  reserve 3,9
+                    switch4.switchOn();
+                    int arr [2] = {3,9};
+                    vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+                    dect.makeReservation(detectors);
+                } 
+                else {                                                      //0.75 chance to disable switch4, reserve 10
+                   switch4.switchOff(); 
+                    vector<int> detectors(10);
+                    dect.makeReservation(detectors);
+                }
+            
+            }else if(dect.checkReservation(10)){                             //IF 10 is reserved
+                switch4.switchOn();                                        //Enable switch4, reserve 3,9
+                int arr [2] = {3,9};
+                vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+                dect.makeReservation(detectors);
+                    
+            }else{                                                          //IF 9 reserved 
+                switch4.switchOff();                                         //Disable switch4, reserve 10
+                vector<int> detectors(10);
+                dect.makeReservation(detectors);
+            }
+            
+            currTrain->changeSpeed(DCCinst_step13);                          //Go forward
+        }         
+        break;
+    case 9:
+        lcd.printf("at 9"); 
+         if(currTrain->isClockwise()){   //Clock: Goes from 9 to 8
+            if(!dect.checkReservation(8)){
+                switch4.switchOn();         //Enable switch4
+                int arr [3] = {6,7,8};
+                vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+                dect.makeReservation(detectors);                                 //Reserve 6,7,8
+                currTrain->changeSpeed(DCCinst_step13);                          //Go forward
+
+            }
+              else{
+                currTrain->Stop();       
+            }
+        }
+        else{                                           //Other Clock: Goes from 9->3
+            currTrain->changeDirection();               //change direction  
+            switch4.switchOff();                        //Disable switch4
+            int arr [3] = {6,7,8};
+            vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+            dect.clearReservation(detectors);              //free nr 6,7,8 
+            currTrain->changeSpeed(DCCinst_step13);                          //Go forward                                                       
+        }
+        break;
+    case 10:
+        lcd.printf("At 10");
+        if(!currTrain->isClockwise())        //IF CC goes towards 12
+        {
+            int arr [4] = {6,7,8,9};
+            vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+            dect.clearReservation(detectors); //free nr 6, 7, 8, 9
+            switch1.switchOff();          //Turn of switch1
+            if(dect.checkReservation(12)){   //Check if 12 is reserved
+                currTrain->Stop();           //IF yes STOP
+            }
+            else{                           
+                int arr [5] = {0,1,2,12,13};//else reserve 12,13,0,1,2 
+                vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+                dect.makeReservation(detectors);
+                currTrain->changeSpeed(DCCinst_step13);      //go forward
+            }    
+        }         
+        
+         else{ //IF C :goes towards 8
+            int arr [5] = {0,1,2,12,13};
+            vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+            dect.clearReservation(detectors);   //free nr 0,1,2,12,13
+            switch4.switchOff();                 //Turn off switch4
+            
+            if(dect.checkReservation(8)){   //Check if 8 is reserved
+                currTrain->Stop();           //IF yes STOP                
+            }
+            else{
+                int arr [3] = {6,7,8};
+                vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+                dect.makeReservation(detectors);  //else reserve 6,7,8 and go forward
+                currTrain->changeSpeed(DCCinst_step13);
+            }  
+        }   
+        break;
+        
+    case 11:
+        lcd.printf("at 11");
+        
+        if(currTrain->isClockwise()){   //Clockwise: Goes from 11 to 5
+            currTrain->changeDirection();               //change direction
+            switch1.switchOff();         //Disable switch1
+            int arr [5] = {0,1,2,12,13};
+            vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+            dect.clearReservation(detectors);                                 //free 0,1,2,12,13
+            currTrain->changeSpeed(DCCinst_step13);         //go forward    
+
+        }
+        else{                                           //Other Clock: Goes from 11->12 
+            if(!dect.checkReservation(8)){
+                
+                switch1.switchOn();                        //Enable switch1
+                int arr [5] = {0,1,2, 12,13};
+                vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+                dect.makeReservation(detectors);               //Reserve 0,1,2, 12, 13   
+                currTrain->changeSpeed(DCCinst_step13);         //go forward   
+            }
+            else{
+                currTrain->Stop();       
+            }                                                             
+        }
+        break;
+    case 12: 
+        lcd.printf("at 12");
+        if(!currTrain->isClockwise())        //IF CC goes towards 13
+        {
+            switch1.switchOff();               //Turn of switch1
+            int arr [3] = {5,10,11};
+            vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+            dect.clearReservation(detectors); //free nr 5, 10, 11
+            currTrain->changeSpeed(DCCinst_step13);                          //Move forward
+        }
+        else{                                                               //if C
+           if(!dect.checkReservation(10) && !dect.checkReservation(11)){      //Check if 10 or 11 is reserved
+               int v1 = rand() % 100;                                       //IF NOT randomize which way to go
+               if (v1 < 25){                                                //0.25 chance to enable the switch1, reserve 5,11
+                    switch1.switchOn();
+                    int arr [2] = {5,11};
+                    vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+                    dect.makeReservation(detectors);
+                } 
+                else {                                                      //0.75 chance to disable switch1, reserve 10
+                   switch1.switchOff(); 
+                    vector<int> detectors(10);
+                    dect.makeReservation(detectors);
+                }
+            
+            }else if(dect.checkReservation(10)){                             //IF 10 is reserved
+                switch1.switchOn();                                        //Enable switch1, reserve 5,11
+                int arr [2] = {5, 11};
+                vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
+                dect.makeReservation(detectors);
+                    
+            }else{                                                          //IF 11 reserved
+                switch1.switchOff();                                         //Disable switch1, reserve 10
+                vector<int> detectors(10);
+                dect.makeReservation(detectors);
+            }
+            
+            currTrain->changeSpeed(DCCinst_step13);                          //Go forward
+        }         
+        break;
+            
+    
+    case 13: 
+        lcd.printf("at 13");
+        break;
+    case 14:
+        lcd.printf("at 21");
+        break;
+    case 15:
+        lcd.printf("at 22"); 
+        
+        //lcd.printf("In case ");
+        //lcd.printf("%d", pos);
+        break;
+    default:   
+        lcd.printf("Not in a valid case");
+        break; 
+    }
+    
+    mcp->_read(GPIOA);
+    mcp->_read(GPIOB);
+}
+
+void checkDetector(int inter){
     lcd.cls();
     int pos;
     wait_us(100);
@@ -227,177 +580,9 @@
     else 
         //PROBLEM
         lcd.printf("Fail to detect int");
-    
-    Train currTrain = assignTrain(pos);
+        
+    executeCase(pos);
     
-    switch(pos){
-    case 0:
-    case 1:
-        //Slow down
-    case 2:
-        //IF Clock 2->1
-            //Disable Switch2
-            //Free 3,4,9 
-            //Move forward
-        //IF CC
-            //Check if 4 or 3 is reserved
-                //IF NOT randomize which way to go
-                    //0.3 chance to enable the switch2, reserve 3,9 and go
-                    //0.7 chance to disable switch2, reserve 4 and go
-                //IF 4 is reserved
-                    //Enable switch2, reserve 3,9 and go
-                //IF 3 reserved 
-                    //Disable switch2, reserve 4 and go
-    case 3:
-        //Clock: Goes from 3 to 2
-            //Enable switch2
-            //Reserve 0,1,2, 12, 13
-            //Go forward
-        //Other Clock: Goes from 3->9
-            //disable switch2
-            //free 0,1,2,12,13
-            //Go forward
-    case 4:
-        lcd.printf("At 4");
-        if(currTrain.isClockwise())        //IF Clock goes towards 2
-        {
-            int arr [4] = {5,6,7,8};
-            vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
-            dect.clearReservation(detectors); //free nr 5,6, 7, 8
-            //switch2.switchOff();          //Turn of switch2
-            if(dect.checkReservation(2)){   //Check if 2 is reserved
-                currTrain.Stop();           //IF yes STOP
-            }
-            else{                           
-                int arr [5] = {0,1,2,12,13};//else reserve 12,13,0,1,2 
-                vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
-                dect.reserveDetector(detectors);
-                currTrain.changeSpeed(DCCinst_step13);      //go forward
-            }    
-        }                     
-        else{ //IF CC :goes towards 6
-            int arr [5] = {0,1,2,12,13};
-            vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
-            dect.clearReservation(detectors);   //free nr 0,1,2,12,13
-            switch3.switchOn();                 //Turn on switch3
-            
-            if(dect.checkReservation(6)){   //Check if 6 is reserved
-                lcd.printf("stop");
-                currTrain.Stop();           //IF yes STOP
-                lcd.cls();
-                lcd.printf("Currtrain: ");
-                lcd.printf("%d", currTrain.getSpeed());
-                lcd.printf("\n Redtrain: ");
-                lcd.printf("%d", redTrain.getSpeed());
-                
-            }
-            else{
-                int arr [3] = {6,7,8};
-                vector<int> detectors(arr, arr + sizeof(arr) / sizeof(int));
-                dect.reserveDetector(detectors);  //else reserve 6,7,8 and go forward
-                currTrain.changeSpeed(DCCinst_step13);
-            }
-            
-        }   
-        break;      
-    case 5:
-        //Clock: Goes from 5->6
-            //Enable switch3
-            //Reserve 6,7,8
-            //Go forward
-        //Other Clock: Goes from 5->11
-            //disable switch3
-            //free 6,7,8
-            //Go forward
-    case 6:
-        //IF CC
-            //Disable switch3
-            //Free 4,5,11
-            //Move forward
-        //IF Clock
-            //Check if 4 or 5 is reserved
-                //IF NOT randomize which way to go
-                    //0.3 chance to enable the switch3, reserve 5,11 and go
-                    //0.7 chance to disable switch3, reserve 4 and go
-                //IF 4 is reserved
-                    //Enable switch3, reserve 5,11 and go
-                //IF 5 reserved 
-                    //Disable switch3, reserve 4 and go
-                    
-    
-    case 7:
-    case 8:
-        //IF Clock 8->7
-            //Disable switch4
-            //Free 3,9,10
-            //Move forward
-        //IF CC
-            //Check if 9 or 10 is reserved
-                //IF NOT randomize which way to go
-                    //0.3 chance to enable the switch4, reserve 3,9 and go
-                    //0.7 chance to disable switch4, reserve 10 and go
-                //IF 10 is reserved
-                    //Enable switch4, reserve 3,9 and go
-                //IF 9 reserved 
-                    //Disable switch4, reserve 10 and go
-    case 9:
-        //Clock: Goes from 9 to 8
-            //Enable switch4
-            //Reserve 6,7,8
-            //Go forward
-        //Other Clock: Goes from 9->3
-            //disable switch4
-            //free 6,7,8
-            //Go forward
-    case 10:
-        //IF CC
-            //free nr 6,7,8,9
-            //Turn off switch1
-            //Check if 12 is reserved
-            //IF yes STOP
-            //else reserve 12,13,0,1,2 and go forward
-        //IF Clock
-            //Free nr 0,1,2,11, 12, 13
-            //Turn off switch4
-            //Check if 8 is reserved
-            //IF yes STOP
-            //else reserve 6,7,8 go forward
-    case 11:
-        //Clock: Goes from 11 to 12
-            //Enable switch1
-            //Reserve 0,1,2, 12, 13
-            //Go forward
-        //Other Clock: Goes from 11->5
-            //disable switch1
-            //free 0,1,2,12,13
-            //Go forward
-    case 12: 
-        //IF CC: 12->13
-            //Disable switch1
-            //Free 5,10,11
-            //Move forward
-        //IF Clock
-            //Check if 11 or 10 is reserved
-                //IF NOT randomize which way to go
-                    //0.3 chance to enable switch1, reserve 5,11 and go
-                    //0.7 chance to disable switch1, reserve 10 and go
-                //IF 10 is reserved
-                    //Enable switch1, reserve 5,11 and go
-                //IF 11 reserved 
-                    //Disable switch1,reserve 10 and go
-    case 13: 
-        
-        //lcd.printf("In case ");
-        //lcd.printf("%d", pos);
-        break;
-    default:   
-        lcd.printf("Not in a valid case");
-        break; 
-    }
-    
-    mcp->_read(GPIOA);
-    mcp->_read(GPIOB);
-    return true;
 }
 
 bool readSwitch(DigitalIn theSwitch){
@@ -444,3 +629,17 @@
     mcp->_write(GPINTENA, (unsigned char )0xff);
     mcp->_write(GPINTENB, (unsigned char )0xff);// Ready to go!
 }
+
+void init_trains(){
+    redTrain.changeSpeed(0x40);
+    silverTrain.changeSpeed(0x40);
+    
+    vector<int> res;
+    res.push_back(2);
+    res.push_back(4);
+    dect.makeReservation(res); //Run trains counterclockwise and reserve the 
+    vector<int> res2;
+    res2.push_back(12);
+    res2.push_back(13);
+    dect.makeReservation(res2);
+}