#include <Servo.h> //includes the servo library
/*create servo object*/
Servo myServo; //creates the servo object
/*define pins for the components*/
const int bluLedPin = 5; //RED LED TO PIN 5
const int grnLedPin = 6; //BLUE LED TO PIN 6
const int offSlideSwitchPin = 4; //off is pin 4
const int onSlideSwitchPin = 2; //on is pin 2
const int buzzerPin = 7; //buzzer pin 12
const int LDRpin = A0;
bool onSlideSwitchState = true;
bool offSlideSwitchState = true;
const int ServoPin = 9;
/*define variables*/
int pos = 0; //starting position of the servo
int bluLedState; //variable for the state of blu led
int grnLedState; //variable for the state of grn led
int slideSwitchState;
//int onSlideSwitchState;
//int offSlideSwitchState;
int luxAmount; //variable to store Lux value
int luxThreshold = .2; //threshold for the lux value
unsigned long currentMillis = 0; //variable to store current time
unsigned long previousMillis = 0; //variable to store previous time
unsigned long elapsedTime = 0; //variable to store elapsed time
const unsigned long period = 1000; //variable to store the period to check periods of time
void onOffSwitch(){
/*turns on or off the led depending on switch position on/off*/
if (onSlideSwitchState == HIGH) {digitalWrite(grnLedPin, HIGH);}
else {digitalWrite(grnLedPin, LOW);}
if (offSlideSwitchState == HIGH) {digitalWrite(bluLedPin, HIGH);}
else {digitalWrite(bluLedPin, LOW);}
currentMillis = previousMillis;
if (currentMillis - previousMillis <= period) {digitalRead(onSlideSwitchPin);}
if (onSlideSwitchState == LOW) {digitalWrite(bluLedPin, HIGH);}
else {digitalWrite(bluLedPin, LOW);}
/*end "turns on or off" the led depending on switch position on/off*/
}
void setup(){
Serial.begin(115200); //begin the serial monitor
pinMode(bluLedPin, OUTPUT); //SETS PIN 5 AS OUTPUT
pinMode(grnLedPin, OUTPUT); //SETS PIN 6 AS AN OUTPUT
pinMode(onSlideSwitchPin, INPUT); //SETS PIN 2 AS AN INPUT
pinMode(offSlideSwitchPin, INPUT); //SETS PIN 4 AS AN INPUT
pinMode(LDRpin, INPUT); //SETS PIN a0 AS AN INPUT
pinMode(buzzerPin, OUTPUT);
digitalWrite(buzzerPin, LOW);
digitalWrite(bluLedPin, LOW); //TURNS blue LED off
digitalWrite(grnLedPin, HIGH); //TURNS green led ON
digitalWrite(offSlideSwitchPin, HIGH); //off switch is on or high (system is off)
digitalWrite(onSlideSwitchPin, LOW); //on switch is off or low (system is off)
}
void loop(){
myServo.attach(9);
myServo.write(180);
bluLedState = digitalRead(bluLedPin); //reads the current state of the blue led ie on or off
grnLedState = digitalRead(grnLedPin); //reads the current state of the redled ie on or off
onSlideSwitchState = digitalRead(onSlideSwitchPin); //reads the current state of the on switch ie on or off
offSlideSwitchState = digitalRead(offSlideSwitchPin); //reads the current state of the off switch ie on or off
digitalRead(grnLedState);
digitalRead(onSlideSwitchState);
currentMillis = previousMillis;
if (currentMillis - previousMillis <= period){onOffSwitch();}
//Lux
double Light (int RawADC0)
double Vout=RawADC0*0.0048828125;
//int lux=500/(10*((5-Vout)/Vout));//use this equation if the LDR is in the upper part of the divider
int lux=(2500/Vout-500)/10;
return lux;
//luxAmount = analogRead(LDRpin);
//Serial.println(luxAmount);
Serial.println(int(Light(analogRead(0))));
delay(1000);
if ((grnLedState == HIGH) && (lux >= 250)){digitalWrite(buzzerPin, LOW);} else {digitalWrite(buzzerPin, HIGH);}
if ((grnLedState == HIGH) && (luX == 0)){myServo.write(180);}
}
void blinkGrnLed(){
digitalWrite(grnLedPin, LOW);
delay(500);
digitalWrite(grnLedPin, HIGH);
delay(500);
digitalWrite(grnLedPin, LOW);
delay(500);
digitalWrite(grnLedPin, HIGH);
delay(500);
}
/*
if ((Lux >= luxThreshold) && (grnLedState == HIGH)) {
digitalWrite(buzzerPin, LOW);}
if ((Lux <= luxThreshold) && (grnLedState == HIGH)) {
digitalWrite(buzzerPin, HIGH);
}*/
/*currentMillis = millis();
elapsedTime = currentMillis - previousMillis;
//if (elapsedTime < 30000) {digitalWrite(buzzerPin, HIGH);}
//else {digitalWrite(buzzerPin, LOW);}
previousMillis = currentMillis;
}*/
/*{for (pos = 90; pos <= 180; pos += 1) { // goes from 0 degrees to 90 degrees in steps of 1 degree
myServo.write(pos); // tell servo to go to position in variable 'pos'
delay(15);} // waits 15ms for the servo to reach the position
for (pos = 180; pos >= 90; pos -= 1) { // goes from 90 degrees to 0 degrees
myServo.write(pos); // tell servo to go to position in variable 'pos'
delay(15);} // waits 15ms for the servo to reach the position
for (pos = 0; pos == 0; pos == 0){
myServo.write(90);
delay(15);
}*/