Hallo Leute,
vielleicht kann mir hier jemand helfen.
Habe ein Projekt von Instructables nachgebaut und stehe nun vor folgendem Problem.
http://www.instructables.com/id/DIY-Bed-LEDs-Time-and-Motion-Activated-Video-Tutor/
Es sind ja verschiedene Zeiten im Sketch angegeben mit Licht an, aus, .. und Zeiten mit PIR.
Ich bräuchte jedoch nur einen Zeitraum von 18:30 bis 7:00, wo das Licht bei Bewegung an geht. Ansonsten soll der PIR nicht reagieren.
Anscheinend bin ich aber zu dämlich und bekomme das nicht hin.
Hier der Sketch:
/*setting up RTCLib*/
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
const int ledPin = 9; //PWM pin for LED fading
const int pirPin = 2; //digital input
const unsigned long motionDelay = 5; //the number of minutes the LEDs stay on if motion is detected
unsigned long currentMillis = 0; //used to store ms since system was turned on
unsigned long prevMillis = 0; //used to count elapsed time
bool ledState = false; //stores whether the LEDs are on or off
bool motionState = false, offState = true; //states used to change what activates the LEDs according to daily cycle
const int onHourAM = 6, onMinuteAM = 30, offHourAM = 8, offMinuteAM = 30; //morning time
const int onHourPM = 20, onMinutePM = 0, offHourPM = 21, offMinutePM = 10; //evening time
/*used to make the code work for both active high and active low motion sensors*/
//#define pirActiveLow //uncomment if your PIR motion sensors is active high, leave this line if your sensor is active low
#ifdef pirActiveLow
bool pirSensSig = LOW;
#else
bool pirSensSig = HIGH;
#endif
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(pirPin, INPUT);
//pinMode(pirPin, INPUT_PULLUP); //uncomment if your PIR sensor needs a pullup resistor. Most PIR sensors don't need this
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); //if RTC hasn't started yet initalize time values from PC
//Used to set RTC with a give date and time. Used for debugging
//If you want the time to be January 21, 2014 at 3am you would call: rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0)
//rtc.adjust(DateTime(2016, 9, 28, 18, 20, 50));
initOn(); //shows the system starting by fading and blinking LEDs
}
void loop()
{
DateTime now = rtc.now(); //read time of day from RTC
printTime(); //prints out the current time
if(((now.hour() == onHourAM) && (now.minute() >= onMinuteAM) && motionState) ||
(now.hour() == onHourPM) && (now.minute() >= onMinutePM) && offState) //if time is late in evening or early morning - fade on LEDs, switch off motionState and offState
{ //reads motionState and offState to make sure LEDs are only faded on once
Serial.println("On state");
ledState = fadeLED(HIGH);
motionState = false;
offState = false;
}
if((now.hour() == offHourPM) && (now.minute() >= offMinutePM) && !motionState) //if time is in the middle of the night - fade off LEDs, switch on motionState
{
ledState = fadeLED(LOW);
motionState = true;
Serial.println("Starting motion detect");
delay(500); //wait half a second after turning off LEDs before starting motion detection
motionRead(); //starts function for monitoring motion from the PIR sensor
DateTime now = rtc.now(); //reads time when motionRead function has ended
}
if((now.hour() >= offHourAM) && (now.minute() >= offMinuteAM) && (now.hour() < onHourPM) && !offState) //if time is in the middle of the day - fade off lights, switch off motionStaate, switch on offState
{
offState = true;
motionState = false;
Serial.println("Off state");
}
//handled in two different conditionals to make sure LEDs are faded off only once when the time is in the middle of the day
if(offState && ledState) //if it's the time of day where LEDs are completely off and they're already on
ledState = fadeLED(LOW); //fades LEDs off and stores state of LEDs
delay(100);
}
/*function to read LED state and fade on or off accordingly*/
bool fadeLED(bool posInc)
{
bool ledState;
int ledVal, incVal;
if(posInc)
{
ledVal = 0; //LEDs starts off and will fade on in for loop
incVal = 1; //used to increase brightness
ledState = true;
Serial.println("Fading on");
}
else
{
ledVal = 255; //LEDs starts on and will fade off in for loop
incVal = -1; //used to decrease brightness
ledState = false;
Serial.println("Fading off");
}
for(int i = 0; i <= 255; i++) //loop to fade LEDs on and off
{
analogWrite(ledPin, ledVal);
ledVal += incVal;
delay(30);
}
return ledState; //returns whether lights are on or off
}
/*function to read motion sensor and turn on lights accordingly*/
void motionRead()
{
while(motionState) //if the system is in motion detect mode
{
DateTime now = rtc.now();
printTime();
currentMillis = millis(); //stores current time in ms
if((now.hour() == onHourAM) && (now.minute() == onMinuteAM)) //read time of day from RTC. if-test to change motionState and break loop
{
Serial.println("No more motion state");
break; //breaks loop and function, goes back to main
}
if(digitalRead(pirPin) == pirSensSig) //if motion is detected
{
prevMillis = currentMillis; //counter is reset each time motion is detected regardless of whether LEDs are on or off
Serial.println("Detecting motion");
if(!ledState) //turns on LEDs only if motion was just detected and LEDs are already off
{
Serial.println("Detecting motion and turning on lights");
ledState = fadeLED(HIGH); //increments the LEDs on and stores state of LEDs
}
}
delay(200);
if(((currentMillis - prevMillis) >= (motionDelay*1000*60)) && ledState) //if it's been the set number of minutes since motion has been detected and LEDs are on
{
ledState = fadeLED(LOW); //fades LEDs off and stores state of LEDs
Serial.println("No more motion");
}
Serial.println(currentMillis - prevMillis);
}
}
/*Function to print the current time to serial monitor. Used for debugging*/
void printTime()
{
DateTime now = rtc.now();
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.println(now.second(), DEC);
}
/*function for showing the system is starting by fading and blinking LEDs*/
void initOn()
{
ledState = fadeLED(HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
delay(500);
ledState = fadeLED(LOW);
delay(500);
Serial.println("Bed leds initialized");
}
Ich haben schon den Autor um Hilfe gebeten, bekomme aber keine Antwort.
Schon mal vielen Dank für eure Unterstützung!!