New project

Dependencies:   mbed TextLCD

Revision:
15:8d96f7a06103
Parent:
14:a5fd98a957e6
Child:
16:55c3c5727f14
--- a/main.cpp	Wed Jun 06 23:09:22 2018 +0000
+++ b/main.cpp	Thu Jun 07 15:09:53 2018 +0000
@@ -3,6 +3,7 @@
 #include <string>
 #include <iostream>
 #include "mcp23017.h"
+#include "train.h"
 
 //board 1
 
@@ -19,18 +20,20 @@
 TextLCD lcd(p22, p21, p23, p24, p25, p26); // lcd
 
 AnalogIn Ain(p20);          // pot. met.
-DigitalOut Track(p21);      // train track
+DigitalOut Track(p29);      // train track
 
 /* Train detectors d2, d21, d22 (trainstation) */
-AnalogIn detect_21(p15);
-AnalogIn detect_22(p16);
-AnalogIn detect_2(p17);
+DigitalIn detect_21(p17);
+DigitalIn detect_22(p16);
+DigitalIn detect_2(p15);
 
 /* Switch Definitions */
-DigitalIn sw1(p5);
-DigitalIn sw2(p30);
+InterruptIn sw1(p5);
+InterruptIn inter0(p12);
+InterruptIn inter1(p13);
+/*DigitalIn sw2(p30);
 DigitalIn sw3(p29);
-DigitalIn sw4(p28);
+DigitalIn sw4(p28);*/
 
 /* Train Definitions */
 unsigned int DCCaddress_darkRed = 0x01;
@@ -41,34 +44,23 @@
 /*----------------------------------------------------------------------------
 Train movement
 *----------------------------------------------------------------------------*/
-//move forward
-unsigned int DCCinst_forward = 0x68; //forward half speed
 
 //move backwards/reverse
-unsigned int DCCinst_reverse = 01001000; //reverse half speed
+const unsigned int DCCinst_reverse = 01001000; //reverse speed
 
-//speed dial
-unsigned int DCCinst_step1 = 01100010; //step 1
-unsigned int DCCinst_step2 = 01110010; //step 2
-unsigned int DCCinst_step3 = 01100011; //step 3
-unsigned int DCCinst_step4 = 01110011; //step 4
-unsigned int DCCinst_step5 = 01100100; //step 5
-unsigned int DCCinst_step6 = 01110100; //step 6 1/4 speed
+//speed dial forward
+const unsigned int DCCinst_step2 = 01110010; //step 2
+const unsigned int DCCinst_step4 = 01110011; //step 4
+const unsigned int DCCinst_step6 = 01110100; //step 6 1/4 speed
 
-unsigned int DCCinst_step13 = 01100000; //step 13 1/2 speed
+const unsigned int DCCinst_step13 = 01111000; //step 13 1/2 speed
 
-unsigned int DCCinst_step20 = 01110101; //step 20 3/4 speed
+const unsigned int DCCinst_step20 = 01110101; //step 20 3/4 speed
 
-unsigned int DCCinst_step28 = 01111111; //step 28 Full speed
+const unsigned int DCCinst_step28 = 01111111; //step 28 Full speed
 
 //stop 
-unsigned int DCCinst_forward_stop = 0x60; //forward and stop 01100000
-
-//move forward
-
-//speed dial
-
-//stop 
+const unsigned int DCCinst_stop = 01100000; //forward and stop 01100000
 
 //read switch out
 
@@ -86,29 +78,105 @@
 void initMcp0(void);            // Initialization of MCP23017 with address bits 000 (track sensors)
 void testMcp0(void);            // Test of MCP23017 with address bits 000
 void readVoltage();
-void readDetector();
+bool readDetector();
 void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count); //send command
 
+
+void testInterupt(){
+    lcd.cls();
+    myled = 1;
+    lcd.printf("In interupt function");
+    lcd.printf("%d", sw1.read());
+    myled4 = 0;
+}
+
+void testInterupt2(){
+    lcd.cls();
+    myled = 0;
+    lcd.printf("In interupt2 function");
+    lcd.printf("%d", sw1.read());
+    myled4 = 1;
+}
+
+void riseFunction(){
+    lcd.printf("In rise function");
+    lcd.printf("%d", inter1.read());
+}
+
+void fallFunction(){
+    lcd.printf("In fall function");
+    lcd.printf("%d", inter1.read());    
+}
+
 int main() {
+    /*sw1.rise(&testInterupt2);
+    sw1.fall(&testInterupt);
+    inter0.rise(&riseFunction);
+    inter1.rise(&riseFunction);*/
     while(1){
-        //unsigned int DCCaddress = 0x03;
-        //unsigned int DCCaddress_darkRed = 0x01;
-        readDetector();
+        
+        //wait(0.25);
+        unsigned int DCCaddress = 0x03;
+        unsigned int DCCinst_step6 = 0x68; //step 6 1/4 speed
+        
+        DCC_send_command(DCCaddress_darkRed,DCCinst_step6,400); // forward half speed train address 3
         lcd.printf("Forward");
-        DCC_send_command(DCCaddress_lightRed,DCCinst_forward,400); // forward half speed train address 3
-        wait(0.5);
+        //unsigned int DCCaddress_darkRed = 0x01;
+        /*if(readDetector())
+        {
+            DCC_send_command(DCCaddress_darkRed,DCCinst_stop,400); // forward half speed train address 3
+            lcd.printf("Stop");
+            wait(2);
+        }
+        else
+            lcd.printf("Go forward");*/
+        //lcd.cls();
+        //lcd.printf("Forward");
+        //wait(2);
+        DCC_send_command(DCCaddress_darkRed,DCCinst_stop,400); // forward half speed train address 3
         lcd.printf("Stop");
-        DCC_send_command(DCCaddress_lightRed,DCCinst_forward_stop,400); // forward half speed train address 3
-        wait(1);
+        wait(2);
+        //wait(1.5);
+        //lcd.cls();
+        //lcd.printf("Stop");
+        //wait(1);
         //readVoltage();
         //myled=1; 
     }
 }
 
-void readDetector(){
-    float val = detect_21.read();
-    lcd.printf("%.2f", val);
+bool readDetector(){
+    int val1 = detect_2.read();
+    
+    
+    if(val1 == 1)
+    {
+        lcd.cls();
+        lcd.printf("Detect 2: ");
+        lcd.printf("%d", val1);
+        return true;
     }
+        
+    
+    int val2 = detect_21.read();
+    
+    if(val2 == 1)
+    {
+        lcd.cls();
+        lcd.printf("Detect 21: ");
+        lcd.printf("%d", val2);
+        return true;
+    }
+    int val3 = detect_22.read();
+    if(val3 == 1)
+    {
+        lcd.cls();
+        lcd.printf("Detect 22: ");
+        lcd.printf("%d", val3);
+        return true;
+    }
+    return false;
+}
 
 void readVoltage(){
         float f = Ain.read(); //Read voltage value
@@ -116,7 +184,7 @@
         lcd.printf("%.2f", Vin);
         wait(0.1);
         lcd.printf("\n");
-    }
+}
 
 void mcpWriteReg(uint8_t address, uint8_t reg, uint8_t data){
     char cmd[2];cmd[0] = reg;