Jasmine Karlsson / Mbed 2 deprecated train_rail

Dependencies:   mbed TextLCD

Revision:
17:b7b5f40cebff
Parent:
16:55c3c5727f14
Child:
18:f5824ba95892
--- a/main.cpp	Fri Jun 08 11:43:36 2018 +0000
+++ b/main.cpp	Fri Jun 08 14:12:49 2018 +0000
@@ -1,7 +1,5 @@
 #include "mbed.h"
 #include "TextLCD.h"
-#include <string>
-#include <iostream>
 #include "mcp23017.h"
 #include "train.h"
 
@@ -10,52 +8,67 @@
 /*----------------------------------------------------------------------------
 Pin definitions
 *----------------------------------------------------------------------------*/
-DigitalOut myled(LED1);
+DigitalOut Track(p20);      // train track
+
+DigitalOut myled1(LED1);
 DigitalOut myled2(LED2);
 DigitalOut myled3(LED3);
 DigitalOut myled4(LED4);
+DigitalOut externalLed1(p15);
+DigitalOut externalLed2(p16);
+DigitalOut externalLed3(p17);
+DigitalOut externalLed4(p18);
 
 TextLCD lcd(p22, p21, p23, p24, p25, p26); // lcd
 
 //AnalogIn Ain(p20);          // pot. met.
-DigitalOut Track(p20);      // train track
-
 /* Train detectors d2, d21, d22 (trainstation) */
 // detect_21(p17);
 //DigitalIn detect_22(p16);
 //DigitalIn detect_2(p15);
 
-/* Switch Definitions */
 DigitalIn sw1(p29);
 DigitalIn sw2(p30);
 DigitalIn sw3(p11);
 DigitalIn sw4(p12);
 //InterruptIn sw1(p5);
-//InterruptIn inter0(p12);
-//InterruptIn inter1(p13);
+InterruptIn inter0(p13);
+InterruptIn inter1(p14);
+
+/*----------------------------------------------------------------------------
+Addresses
+*----------------------------------------------------------------------------*/
+const unsigned int DCCaddress_darkRed = 0x01;
+const unsigned int DCCaddress_lightRed = 0x03;
+const unsigned int DCCaddress_switch = 0x06;
 
 /*----------------------------------------------------------------------------
 Train movement
 *----------------------------------------------------------------------------*/
-
 //move backwards/reverse
 const unsigned int DCCinst_reverse = 0x48; //reverse speed
 
 //speed dial forward
 const unsigned int DCCinst_step2 = 0x72; //step 2
 const unsigned int DCCinst_step4 = 0x73; //step 4
-unsigned int DCCinst_step6 = 0x68; //step 6 1/4 speed
+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
 const unsigned int DCCinst_step28 = 0x7F; //step 28 Full speed
-
+const unsigned int DCCinst_switch1 = 0x81; //Activate switch1
+const unsigned int DCCinst_switch2 = 0x82; //Activate switch2
+const unsigned int DCCinst_switch3 = 0x84; //Activate switch3
+const unsigned int DCCinst_switch4 = 0x88; //Activate switch4
+const unsigned int DCCinst_deactive_switch = 0x80; //Deactivate switches
 //stop 
 const unsigned int DCCinst_stop = 0x40; //forward and stop 01100000
 
-/* Functions definition */
+/*----------------------------------------------------------------------------
+Function definitions
+*----------------------------------------------------------------------------*/
 void readVoltage();
 bool readDetector();
-bool readSwitch();
+bool readSwitch(DigitalIn theSwitch);
 void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count); //send command
 
 
@@ -67,21 +80,50 @@
     sw1.fall(&testInterupt);
     inter0.rise(&riseFunction);
     inter1.rise(&riseFunction);*/
+    lcd.printf("Start the journey");
     while(1){
         
-        /* Train Definitions */
-        unsigned int DCCaddress_darkRed = 0x01;
-        //unsigned int DCCaddress_lightRed = 0x03;
-        lcd.printf("Start the journey");
         wait(0.2);
-        if(readSwitch()){
+        if(readSwitch(sw1)){
             lcd.cls();
             lcd.printf("Forward");
             DCC_send_command(DCCaddress_darkRed,DCCinst_step13,10); // forward half speed train address 3
         }else{
             lcd.cls();
-            lcd.printf("Reverse");
-            DCC_send_command(DCCaddress_darkRed,DCCinst_reverse,10); // forward half speed train address 3
+            lcd.printf("Stop");
+            DCC_send_command(DCCaddress_darkRed,DCCinst_stop,10); // forward half speed train address 3
+        }
+        
+        if(readSwitch(sw2)){
+             
+             DCC_send_command(DCCaddress_switch,DCCinst_switch1,10);
+             myled1 = 1;
+             externalLed1 = 1;
+             DCC_send_command(DCCaddress_switch,DCCinst_deactive_switch,10);
+             myled1 = 0;
+             externalLed1 = 0;
+             
+             DCC_send_command(DCCaddress_switch,DCCinst_switch2,10); //Make sure to deactivate 2 right after activating it
+             myled2 = 1;
+             externalLed2 = 1;
+             DCC_send_command(DCCaddress_switch,DCCinst_deactive_switch,10);
+             myled2 = 0;
+             externalLed2 = 0;
+             
+             DCC_send_command(DCCaddress_switch,DCCinst_switch3,10);
+             myled3 = 1;
+             externalLed3 = 1;
+             DCC_send_command(DCCaddress_switch,DCCinst_deactive_switch,10);
+             myled3 = 0;
+             externalLed3 = 0;
+             
+             DCC_send_command(DCCaddress_switch,DCCinst_switch4,10);
+             myled4 = 1;
+             externalLed4 = 1;
+             DCC_send_command(DCCaddress_switch,DCCinst_deactive_switch,10);
+             myled4 = 0;
+             externalLed4 = 0;
+             
         }
              
     
@@ -123,8 +165,8 @@
 
 
 
-bool readSwitch(){
-    int val = sw1.read();
+bool readSwitch(DigitalIn theSwitch){
+    int val = theSwitch.read();
     lcd.printf("%d", val);
     if(val == 1)
     return true;
@@ -159,7 +201,7 @@
         lcd.printf("%d", val3);
         return true;
     }*/
-  //  return false;
+    return false;
 }
 
 void readVoltage(){