3rd year group project. Electronic and Electrical Engineering. Heriot-Watt University. This is the code for the mbed for the Automatic Little Object Organiser (ALOO).

Dependencies:   MCP23017 TCS3472_I2C WattBob_TextLCD mbed

Revision:
9:dc8f155b71c8
Parent:
8:e1da2ae62885
Child:
10:16ba52f8e025
diff -r e1da2ae62885 -r dc8f155b71c8 main.cpp
--- a/main.cpp	Mon Nov 16 22:20:11 2015 +0000
+++ b/main.cpp	Mon Nov 16 23:44:44 2015 +0000
@@ -33,7 +33,7 @@
 Commander 	_commander = Commander();
 Commander 	*commander = &_commander;
 FPGA 		_fpga = FPGA();
-FPGA		*fpga = &_fpga;
+extern FPGA		*fpga = &_fpga;
 
 extern Block _HazBlock;
 Block *HazBlock = &_HazBlock;
@@ -61,6 +61,7 @@
 	pc.attach(&Rx_interrupt, Serial::RxIrq);
 
 	for (;;) {
+		lcd->cls();
 		i2cport->write_bit(1, 12);
 		lcd->printf("1: Start sorting.");
 		LCDSL();
@@ -69,20 +70,23 @@
 
 		int selection = 0;
 		do {
+			myLED4 = selection;
 			selection = readSwitches();
-		} while (selection == 1 || selection == 2);
-
+		} while (selection != 1 && selection != 2 && connectedToPC == false);
+		D_LEDS_OFF();
 		if (selection == 1) {
 			// User selected op 1: Start sorting autonomously.
-			i2cport->write_bit(0, 13);
+			i2cport->write_bit(1, 12);
 			lcd->cls();
 			LCDFL();
-			lcd->printf("Starting sorting operation");
-			wait(1);
-
+			lcd->printf("Starting sorting");
+			wait(0.5);
+			D_LEDS_OFF();
+			
 			for(;;) {
 				lcd->cls();
-				lcd->printf("Waiting for block");
+				lcd->printf("Waiting...");
+				
 				i2cport->write_bit(1, 15);
 				bool aboardOperation = false;
 
@@ -96,13 +100,15 @@
 						if (aboardOperation == false) {
 							lcd->cls();
 							LCDFL();
-							lcd->printf("Waiting for block");
+							lcd->printf("Waiting...");
+							i2cport->write_bit(1, 15);
 						}
 					}
 				} while (aboardOperation == false && blockInserted != 1);
 
 				// Break and return to main menu i.e. Start Op, or Connect to PC.
 				if (aboardOperation == true) {
+					D_LEDS_OFF();
 					break;
 				}
 
@@ -122,20 +128,25 @@
 				NVIC_EnableIRQ(UART1_IRQn);
 			}
 
-		} else if (selection == 2) {
+		} else if (selection == 2 || connectedToPC == true) {
 			for (;;){
-				// Wait for PC to send '!<pc>connect;' command.
-				lcd->cls();
-				LCDFL();
-				lcd->printf("Waiting for PC...");
-				LCDSL();
-				lcd->printf("4: Main Menu");
-
+                if (!connectedToPC) {
+                    i2cport->write_bit(1, 13);
+                    // Wait for PC to send '!<pc>connect;' command.
+                    lcd->cls();
+                    LCDFL();
+                    lcd->printf("Waiting for PC...");
+                    wait(0.5);
+                    D_LEDS_OFF();
+                    LCDSL();
+                    lcd->printf("4: Main Menu");
+                }
+				i2cport->write_bit(1, 15);
 				int aboardOperation = false;
 				while (connectedToPC == false && aboardOperation == false){
 					aboardOperation = readSwitches() == 4;
 				}
-				if (aboardOperation == true) { break; }
+				if (aboardOperation == true) { D_LEDS_OFF(); break; }
 				else if (connectedToPC == true){
 					lcd->cls();
 					LCDFL();
@@ -147,7 +158,8 @@
 				while (aboardOperation == false && connectedToPC == true){
 					aboardOperation = readSwitches() == 4;
 				}
-				if (aboardOperation == true ) { break; }
+	
+				if (aboardOperation == true ) { D_LEDS_OFF(); break; }
 			}
 		}
 
@@ -157,6 +169,8 @@
 /// Called every-time it receives an char from PC.
 void Rx_interrupt(){
 	char interruptChar = pc.getc();
+	// Uncomment to Echo to USB serial to watch data flow
+        pc.putc(interruptChar);
 
 	NVIC_DisableIRQ(UART1_IRQn);
 
@@ -223,12 +237,20 @@
 
 bool displayAboardDialog()
 {
+	while (i2cport->read_bit(11) == 1){}
+	i2cport->write_bit(1, 12);
+	
 	lcd->cls();
 	LCDFL();
-	lcd->printf("Aboard operation?");
+	lcd->printf("Aboard?");
 	LCDSL();
-	lcd->printf("1: YES, 2,3,4: NO");
-	int reply = readSwitches();
+	lcd->printf("1:YES, 2,3,4:NO");
+	int reply = 0;
+	do {
+		reply = readSwitches();
+	} while(reply == 0);
+
+	D_LEDS_OFF();
 	if (reply == 1) {
 		return true;
 	} else {