     // change the tilt of the tray
 
     isTrayTiltTimerComplete = false;
-    tiltTray();
+    stopMotorTimer();
     state = TRAY_TILTING;
+    if (currentMotorPosition == LEFT) {
+      tiltMotor(RIGHT);
+    } else {
+      tiltMotor(LEFT);
+    }
+    startMotorTimer();
+    state = IDLE;
   }
   ArduinoOTA.handle();
-  //delay(300);
-  //delay(5000);
 }
 
 // Timer Call Backs
 
 void environmentTimerFinished(void *pArg) {
+  // don't do too much in here. Just set the state
+  // and handle it in the main loop
   isEnvironmentTimerComplete = true;
 }
 



  if (strcmp (WiFi.SSID().c_str(), ssid) != 0) {
//if (strcmp, (WiFi.SSID(),ssid.c_str()) != 0) {
    WiFi.begin(ssid, password);
  }


    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("WiFi connected with OTA4, IP address: ");
  Serial.println(WiFi.localIP());
  publishDebug("WiFi Connected with OTA on IP: "+WiFi.localIP());
  configureOTA();
}

void initMotorTimer() {
  // This timer is for tilting the tray (by activating the motor)
  os_timer_setfn(&trayTiltTimer, trayTiltTimerFinished, NULL);
}

void initEnvironmentTimer() {
  // This timer is for taking temperature and humidity readings
  os_timer_setfn(&environmentTimer, environmentTimerFinished, NULL);
}

void startMotorTimer() {
  os_timer_arm(&trayTiltTimer,30000, true);
}

void stopMotorTimer() {
  os_timer_disarm(&trayTiltTimer);
}

void startEnvironmentTimer() {
  os_timer_arm(&environmentTimer,10000, true);
}

void stopEnvironmentTimer() {
  os_timer_disarm(&environmentTimer);
}

void initSwitches() {
  // Configure the ports for reading the motor switches
  publishDebug("Configuring switch sensors");
  lcdStatus("Init switch sensors");
  pinMode(PORT_MOTOR_SWITCH_LEFT, INPUT_PULLUP);
  pinMode(PORT_MOTOR_SWITCH_RIGHT, INPUT_PULLUP);
  pinMode(BUILTIN_LED, OUTPUT);
  delay(500);
}

void lcdStatus(String message) {
