Hey guys, I am working on a smart lighting project which have the ability for me to turn on the lights manually and adjust the preset brightness and could also have it on auto mode which the motion and LDR sensor would take over the control. I am able to run both the auto and manual program individually and working but the problem comes when I merge the two programs together into a single sketch. I am still able to turn on and off the LED lights manually but the program is unable to enter the while loop when "k" was sent. Below are my current code progress
//LDR_PIR Digital Without Dimmer
//Initialization and calibration code
char incoming_value = 0; // default value for bluetooth communication
int dimLed = 5; //First LED
int dimLed2 = 6; //Second LED
const int DUTY_25 = 32; //Preset Brightness level to 25% for LED 1
const int DUTY_50 = 64; //Preset Brightness level to 50% for LED 1
const int DUTY_75 = 128; //Preset Brightness level to 75% for LED 1
const int DUTY2_25 = 32; // Preset Brightness level to 25% for LED 2
const int DUTY2_50 = 64; // Preset Brightness level to 25% for LED 2
const int DUTY2_75 = 128; // Preset Brightness level to 25% for LED 2
int pirsensor = 2; //pir sensor to be at pin 2
int calibrationTime = 30; //calibration timing for PIR set at 30 sec
void setup()
{
Serial.begin(9600);
pinMode(dimLed, OUTPUT); //declaring dimLed as the output for LED 1
pinMode(dimLed2, OUTPUT); //declaring dimLed as the output for LED 2
pinMode(pirsensor, INPUT); //PIR as input
pinMode(A0, INPUT); //A0 connects to LDR as input
Serial.println("Waiting for the sensor to warm up.");
delay(calibrationTime * 1000); // Convert the time from seconds to milliseconds.
Serial.println("SENSOR ACTIVE");
}
void loop()
{
if (Serial.available()) //Any incoming value
{
Serial.println(analogRead(A0));
pirsensor = digitalRead(2); //pirsensor value between 1 or 0
Serial.println(digitalRead(2)); //show PIR value on serial monitor
delay(300);
char incoming_value = Serial.read(); //whatever read on serial to store into incoming_value
Serial.println(incoming_value); //show the value read by Serial.read
// Manual mode
if (incoming_value == '7') //when user send '7' , turn on 1st led
{
digitalWrite(dimLed, HIGH);
}
else if (incoming_value == '8') //when user send '8' , turn off 1st led
{
digitalWrite(dimLed, LOW);
}
else if (incoming_value == 'c') . //when user send 'c' , adjust brightness level to 25%
{
analogWrite( dimLed, DUTY_25 );
}
else if (incoming_value == 'd') //when user send 'd' , adjust brightness level to 50%
{
analogWrite( dimLed, DUTY_50 );
}
else if (incoming_value == 'e') //when user send 'e' , adjust brightness level to 75%
{
analogWrite( dimLed, DUTY_75 );
}
if (incoming_value == 'f') //LED 2 be same as first LED program
{
digitalWrite(dimLed2, HIGH);
}
else if (incoming_value == 'g')
{
digitalWrite(dimLed2, LOW);
}
else if (incoming_value == 'h')
{
analogWrite( dimLed2, DUTY2_25 );
}
else if (incoming_value == 'i')
{
analogWrite( dimLed2, DUTY2_50 );
}
else if (incoming_value == 'j')
{
analogWrite( dimLed2, DUTY2_75 );
}
}
else if (incoming_value == 'k') // Activating "Auto" mode when incoming_value is "k"
{
while ( analogRead(A0) > 0 ) // Upon receiving "k", enters while loop
{
if (analogRead(A0) > 400 && digitalRead(2) == HIGH) . // When LDR reading is above 400 and PIR detected motion = HIGH
{
digitalWrite(dimLed, HIGH); //LDR reading > 400 indicating sky is dark
digitalWrite(dimLed2, HIGH); //Both LED will turned on automatically
Serial.println("Sky is dark and motion is detected"); // Displaying current status in Auto mode
}
else if (analogRead(A0) > 400 && digitalRead(2) == LOW) // When LDR reading is above 400 and PIR detected no motion = LOW
{
digitalWrite(dimLed, LOW); //Both LED will remained off
digitalWrite(dimLed2, LOW);
Serial.println("Sky is Dark and no motion is detected"); // Display current status
}
else if (analogRead(A0) < 400 && digitalRead(2) == LOW) // When LDR reading is below 400 and PIR detected no motion = LOW
{
digitalWrite(dimLed, LOW); //Both LED will remained off
digitalWrite(dimLed2, LOW);
Serial.println("Sky is bright and no motion is detected"); // Display current status
}
else if (analogRead(A0) < 400 && digitalRead(2) == HIGH) // When LDR reading is below 400 and PIR detected motion = HIGH
{
digitalWrite(dimLed, LOW); //Both LED remained off
digitalWrite(dimLed2, LOW);
Serial.println("Sky is bright and motion is detected"); //Display current status
}
}
}
}
