     if (tgtPos != pos)
     {
       //Serial.print(pos);Serial.print("--");
-      //Serial.println(desiredpos);
+      //Serial.println(tgtPos);
       servo.write(pos);
     
       pos+=direction;
       
-      if (pos>=minPos)
+      if (pos<minPos)
       {
         //Serial.print("Reset down:");
         //Serial.println(counter++);
         direction=-1;
       }
     
-      if (pos<=maxPos)
+      if (pos>maxPos)
       {
         //Serial.print("Reset up:");
         //Serial.println(counter++);


// =========== DC Control using the encoder.
int targetpos = 0;

void setTargetPos(int newtargetpos)
{
  targetpos = newtargetpos;
}

void updatedc(Adafruit_DCMotor *dcmotor, int currentpos)
{
  if (targetpos != currentpos)
  {
    dcmotor->setSpeed(90);

    if (targetpos < currentpos)
      dcmotor->run(FORWARD);
    else
      dcmotor->run(BACKWARD);

  } else {
    dcmotor->setSpeed(0);
  }

}

  }
}

/**
 * Homing works by setting servos to 90 degrees, and later to pull 
 * or push the shoulder DC so that the angle on the arm reads 90 degrees
 * 
 * Then homing is stopped and the encoder is reset to zero.
 */
bool homing=false;

void loop() {
  if (accel.available())
