Skip to content

Instantly share code, notes, and snippets.

@yarogniew
Last active January 6, 2019 23:15
Show Gist options
  • Save yarogniew/f3dd80f3c93ce32b6752c8b6b4f9d84d to your computer and use it in GitHub Desktop.
Save yarogniew/f3dd80f3c93ce32b6752c8b6b4f9d84d to your computer and use it in GitHub Desktop.
/*==========================
Czujnik ruchu RCWL-0516
Załadowany do Atmega328 No 01
Jarogniew Milewski
http://yarogniew.net/arduino/
============================*/
#include "Timer.h"
Timer t;
#define MotionSensor 12
#define ledMotion 7
#define relay 8
int afterEvent;
int tickEvent;
unsigned long currentTime;
bool blk1 = false; // blokada pętli
unsigned long zwloka = 30*1000L;
void setup() {
// Serial.begin(9600);
pinMode (MotionSensor, INPUT);
pinMode (ledMotion, OUTPUT);
pinMode (relay, OUTPUT);
// Serial.println("Sensor gotowy");
currentTime = millis()/1000;
}
void loop() {
t.update();
bool IsMove = digitalRead(MotionSensor); // stan sensora ruchu
if(IsMove && !blk1)
{
digitalWrite(ledMotion, HIGH);
digitalWrite(relay, HIGH);
// Serial.println("Wykryto ruch włączam światło");
blk1 = true;
t.stop(afterEvent);
t.stop(tickEvent);
}
if( !IsMove && blk1)
{
digitalWrite(ledMotion, LOW);
Serial.println("Brak ruchu");
blk1 = false;
afterEvent = t.after(zwloka, offLight);
currentTime = millis()/1000;
everyOneSecond();
}
}
void offLight()
{
// Serial.println(" wyłączam światło");
digitalWrite(relay, LOW);
t.stop(tickEvent);
}
void everyOneSecond()
{
tickEvent = t.every(1000, podajemyCzas);
}
void podajemyCzas()
{
Serial.println((millis()/1000) - currentTime );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment