CONTROLLING LIGHT BY LDR

Light by LDR

#include <avr/interrupt.h>
#include <avr/eeprom.h>
int addr1 = 1;
int readValue1;
int a;
const int buttonPin1 = 9; //press this button for setting darkness when light desire to on
int buttonState1 = 0;

#include <SoftPWM.h>

void setup()
{
pinMode(buttonPin1, INPUT);

digitalWrite(buttonPin1, HIGH);

SoftPWMBegin();
SoftPWMSet(3, 0);
SoftPWMSetFadeTime(3, 3000, 2000);
Serial.begin(9600);

gelap();
}
void loop()
{
buttonState1 = digitalRead(buttonPin1);

if (buttonState1 == LOW) {
a = analogRead(A0);
while (!eeprom_is_ready());
cli();
if(eeprom_read_word((uint16_t*)addr1) != a) {
eeprom_write_word((uint16_t*)addr1, a);
}
sei();

}

else{

action();
}
}
void gelap(){
SoftPWMSet(3, 0);
}
void white(){
SoftPWMSet(3, 255);

}

void action(){
a = analogRead(A0);
readValue1 = eeprom_read_word((uint16_t*)addr1);
if (a < readValue1)
{
white();
}
else
{
gelap();
Serial.print(readValue1);
Serial.print(” “);
Serial.println(a);
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s