Servo

I am working on a small (litterally) solar cell project, where I am trying to move a solar cell with a servo motor to where the sun is shinning brightest.
I have connected the solar cell to a analog input(A0) - that´s working fine
I have connected the servo to digital I/O (9) - that´s is working fine.
It works seperatly, but when I try to do both at the same time - it does not work. It is almost like one thing cancels out the other - serial monitoring stops working.

#include <Servo.h>

int sensorPin = 0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
Servo servo;
const int ADJUSTMENT_TIME = 350;
const int ADJUSTMENT_RIGHT = 1500;
const int ADJUSTMENT_LEFT = 0;

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600); 
}

void loop()
{
  Serial.print("Beginning new loop:\n");
  
  if(tryMove(0)) 
  {
    
  } 
  else if(tryMove(1))
  {
    
  } 
  else
  {
    Serial.print("We're in best possible position.\n");
   } 
    Serial.print("The end!\n"); 
    delay(10000); 
}
 
int tryMove(int dir) 
  {
    int sensorOriginalPosition = 0; 
    int sensorNewPosition = 0; 
    sensorOriginalPosition = analogRead(sensorPin); 
    Serial.print("try moving the sensor ");
    move(dir); 
    Serial.print(dir > 0 ? "right\n" : "left\n"); //use motor 
    delay(5000); 
    sensorNewPosition = analogRead(sensorPin); 
    Serial.print("First value:\n "); 
    Serial.println(sensorOriginalPosition, DEC);
    Serial.print("Second value:\n "); 
    Serial.println(sensorNewPosition, DEC); 
    if(sensorOriginalPosition < sensorNewPosition) 
    
    { 
    return 1;  
  }
  if(dir > 0)
  {
    move(0);
    } 
    else
    {
    move(1);
    }
  
    return 0; 
   }

void move(int dir) 
{
  servo.attach(9); 
  if(dir > 0){
    servo.writeMicroseconds(ADJUSTMENT_RIGHT);
    Serial.print("Moving right!\n");
  } else {
    servo.writeMicroseconds(ADJUSTMENT_RIGHT);
    Serial.print("Moving left!\n");
  } 
  
  delay(ADJUSTMENT_TIME); 
  servo.detach(); 

}

All help is appreciated!!
Thanks in advance!

Best regards Lars

Why are you attaching and detaching the servo? Some reason not to leave it attached?

The move() function appears to move the servo the same way, regardless of the input value, although it does print different messages.

The reason for attaching and detaching is that this is only way we have been able to control the servo.

True that there is a error in the move function. But why does the serial monitoring stop working when we run this - that we don not understand!

Is possible to look at the source code for the servo library, so that we look at what the code/methods is actually doing in c code??

Is possible to look at the source code for the servo library, so that we look at what the code/methods is actually doing in c code??

Of course it is. The "library" is just a header file and matching source file.

But why does the serial monitoring stop working when we run this - that we don not understand!

There is no reason for the Serial instance to quit working. Other people don't have that issue. My guess is that there is a flaw in your logic that is causing an endless loop in which no serial output is occurring. The logic is hard to follow, though.