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:
}
`
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.
You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps
press Ctrl-T for autoformatting your code
do a rightclick with the mouse and choose "copy for forum"
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
....
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);
}
}