Skip to content

Instantly share code, notes, and snippets.

@emiloconan
Last active May 28, 2020 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emiloconan/743209d5ceb620fdc4963bb7a06f39bb to your computer and use it in GitHub Desktop.
Save emiloconan/743209d5ceb620fdc4963bb7a06f39bb to your computer and use it in GitHub Desktop.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
int _brightness ;
int _time ;
int _flag ;
// For these LCD controls to work you MUST replace the standard LCD library from...
// https://github.com/marcoschwartz/LiquidCrystal_I2C
// Direct download https://github.com/marcoschwartz/LiquidCrystal_I2C/archive/master.zip
// Your project will not compile until this is done.
LiquidCrystal_I2C lcd_I2C_27(0x27,16,2); // set the LCD address for a 16 chars and 2 line display
void setup(){
Serial.begin(9600);
lcd_I2C_27.init (); // initialize the lcd
lcd_I2C_27.backlight();
pinMode( 6 , OUTPUT); // set the LED as output
_brightness = 0 ;
_time = 1 ;
_flag = 1 ;
}
void loop(){
Serial.print(analogRead( A0 )); //Potentiometer
Serial.print(" ");
Serial.println();
Serial.print(analogRead( A1 )); //Photoresistor
Serial.print(" ");
Serial.println();
lcd_I2C_27.clear();
if ( analogRead( A1 ) < 3000 && _flag == 1 ) {
_time = constrain( map ( analogRead( A1 ) , 0 , 1023 , 1 , 300 ) , 1 , 300 ) ; //Set the time limit to 300 seconds, it would be possible to change the time limit to higher or lower values by changing the maximum number 300.
lcd_I2C_27.setCursor(6 , 0) ;
lcd_I2C_27.print( _time );
lcd_I2C_27.setCursor(3 , 1) ;
lcd_I2C_27.print( "seconds" ); //the LCD shows the instant time set up by the Potentiometer
delay( 280 );
}
if ( analogRead( A0 ) < 600 && _flag == 1 ) {
digitalWrite( 6 , HIGH ); // Sets the LED on
delay( ( _time * 1000 ) ); //Delay for the time set up
for (int i4 = 0 ; i4 < 255 ; ++i4 ) {
_brightness = ( _brightness - 1.0 ) ; //Decreasing the brightness
analogWrite(6 , _brightness); //LED slowly diminishes
delay( 5 );
}
_flag = 0 ;
// When flag==1, the device is always restarting, so the light will continue to glow.
//The flag is set to 0 so after the glow, so the light only lights up once in the dark.
lcd_I2C_27.setBacklight(LOW); //turn the LCD display dark
}
if( analogRead( A0 ) > 500 && _flag == 0 ) {
delay(3000);
if(analogRead( A0 ) > 500 ){ //If the light is on for 3 seconds or more, the device resets itself so that you could adjust the time limit and turn off the light again with your smart lamp!
_flag = 1 ;
lcd_I2C_27.backlight();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment