어둡지 않으면 켜지지 않는 모션 컨트롤 라이트를 만들려고합니다.모션 컨트롤 라이트
움직임이 감지되면 pwm을 사용하여 표시등을 천천히 켜고 끕니다. 지금 당장은 코드가 작동하고 내가 원하는 거의 모든 것을 수행합니다.
문제는 전체 밝기에 도달 한 후 조명을 켜기 위해 지연 기능을 사용하고 있다는 것입니다. 그리고 빛이 켜져있는 동안 움직임이 감지되면 타이머가 재설정되지 않습니다. 지연 타이머가 만료되고 모션이 감지되면 밝기가 내려간 다음 다시 올라갑니다.
나는 모션 감지기가 주도권을 유지하지만 작동시키지 못했던 타이머를 재설정하고 싶습니다.
http://playground.arduino.cc/Code/AvoidDelay
https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
을하지만 때문에 아두 이노 코드의 내 다소 제한 이해의 나는 작업을 얻을 수 없었다 :
는 나는 이러한 살펴 보았다. 어떤 도움을 주셔서 감사합니다.
는
아래의 작업 코드를 해결했다
const byte MotionPin = 4; // Motionsensor
int LightSensorPin = 2; // Lightsensor
const byte LedPin = 11; // LED
unsigned long MotionDetectedMillis; // When motion was detected
unsigned long ledTurnedOnAt; // when led was turned on
unsigned long turnOnDelay = 50; // wait to turn on LED
unsigned long turnOffDelay = 5000; // turn off LED after this time
bool ledReady = false; // Has motion been detected
bool ledState = false; // Led on or off
int LightSensorState = 0;
int fadeAmount = 5; // How much to fade in a loop
byte brightness = 0;
void setup() {
pinMode(MotionPin, INPUT);
pinMode(LightSensorPin, INPUT);
pinMode(LedPin, OUTPUT);
digitalWrite(LedPin, LOW);
}
void loop() {
// get the time at the start of this loop()
unsigned long currentMillis = millis();
if (digitalRead(MotionPin) == HIGH && digitalRead(LightSensorPin)== HIGH)
{
MotionDetectedMillis = millis();
ledReady = true;
}
if (ledReady)
{
if ((unsigned long)(currentMillis - MotionDetectedMillis) >= turnOnDelay)
{
while (brightness < 255)
{
brightness = brightness + fadeAmount;
analogWrite(LedPin, brightness);
delay(30);
}
// setup our next "state"
ledState = true;
// save when the LED turned on
ledTurnedOnAt = currentMillis;
ledReady = false;
}
}
// see if we are watching for the time to turn off LED
if (ledState)
{
if ((unsigned long)(currentMillis - ledTurnedOnAt) >= turnOffDelay)
{
while (brightness >0)
{
brightness = brightness - fadeAmount;
analogWrite(LedPin, brightness);
delay(30);
}
ledState = false;
}
}
}
두 번째 링크는 문제를 직접 해결하는 훌륭한 자습서입니다. 도움이되지 않는다면 좀 더 구체적인 질문을해야 할 것입니다. 튜토리얼의 어떤 부분이 혼란 스럽습니까? –
내가 도움이 필요한 부분은 내가 어떻게 그 코드를 나의 것에 통합 할 것인가이다. 나는 꽤 많은 시간을 보냈고 단지 작동시키지 못했습니다. – lollero2