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);
}
}
}