Light Optimization

In this project, I try to get to the best light value and the best shadow lines in my stadium by opening and closing the panels. To reach this goal, I defined 3 conditions in my code to move the panels according to the light sensor values.

Diagrams

Parts & Components


Code

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>

Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);

void displaySensorDetails(void)
{
sensor_t sensor;
tsl.getSensor(&sensor);
Serial.println(“————————————“);
Serial.print  (“Sensor:       “); Serial.println(sensor.name);
Serial.print  (“Driver Ver:   “); Serial.println(sensor.version);
Serial.print  (“Unique ID:    “); Serial.println(sensor.sensor_id);
Serial.print  (“Max Value:    “); Serial.print(sensor.max_value); Serial.println(” lux”);
Serial.print  (“Min Value:    “); Serial.print(sensor.min_value); Serial.println(” lux”);
Serial.print  (“Resolution:   “); Serial.print(sensor.resolution); Serial.println(” lux”);
Serial.println(“————————————“);
Serial.println(“”);
delay(500);
}

void configureSensor(void)
{
/* You can also manually set the gain or enable auto-gain support */
// tsl.setGain(TSL2561_GAIN_1X);      /* No gain … use in bright light to avoid sensor saturation */
// tsl.setGain(TSL2561_GAIN_16X);     /* 16x gain … use in low light to boost sensitivity */
tsl.enableAutoRange(true);            /* Auto-gain … switches automatically between 1x and 16x */

/* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) */
tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);      /* fast but low resolution */
// tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS);  /* medium resolution and speed   */
// tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS);  /* 16-bit data but slowest conversions */

/* Update these values depending on what you’ve set above! */
Serial.println(“————————————“);
Serial.print  (“Gain:         “); Serial.println(“Auto”);
Serial.print  (“Timing:       “); Serial.println(“13 ms”);
Serial.println(“————————————“);
}

#include <Stepper.h>
#define STEPS 200
const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 4, 5, 6, 7);

int angle=0;

void setup(void)
{
Serial.begin(9600);
Serial.println(“Light Sensor Test”); Serial.println(“”);

delay(5000);
/* Initialise the sensor */
if(!tsl.begin())
{
/* There was a problem detecting the ADXL345 … check your connections */
Serial.print(“Ooops, no TSL2561 detected … Check your wiring or I2C ADDR!”);
while(1);
}

/* Display some basic information on this sensor */
displaySensorDetails();

/* Setup the sensor gain and integration time */
configureSensor();

/* We’re ready to go! */
Serial.println(“”);
}

void loop(void)
{
/* Get a new sensor event */
sensors_event_t event;
tsl.getEvent(&event);

/* Display the results (light is measured in lux) */
if (event.light)
{

while (event.light < 400 && event.light > 300)
{
Serial.println(event.light);
myStepper.setSpeed(0);
angle=angle;
}

while (event.light >= 400 && angle > 0)
{
myStepper.setSpeed(20);
Serial.println(“lux:”);
Serial.println(event.light);
myStepper.step(-10);
angle=angle-18;
delay(5000);
}

while (event.light <= 300 && angle < 90)
{
myStepper.setSpeed(20);
Serial.println(“lux:”);
Serial.println(event.light);
myStepper.step(10);
angle=angle+18;
delay(5000);
}
//Serial.print(event.light); Serial.println(“lux”);
}
else
{
/* If event.light = 0 lux the sensor is probably saturated
and no reliable data could be generated! */
Serial.println(“Sensor overload”);
}
//delay(250);
}


My video link : https://vimeo.com/149685081