Servo Motor not responding to program involving photodetectors

The idea of my program is that there are two photodetectors and a servo motor that either rotates to 0 or 180 based on which photodetector is detecting a higher amount of light. Here is my code:

`#include <Servo.h>

int servoPin=9;
Servo servo;
int ReadL;

int ReadR;

int maxRead;

int difference;
void setup() {
// put your setup code here, to run once:
pinMode(A0,INPUT);
pinMode(A1,INPUT);
Serial.begin(9600);

servo.attach(servoPin);
}

void loop() {
// put your main code here, to run repeatedly:

analogRead(A0);
analogRead(A1);

ReadL=(analogRead(A0));

ReadR=(analogRead(A1));

Serial.print(ReadL);
Serial.print(" ");
Serial.println(ReadR);

difference=abs(ReadR-ReadL);

if (difference>>50) {

maxRead=max(ReadL,ReadR);
if (maxRead==ReadL) {

servo.write(0);

 }

if (maxRead==ReadR) {

    servo.write(180);
  
 }

}

if (difference<=50) {
  
}

}
`
When I run the program, the servo defaults to about 90 degrees and then doesn't respond anymore past that even if the difference between the detectors is way more than 50.

I was wondering what was going wrong and why and if there is a better approach to this program, to begin with.

Please read how to best post your source code, and while editing your post, can you please show us how your equipment is connected together.
Thanks.

The most obvious thing to do is to actually print the value of "difference".
Paul

Hum
if (difference>>50) {
:thinking:

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

this line of code

if (difference>>50) 

does bitshifting to the right 50 bits
as difference is an int (16bit-integer) this means after the bitshifting difference will always contain zero and this is what the if-condition sees as the result

if (0)
is the same as
if (false)

difference with binary value
1234567890123456
0100000000000000

after difference >> 14
1234567890123456
0000000000000001





difference with binary value
1234567890123456
0100000000000000

after difference >> 15
1234567890123456
0000000000000000

after difference >> 16
1234567890123456
0000000000000000
....

best regards Stefan

Here is a code-version that maks visible in the serial monitor what your code is doing

// start of macros dbg and dbgi
#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);
// usage: dbg("1:my fixed text",myVariable);
// myVariable can be any variable or expression that is defined in scope

#define dbgi(myFixedText, variableName,timeInterval) \
  do { \
    static unsigned long intervalStartTime; \
    if ( millis() - intervalStartTime >= timeInterval ){ \
      intervalStartTime = millis(); \
      Serial.print( F(#myFixedText " "  #variableName"=") ); \
      Serial.println(variableName); \
    } \
  } while (false);
// usage: dbgi("2:my fixed text",myVariable,1000);
// myVariable can be any variable or expression that is defined in scope
// third parameter is the time in milliseconds that must pass by until the next time a
// Serial.print is executed
// end of macros dbg and dbgi

#include <Servo.h>

int servoPin = 9;
Servo servo;
int ReadL;
int ReadR;

int maxRead;

int difference;

void setup() {
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  Serial.begin(9600);

  servo.attach(servoPin);
}

void loop() {
  ReadL = (analogRead(A0));
  ReadR = (analogRead(A1));

  dbgi("1:",ReadL,1234); // print "1:",ReadL=value of ReadL once every 1234 milliseconds
  dbgi("2:",ReadR,1234); 

  difference = abs(ReadR - ReadL);
  dbgi("3:",difference,1234); 

  if (difference >> 50) {
    dbgi("if (difference >> 50) evaluated to TRUE",difference,200);
    maxRead = max(ReadL, ReadR);
    dbgi("4:",maxRead,200);
    if (maxRead == ReadL) {
      servo.write(0);
      dbgi("5: servo.write(0); executed",ReadL,200);
    }

    if (maxRead == ReadR) {
      servo.write(180);
      dbgi("6: servo.write(180); executed",ReadR,200);
    }
  }
  
  if (difference <= 50) {
    dbgi("7: if (difference <= 50) evaluated to TRUE",difference,200);
  }
}

best regards Stefan

You may have seen advice to read an input twice, and throw away the first reading.
The two readings should be from the same multiplexer input

Hi, @nate_engineers118
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

This will help with advice on how to present your code and problems.

A circuit diagram will also help.

Are you building a PV array tracker?

This if statement;

if (difference>>50)

should be;

if (difference > 50)

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

Another comment helped solve my problem but thanks so much for the formatting help, I will definitely be better about that in future posts

Hi,
To give this thread closure and info to anyone with similar problems, can you tell us what solved your problem and the final code?

Is it a PV cell tracker?

Thanks.. Tom... :grinning: :+1: :coffee: :australia: