 	}
 }
 
-void server_HandleNotFound() {
+void server_HandleNotFound() { // Build error message when a request 404's
 	String message = "File Not Found\n\n";
 	message += "URI: ";
 	message += server.uri();

		Serial.print(aleft);
		Serial.print(" ");
		Serial.print(aright);

		Serial.println(" ");
		motor_SetOutputs(nleft,aleft,nright,aright );
	}
}

void motor_SetOutputs(int apina, int apinb, int bpina, int bpinb) { 
 	sx1509.pwm(motor_APinA, apina);
	sx1509.pwm(motor_APinB, apinb);
	sx1509.pwm(motor_BPinA, bpina);
	sx1509.pwm(motor_BPinB, bpinb);
}

void setLEDColour(int r, int g, int b) {
	for (int i = 0; i < 1; i++) { // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    	pixels.setPixelColor(i, pixels.Color(r, g, b));
    	pixels.show(); // sends updated pixel to hardware
  	}
}

void server_SendGood() {
	server.send (200, "text/html", "recieved");
}

int getArg(String arg) {
	return atoi(server.arg(arg).c_str());
}

void server_HandleRoot(){
	Serial.println("Handling Root\n");
	int apina=getArg("apina");
  	int apinb=getArg("apinb");
	int bpina=getArg("bpina");
  	int bpinb=getArg("bpinb");
  	int r=getArg("r");
  	int g=getArg("g");
  	int b=getArg("b");
  	int freq=getArg("freq");
  	int duration=getArg("dur");
  	int restart=getArg("restart");
  	robot_Mode=getArg("mode");
  	if (freq==0) {
  		noTone(tone_Pin);
  	} else if (duration!=0) {
  		tone(tone_Pin,freq,duration);
  		delay(duration);
  		noTone(tone_Pin);
  	} else {	
	  	tone(tone_Pin,freq,1000000);
  	}
 

	motor_SetOutputs(apina,apinb,bpina,bpinb);
	setLEDColour(r,g,b);
	
	String response = "<h1>Update 9-16</h1><h2>Arguments:</h2><p>";
	for (uint8_t i=0; i<server.args(); i++)
		response+="\t"+server.argName(i)+":\t"+server.arg(i)+"\n";

const int motor_BPinA = 2;
const int motor_BPinB = 3;
const int tone_Pin=13;


/*
	Robot can use either a pre-existing wifi network by setting the SSID and PASS variables to the corresponding values and setting HOTSPOT to false
	However, this is not recommended as the personal hotspot for the robot provides a more reliable connection.
	Using existing networks might provide an easier developing experience as it allows connection via ethernet to the robot given the correct IP address which can be obtained from the arduino serial monitor upon boot
*/
const char* esp_SSID = "CarlRobot Gamma";
const char* esp_PASS = "CarlRobot";
const bool 	esp_HOTSPOT = true; 

int robot_Mode=0; 				// 1 = linefollowing, 0 = joystick
unsigned long int loopIter=0;	// Used for flashing the LED when in linefollowing mode, not very important
ESP8266WebServer server(80); 

void setup() {
	Serial.begin(57600); 	// Begin serial, higher baud rate is less stable but faster
	digitalWrite(7, HIGH); 	// Set HIGH output to ESP blue light
	pixels.begin();			// Setup the neopixel LEDs
	setLEDColour(255,0,0);
	Serial.println();

	sx1509.init();  // Initialize the SX1509, does Wire.begin()
	byte clockDivider = 1;
	sx1509.ledDriverInit(motor_APinA);
	sx1509.ledDriverInit(motor_APinA, clockDivider, LOGARITHMIC);
	sx1509.ledDriverInit(motor_APinB);
	sx1509.ledDriverInit(motor_APinB, clockDivider, LOGARITHMIC);
	sx1509.ledDriverInit(motor_BPinA);
	sx1509.ledDriverInit(motor_BPinA, clockDivider, LOGARITHMIC);
	sx1509.ledDriverInit(motor_BPinB);
	sx1509.ledDriverInit(motor_BPinB, clockDivider, LOGARITHMIC);

	// Setup for linefollowing (QRE113) sensors
	sx1509.pinDir(7, OUTPUT); 	// servo
	sx1509.pinDir(8, OUTPUT); 	// ultrasonic trigger
	sx1509.pinDir(9, INPUT); 	// ultrasonic echo
	sx1509.pinDir(10, OUTPUT);  // set SX1509 pin 10 as an output - enables all IR leds
	sx1509.pinDir(11, INPUT);	// set the rest of the sensors to input
	sx1509.pinDir(12, INPUT);
	sx1509.pinDir(13, INPUT);
	sx1509.pinDir(14, INPUT);
	sx1509.pinDir(15, INPUT);

	if (WiFi.status() == WL_NO_SHIELD)
		while(true) // Require the wifishield to boot
			Serial.println("WiFi shield not present");


	if (esp_HOTSPOT) {	// Setup for personal hotspot
		Serial.print("Wireless Access Point SSID: ");
		Serial.println(esp_SSID);
		WiFi.disconnect();
		WiFi.mode(WIFI_AP);
		WiFi.softAP(esp_SSID,esp_PASS);
	} else {			// Setup for existing network
		WiFi.mode(WIFI_STA);
		WiFi.begin(esp_SSID,esp_PASS);
		while (WiFi.status() != WL_CONNECTED) {
