Components and supplies
Arduino UNO
Relay (generic)
PIR Motion Sensor (generic)
Project description
Code
Temporizador.h
c_cpp
1#ifndef Temporizador_h 2#define Temporizador_h 3/* 4Title: Temporizador (a counter or timer ) 5Author: Bernardo Lpez Daz 6Description: 7The class Temporizador defines the methods that implement the operation of a timer. 8The loop method is invoked within the generic loop to update the counters. 9The set method allows you to initialize a counter at a given value. 10When you create an object of this class you have to pass the function to be called when the timer ends the account. 11Once the account is finished, the timer does not restart. 12*/ 13// includes files 14#include "Arduino.h" 15 16class Temporizador 17{ 18 19 20 public: 21 Temporizador(void (*_timeoutCallback)()); 22 void set( uint32_t _counter); 23 void loop(); 24 25 private: 26 uint32_t counter; 27 void (*timeoutCallback)(); 28}; 29 30#endif
Temporizador.cpp
c_cpp
This class implements the counter method necessary. The Relay_PIR_counter needs this class to work
1#include "Temporizador.h" 2 3/******* PUBLIC FUNCTIONS RELATED WITH SLAVES AND BASIC BOARDS ************************/ 4 5 6Temporizador::Temporizador(void (*_timeoutCallback)()) 7{ 8 counter = 0; 9 timeoutCallback = _timeoutCallback; 10} 11 12void Temporizador::set(uint32_t _counter){ 13 counter = millis()+_counter; 14 if (counter <millis()){ 15 counter = 0xFFFFFFFF; 16 } 17} 18void Temporizador::loop(){ 19 if (counter == 0){ 20 return; 21 } 22 if(counter <= millis()){ 23 counter = 0; 24 timeoutCallback(); 25 } 26}
Temporizador.cpp
c_cpp
This class implements the counter method necessary. The Relay_PIR_counter needs this class to work
1#include "Temporizador.h" 2 3/******* PUBLIC FUNCTIONS RELATED WITH 4 SLAVES AND BASIC BOARDS ************************/ 5 6 7Temporizador::Temporizador(void 8 (*_timeoutCallback)()) 9{ 10 counter = 0; 11 timeoutCallback 12 = _timeoutCallback; 13} 14 15void Temporizador::set(uint32_t _counter){ 16 counter 17 = millis()+_counter; 18 if (counter <millis()){ 19 counter = 0xFFFFFFFF; 20 } 21} 22void 23 Temporizador::loop(){ 24 if (counter == 0){ 25 return; 26 } 27 if(counter 28 <= millis()){ 29 counter = 0; 30 timeoutCallback(); 31 } 32}
Relay_PIR_timer.ino
arduino
Manage the time a device remains activated (lights on) when no presence is detected
1/* 2 * Title: MyPip&Rele 3 * Author: Bernardo Lpez Daz 4 * Description: 5 Manage the time a device remains activated (lights on) when no presence is detected 6 The hw devices that make up the project are: 7 1. Arduino board 8 2. Presence detector (PIR) connected to digital input 2 of the Arduino board 9 3. Relay that makes the voltage switch that feeds the bulb. It is connected to the digital output 3 of the Arduino board 10 4. 5 volt power supply for the Arduino board. 11 Start a counter (120 seconds). When the timer expires,acts on a relay to open the circuit to which the light is connected (turn on light). The timer restarts each time the PIR detects presence. 12 13*/ 14#include <Temporizador.h> 15 16// constants won't change. They're used here to set pin numbers: 17const int pipPin = 2; // the number of the PIR pin 18const int relePin = 3; // the number of the Rele pin 19const int ledPin = 13; // the number of the LED pin 20const uint32_t alarm = 120000; // When the PIR is activate the counter restart a new count. 21uint8_t pipState = LOW; 22 23Temporizador count(&activarRele); 24 25void setup() { 26 // initialize the LED pin as an output: 27 pinMode(ledPin, OUTPUT); 28 // initialize the pushbutton pin as an input: 29 pinMode(pipPin, INPUT); 30 pinMode(relePin, OUTPUT); 31 Serial.begin(115200); 32} 33 34void loop() { 35 // read the state of the pushbutton value: 36 pipState = digitalRead(pipPin); 37 count.loop(); 38 // check if the pushbutton is pressed. If it is, the buttonState is HIGH: 39 if (pipState == HIGH) { 40 // turn LED on: 41 digitalWrite(ledPin, HIGH); 42 digitalWrite(relePin, HIGH); 43 count.set(alarm); 44 } else { 45 // turn LED off: 46 digitalWrite(ledPin, LOW); 47 } 48 delay(20); 49} 50 51void activarRele(){ 52 digitalWrite(ledPin, LOW); 53 digitalWrite(relePin, LOW); //When the timer is over the ligth is off 54} 55 56
Temporizador.h
c_cpp
1#ifndef Temporizador_h 2#define Temporizador_h 3/* 4Title: Temporizador (a counter or timer ) 5Author: Bernardo Lpez Daz 6Description: 7The class Temporizador defines the methods that implement the operation of a timer. 8The loop method is invoked within the generic loop to update the counters. 9The set method allows you to initialize a counter at a given value. 10When you create an object of this class you have to pass the function to be called when the timer ends the account. 11Once the account is finished, the timer does not restart. 12*/ 13// includes files 14#include "Arduino.h" 15 16class Temporizador 17{ 18 19 20 public: 21 Temporizador(void (*_timeoutCallback)()); 22 void set( uint32_t _counter); 23 void loop(); 24 25 private: 26 uint32_t counter; 27 void (*timeoutCallback)(); 28}; 29 30#endif
Comments
Only logged in users can leave comments
berlopdia
0 Followers
•0 Projects
Table of contents
Intro
20
0