Controling Servo Loop with Light Sensor

Hi. I’m basically trying to write a loop that controls a servo motor with a light sensor on it.
The servo needs to keep adjusting its angle until the light sensor reaches maximal light reading. I’ve tried different codes, and this one seems to work best. The problem I still have is that the servo gets stuck in either loop and keeps reading, even though it has reached the max. I hope someone can help. :slight_smile:

</>
<
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include “Adafruit_TSL2591.h”
#include <Servo.h>

Servo myservo;
int pos = 90; // initial position

Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591); // pass in a number for the sensor identifier (for your use later)

//
/*
Configures the gain and integration time for the TSL2591 Light Sensor
*/
/
/
void configureSensor(void)
{
// You can change the gain on the fly, to adapt to brighter/dimmer light situations
tsl.setGain(TSL2591_GAIN_HIGH); // 428x gain

// Changing the integration time gives you a longer time over which to sense light
// longer timelines are slower, but are good in very low light situations!
tsl.setTiming(TSL2591_INTEGRATIONTIME_100MS); // shortest integration time (bright light)

}

/**************************************************************************/

void setup(void)
{
Serial.begin(9600);

AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz

myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo.write(pos);
delay(500); //small delay since the robot moves

/* Configure the sensor */
configureSensor();
}

/**************************************************************************/

void loop(void)
{
int tol =10;
int inc = 5;

int ada=0;
int adanew=1;

do{
if (adanew>ada)
{ do{
ada = tsl.getLuminosity(TSL2591_INFRARED);
pos=pos+inc;
myservo.write(pos); // write the position to servo
delay (500);
adanew = tsl.getLuminosity(TSL2591_INFRARED);}
while ((adanew>ada)&&((adanew-ada)<tol)&&(pos<=170));
}

if (ada>adanew)
{ do{
pos=pos-inc;
myservo.write(pos); // write the position to servo
ada = tsl.getLuminosity(TSL2591_INFRARED);
delay (500);
adanew = tsl.getLuminosity(TSL2591_INFRARED);}
while ((adanew<ada)&&((ada-adanew)<tol)&&(pos>90));
}
Serial.print(ada);
if (pos>=170)
{do {pos=pos-5;} while (pos>=90);}
if (pos<=90)
{pos=90;}

} while (((adanew-ada)>tol)&&((ada-adanew)>tol)&&(pos>=90)&&(pos<=170));

}

</>

First, a do/while statement is probably NOT the best choice.

Second, your code is poorly formatted. Thecurlybracesbelongonthenextlineinmyopinionbutataminimumthereneedstobespacesbetweenthemandthecodetheygowith.

Use Tools + Auto Format before posting code. Read the stickies again. The icon to post code looks like </>. The code tags do not.

NOTHING follows the curly brace.

do {pos=pos-5;} while (pos>=90);

Completely f**king useless. Just set pos to whatever new value it is supposed to have, since you are not actually using the value to move a servo.

I think at least part of your problem is: while (((adanew-ada)>tol)&&((ada-adanew)>tol)&&(pos>=90)&&(pos<=170));

This part can never be true: ((adanew-ada)>tol) && ((ada-adanew)>tol)