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:
- 18:f5824ba95892
- Parent:
- 17:b7b5f40cebff
- Child:
- 20:32ba0a5f2d02
diff -r b7b5f40cebff -r f5824ba95892 main.cpp
--- a/main.cpp Fri Jun 08 14:12:49 2018 +0000
+++ b/main.cpp Mon Jun 11 10:53:14 2018 +0000
@@ -1,7 +1,9 @@
#include "mbed.h"
#include "TextLCD.h"
-#include "mcp23017.h"
-#include "train.h"
+#include "MCP23017.h"
+#include "Train.h"
+
+#include <ctime>
//Board 1
@@ -34,6 +36,7 @@
//InterruptIn sw1(p5);
InterruptIn inter0(p13);
InterruptIn inter1(p14);
+MCP23017 *mcp;
/*----------------------------------------------------------------------------
Addresses
@@ -46,15 +49,15 @@
Train movement
*----------------------------------------------------------------------------*/
//move backwards/reverse
-const unsigned int DCCinst_reverse = 0x48; //reverse speed
+//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
-const unsigned int DCCinst_step6 = 0x68; //step 6 1/4 speed
+//const unsigned int DCCinst_step2 = 0x72; //step 2
+//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
-const unsigned int DCCinst_step28 = 0x7F; //step 28 Full 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
@@ -63,13 +66,19 @@
//stop
const unsigned int DCCinst_stop = 0x40; //forward and stop 01100000
+//detectors addresses
+const unsigned int detector_0 = 0xFFFE;
+const unsigned int detector_0_1 = 0xFFFC;
+const unsigned int detector_1 = 0xFFFD;
+
/*----------------------------------------------------------------------------
Function definitions
*----------------------------------------------------------------------------*/
void readVoltage();
-bool readDetector();
+bool readDetector(DigitalIn detector);
bool readSwitch(DigitalIn theSwitch);
void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count); //send command
+void initialize_mcp();
/*----------------------------------------------------------------------------
@@ -80,18 +89,37 @@
sw1.fall(&testInterupt);
inter0.rise(&riseFunction);
inter1.rise(&riseFunction);*/
+ Train lightRed(DCCaddress_lightRed);
+ Train darkRed(DCCaddress_darkRed);
lcd.printf("Start the journey");
+ time_t tstart, tend;
+ initialize_mcp();
+ int data = mcp->readRegister(0x12);
+ lcd.printf("%d", data);
+ wait(2);
+
while(1){
wait(0.2);
if(readSwitch(sw1)){
+ //lcd.cls();
+ //lcd.printf("Forward");
+ tstart = time(0);
+ DCC_send_command(DCCaddress_darkRed,DCCinst_step13,10); // forward half speed train address 3
+ tend = time(0);
lcd.cls();
- lcd.printf("Forward");
- DCC_send_command(DCCaddress_darkRed,DCCinst_step13,10); // forward half speed train address 3
+ lcd.printf("Time to send command:");
+ lcd.printf("%f", difftime(tend, tstart));
+
}else{
+ //lcd.cls();
+ //lcd.printf("Stop");
+ tstart = time(0);
+ DCC_send_command(DCCaddress_darkRed,DCCinst_stop,10); // forward half speed train address 3
+ tend = time(0);
lcd.cls();
- lcd.printf("Stop");
- DCC_send_command(DCCaddress_darkRed,DCCinst_stop,10); // forward half speed train address 3
+ lcd.printf("Time to send command:");
+ lcd.printf("%f", difftime(tend, tstart));
}
if(readSwitch(sw2)){
@@ -125,12 +153,9 @@
externalLed4 = 0;
}
-
-
}
}
-
/*----------------------------------------------------------------------------
Functions
*----------------------------------------------------------------------------*/
@@ -162,9 +187,6 @@
lcd.printf("%d", inter1.read());
}*/
-
-
-
bool readSwitch(DigitalIn theSwitch){
int val = theSwitch.read();
lcd.printf("%d", val);
@@ -174,31 +196,13 @@
return false;
}
-bool readDetector(){
- /*int val1 = detect_2.read();
- if(val1 == 1)
+bool readDetector(DigitalIn detector){
+ /*int val = detect.read();
+ if(val == 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);
+ lcd.printf("Detect: ");
+ lcd.printf("%d", val);
return true;
}*/
return false;
@@ -211,6 +215,25 @@
wait(0.1);
lcd.printf("\n");*/
}
+void initialize_mcp(){
+ mcp = new MCP23017(p28, p27, 0x40);
+ mcp->reset();
+ mcp->writeRegister(0x00, (unsigned char )0xff);
+ mcp->writeRegister(0x01, (unsigned char )0xff);
+ mcp->writeRegister(0x02, (unsigned char )0x00);
+ mcp->writeRegister(0x03, (unsigned char )0x00);
+ mcp->writeRegister(0x04, (unsigned char )0xff);
+ mcp->writeRegister(0x05, (unsigned char )0xff);
+ mcp->writeRegister(0x06, (unsigned char )0xff);
+ mcp->writeRegister(0x07, (unsigned char )0xff);
+ mcp->writeRegister(0x08, (unsigned char )0xff);
+ mcp->writeRegister(0x09, (unsigned char )0xff);
+ mcp->writeRegister(0x0a, (unsigned char )0x42);
+ mcp->writeRegister(0x0b, (unsigned char )0x42);
+ mcp->writeRegister(0x0c, (unsigned char )0x00);
+ mcp->writeRegister(0x0d, (unsigned char )0x00);
+}
+
void DCC_send_command(unsigned int address, unsigned int inst, unsigned int repeat_count)
{
