#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int LDR_pin = A0; // pin that the LDR sensor is attached to
const int set_pot_pin = A1;
const int led_light_pin = 9; // pin that the LED array is attached to
// variables:
int sensorValue = 0; // the sensor value
int sensorMin = 1023; // minimum sensor value
int sensorMax = 0; // maximum sensor value
int threshold = 0;
int light_intensity = 55, led_intensity = 20;
void setup()
{
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c)) { // Address 0x3D for 128x64
// Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.print("calibrating.....");
display.display();
analogWrite(led_light_pin, 0);
digitalWrite(13, HIGH);
while (millis() < 10000)
{
sensorValue = analogRead(LDR_pin);
if (sensorValue > sensorMax) sensorMax = sensorValue;
if (sensorValue < sensorMin) sensorMin = sensorValue;
}
display.clearDisplay();
display.print("calibration over");
display.setCursor(0, 1);
display.print("max light:");
display.print(sensorMax);
display.setCursor(0, 2);
display.print("min light:");
display.print(sensorMin);
display.display();
delay(2000);
display.clearDisplay();
display.print("light intensity");
display.setCursor(0, 2);
display.print("threshold:");
display.setCursor(0, 3);
display.print("LED light:0%");
display.display();
}
void loop()
{
sensorValue = analogRead(LDR_pin);
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 100);
sensorValue = constrain(sensorValue,0,100);
display.setCursor(0, 1);
display.print(sensorValue);
display.print('%');
threshold = analogRead(set_pot_pin);
threshold = map(threshold, 0, 1023, 0, 100);
display.setCursor(11, 2);
display.print(threshold);
display.print('%');
if (sensorValue < threshold)
{
analogWrite(led_light_pin, light_intensity);
display.setCursor(11, 3);
display.print(led_intensity);
display.print('%');
if(light_intensity<255)
{
light_intensity += 25;
led_intensity += 10;
}
}
display.display();
delay(2000);
}