 # how do I define the night time in an if statement?

I want to use an RTC with a lightsensor. I wrote 2 if statements, one when its day and one when its night:

the one for the day looks like this

just for overview
a = lightValue
b = hour()

“if (a < 500 && b >= 6 && b <= 21) {…}”

I think this will work. But what do I do about the night? because i cant write that the hour should be less than 6 but also greater that 21.

Will it work if I write it like this:

“if (a >= 500 && b < 6 || b > 21) {…}”?

I cant split the “night if statement” up in two if statements because that would run the code twice and I dont want that.

“if (a >= 500 && b < 6) {…}”
“if (a >= 500 && b >21) {…}”

is there some expression that allows you to say it should be the oppisite of the “day if statement”?

this is the complete (unfinished) code:

``````#include <DS1307RTC.h>
#include <TimeLib.h>
#include <Wire.h>

int lightValue;     //lichtsensor

const unsigned long SECOND = 1000;
const unsigned long MINUTE = 60 * SECOND;

const int motorA = 5;   //motor
const int motorB = 6;
const int motorSpeed = 3;

void setup()
{
pinMode(motorA, OUTPUT);
pinMode(motorB, OUTPUT);
Serial.begin(9600);
setSyncProvider(RTC.get);
adjustTime(10);
}

void printDigits(int digits){
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}

void loop()
{
//motorcontrol
lightValue = analogRead(A0);
Serial.println(lightValue);

//day
if (lightValue < 640 && hour() >= 6 && hour() <= 21){
digitalWrite(motorA, HIGH);
digitalWrite(motorB, LOW);

analogWrite(motorSpeed, 150);
delay(1000);
analogWrite(motorSpeed, 200);
delay(1000);
analogWrite(motorSpeed, 250);
delay(1000);
analogWrite(motorSpeed, 255);
delay(4000);

while (hour() > 6 && hour() < 21){
analogWrite(motorSpeed, 0);
}
}

//night
if (lightValue >= 640 && hour() < 6 || hour() > 21){
digitalWrite(motorA, LOW);
digitalWrite(motorB, HIGH);

analogWrite(motorSpeed, 75);
delay(1000);
analogWrite(motorSpeed, 100);
delay(1000);
analogWrite(motorSpeed, 150);
delay(2600);

while (lightValue >= 640){
analogWrite(motorSpeed, 0);
lightValue = analogRead(A0);
if (lightValue < 640){
delay(5 * MINUTE);
}
lightValue = analogRead(A0);
Serial.println(lightValue);
delay(2 * MINUTE);
}
}

}
``````

Just use an else. If conditions, it's day, else it's night.

a = lightValue
b = hour()

Why not simply use lightValue and hour() ?

``````"if (lightValue < 500 && hour() >= 6 && hour() <= 21)
etc, etc
``````

So much easier to read and understand

UKHeliBob:
Why not simply use lightValue and hour() ?
So much easier to read and understand

i used it in my code. i thought it would be more understandable and better for the overview. Hence

y_u_y:
just for overview
a = lightValue
b = hour()

used it in my code.

I am glad to hear it

i thought it would be more understandable and better for the overview.

In my opinion it would have been better to use what you had in your code, not least because it eliminates the chance of typing errors

okay. for future posts i will remember that and thanks for the help.
I didnt think about "else" 