Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- const int ledPin = 12;
- //A0 - LDR pin
- //A1 - potentiometer pin
- //enumerator states is so we can give states a name
- typedef enum states {
- On = 1;
- Off = 0;
- roomLight = 2;
- }
- //initiate state
- int currentState = roomLight;
- void setup() {
- //setup pin behaiviour
- pinMode(ledPin, OUTPUT);
- pinMode(A0, INPUT);
- pinMode(A1, INPUT);
- //start a serial for debugging
- Serial.begin(9600);
- //roomLight state is somewhat the init state
- if(currentState == roomLight) {
- //proceed to the Off state which is expected at room light
- digitalWrite(ledPin, Off);
- }
- }
- void loop() {
- //read the analogInput
- int potValue = analogRead(A1);
- int ldrValue = analogRead(A0);
- //if reading shows its dark enough
- if(ldrValue <= potValue) {
- //if the current state is On
- //there is no need to add delay nor do anything
- //otherwise check again for the read was valid
- if(currentState != On) {
- //add a delay to reduce undesired interference
- delay(2000);
- //if the value still satisfies the condition for switching
- if(ldrValue <= potValue) {
- //turn it on
- digitalWrite(ledPin, On);
- //set the new state
- currentState = On;
- }
- }
- }
- //if we are passed the point of light where we turn off
- else if(ldrValue >= potValue + 200) {
- //if the current state is Off
- //there is no need to add delay nor do anything
- //otherwise check again for the read was valid
- if(currentState != Off) {
- //add a delay to reduce undesired interference
- delay(2000);
- //if the value still satisfies the condition for switching
- if(ldrValue >= potValue + 200) {
- //turn it off
- digitalWrite(ledPin, Off);
- //set the new state
- currentState = Off;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement