Changing a Sketch

Hi guys , i want to change a sketch for my project. The sketch is made by Great Scott, it is "Solar Tracker". I want to change that in that way that i only want servo to rotate left and right. I try to figure out it by myself ofc , but no matter what i do ( even with the original sketch) my servo only rotate with 90 degree angle, not 180. I use micro servo SG90.
And this is an original code :

int topleft;
int topright;
int downleft;
int downright;
int waittime = 1;

void setup() {
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
TCCR1A = 0;
TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM11);
TCCR1B = 0;
TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS11);
ICR1 = 40000;
OCR1A = 3000;
OCR1B = 3600;
}

void loop() {
topleft = analogRead(A0);
topright = analogRead(A1);
downleft = analogRead(A2);
downright = analogRead(A3);

if (topleft > topright) {
OCR1A = OCR1A + 1;
delay(waittime);
}
if (downleft > downright) {
OCR1A = OCR1A + 1;
delay(waittime);
}
if (topleft < topright) {
OCR1A = OCR1A - 1;
delay(waittime);
}
if (downleft < downright) {
OCR1A = OCR1A - 1;
delay(waittime);
}
if (OCR1A > 4000) {
OCR1A = 4000;
}
if (OCR1A < 2000) {
OCR1A = 2000;
}
if (topleft > downleft) {
OCR1B = OCR1B - 1;
delay(waittime);
}
if (topright > downright) {
OCR1B = OCR1B - 1;
delay(waittime);
}
if (topleft < downleft) {
OCR1B = OCR1B + 1;
delay(waittime);
}
if (topright < downright) {
OCR1B = OCR1B + 1;
delay(waittime);
}
if (OCR1B > 4200) {
OCR1B = 4200;
}
if (OCR1B < 3000) {
OCR1B = 3000;
}
}

I also need to say that I'm maybe not new( cause i made some projects) but I'm not an expert.
Sorry for terrible English :slight_smile:

Why not use the Servo library? It would, in my opinion, be easier and certainly more easy to follow the code.

See the sweep example in the Servo library examples to get a start.

I thinking about that and change it to something like that. But one of the resistors seems to not react , only by covering that reacting i have some movement. That is the code :
int left;
int right;
int waittime = 0;

void setup() {
pinMode(9, OUTPUT);
TCCR1A = 0;
TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM11);
TCCR1B = 0;
TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS11);
ICR1 = 40000;
OCR1A = 3000;
OCR1B = 3600;
}

void loop() {
left = analogRead(A0);
right = analogRead(A1);

if (left > right) {
OCR1A = OCR1A + 1;
delay(waittime);
}

if (left < right) {
OCR1A = OCR1A - 1;
delay(waittime);
}

if (OCR1A > 4000) {
OCR1A = 4000;
}
if (OCR1A < 2000) {
OCR1A = 2000;
}
}

Post a schematic of your project. We need to see that whether you stay with the current code or try to do it with the Servo library. With your schematic I may be able to help you do it with the Servo library.

Do you understand the code that you posted? I can guarantee that code using the Servo library will be easier to understand.

int waittime = 0;

At any rate, the servo needs time to move. 0 milliseconds is not enough time.

Here is a simple light tracker to illustrate using the Servo library. I don't use external resistors for the LDRs. I wire the LDR from ground and to the analog input with the internal pullup enabled for the LDR load resistor.

// simple light tracker using Servo library by C Goulding
#include <Servo.h>

Servo trackerServo;

const byte east_ldrPin = A0;
const byte west_ldrPin = A1;
const byte trackerServoPin = 4;

void setup()
{
   Serial.begin(115200);
   pinMode(east_ldrPin, INPUT_PULLUP);
   pinMode(west_ldrPin, INPUT_PULLUP);
   trackerServo.attach(trackerServoPin);
}

void loop()
{
   static byte trackerServoPos = 90;
   static unsigned long timer = 0;
   unsigned long interval = 20;  // change to set tracking speed
   if (millis() - timer >= interval)
   {
      timer = millis();
      int eastReading = analogRead(east_ldrPin);
      int westReading = analogRead(west_ldrPin);
      int error = eastReading - westReading;
      //Serial.print(F("error  "));
      //Serial.print(error);
      if(error <= -3)
      {
        trackerServoPos ++;
        if(trackerServoPos > 180)
        {
          trackerServoPos = 180;
        }
      }
      else if (error >= 3)
      {
        trackerServoPos --;
        if(trackerServoPos <= 0)
        {
          trackerServoPos = 0;
        }
      }
      //Serial.print(F("   trackerServoPos  "));
      //Serial.println(trackerServoPos);
      trackerServo.write(trackerServoPos);
   }
}

I'm using that schematic with 1kohm resistors.

Here is the schematic to go with my posted code.