Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*~
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2016 Łukasz Tretyn

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
67 changes: 32 additions & 35 deletions Timers.cpp
Original file line number Diff line number Diff line change
@@ -1,51 +1,48 @@
#include "Timers.h"

void NOP(void)

void Timer::restart()
{
return;
_lastTime = millis();
}

Timers::Timers(void)
void Timer::begin(const uint32_t interval)
{
for (int i=0; i<TIMER_ITEMS; i++)
{
_elements[i].func = NOP;
_elements[i].interval = 0;
_elements[i].begin_time = 0;
}
time(interval);
restart();
}

void Timers::attach(byte slot, unsigned long interval, timerFunc func)
bool Timer::available()
{
_elements[slot].func = func;
_elements[slot].interval = interval;
_elements[slot].begin_time = millis();
if (_time == 0)
{
return false;
}

uint32_t actualTime = millis();
uint32_t deltaTime = actualTime - _lastTime;
if (deltaTime >= _time)
{
return true;
}

return false;
}

void Timers::setInterval(byte slot, unsigned long interval)
uint32_t Timer::time()
{
_elements[slot].interval = interval;
_elements[slot].begin_time = millis();
if (_time == 0)
{
return 0;
}

uint32_t actualTime = millis();
uint32_t deltaTime = actualTime - _lastTime;

return _time - deltaTime;
}

void Timers::process(void)
void Timer::time(const uint32_t interval)
{
unsigned long actual_time = millis();

for (int i=0; i<TIMER_ITEMS; i++)
{
long long delta_time = actual_time - _elements[i].begin_time;

if (delta_time < 0)
{
delta_time += 0xffffffff;
delta_time += 1;
}
if (_elements[i].interval > 0 && delta_time >= _elements[i].interval)
{
_elements[i].func();
_elements[i].begin_time = actual_time;
}
}
_time = interval;
}

48 changes: 19 additions & 29 deletions Timers.h
Original file line number Diff line number Diff line change
@@ -1,36 +1,26 @@
#include "Arduino.h"
#ifndef _Timers_h
#define _Timers_h

#ifndef timers_h
#define timers_h

#ifndef TIMER_ITEMS
#define TIMER_ITEMS 8
#endif

typedef void (*timerFunc)(void);


void NOP(void);


struct TimerElement
{
timerFunc func;
unsigned long interval;
unsigned long begin_time;
};
#include <Arduino.h>
#include <inttypes.h>

#define SECS(t) (unsigned long) (t * 1000)
#define MINS(t) SECS(t) * 60
#define HOURS(t) MINS(t) * 60
#define STOP 0

class Timers
class Timer
{
private:
struct TimerElement _elements[TIMER_ITEMS];

public:
Timers(void);
void attach(byte slot, unsigned long interval, timerFunc func);
void setInterval(byte slot, unsigned long interval);
void process(void);
private:
uint32_t _time;
uint32_t _lastTime;

public:
void begin(const uint32_t);
void restart();
bool available();
uint32_t time();
void time(const uint32_t);
};

#endif
34 changes: 34 additions & 0 deletions examples/analog_beeper/analog_beeper.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Głośnik podłączony do pinu 3 genetuje przerywane sygnały dźwiękowe
// Sygnały są tym częstsze, im wyższe jest napięcie na wejściu analogowym A0
// Autor: Łukasz Tretyn - http://nettigo.pl

#include <Timers.h>

// config
const byte SPEAKER_PIN = 3;
const byte ANALOG_IN_PIN = 0;
const unsigned long MAX_BEEP_CYCLE = SECS(1);

Timer beeperTimer;

void beeperSetup() {
beeperTimer.begin(STOP);
}

void beeperUpdate() {
word analogData = analogRead(ANALOG_IN_PIN);
unsigned long beepTime = map(analogData, 0, 1023, 0, MAX_BEEP_CYCLE);
beeperTimer.time(beepTime);
if (beeperTimer.available()) {
beeperTimer.restart();
tone(SPEAKER_PIN, 500, beepTime / 2);
}
}

void setup() {
beeperSetup();
}

void loop() {
beeperUpdate();
}
51 changes: 51 additions & 0 deletions examples/auto_light_timer/auto_light_timer.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Automatyczny wyłącznik światła
// 5 niezależnych kanałów
// Świeci przez 1 minutę od wciśniecia przycisku
// Wejścia z rezystorem podciągającym dla przycisków/czujników podczerwieni
// na pinach cyfrowych 2, 3, 4, 5, 6
// Cyfrowe wyjścia dla tranzystorów MOSFET/Przekaźników
// na pinach cyfrowych 7, 8, 9, 10, 11
// Autor: Łukasz Tretyn - http://nettigo.pl

#include <Timers.h>

// Config
const byte LIGHTS_NUM = 5;
const byte switchPins[LIGHTS_NUM] = {2, 3, 4, 5, 6};
const byte lightPins[LIGHTS_NUM] = {7, 8, 9, 10, 11};
const unsigned long LIGHTS_ON_TIME = MINS(1);
const byte LIGHT_ON_STATE = HIGH;
const byte SWITCH_ON_STATE = LOW;

Timer lightTimers[LIGHTS_NUM];

void lightSetup() {
for (byte i=0; i<LIGHTS_NUM; i++) {
lightTimers[i].begin(STOP);
pinMode(switchPins[i], INPUT_PULLUP);
pinMode(lightPins[i], OUTPUT);
}
}

void lightUpdate() {
for (byte i=0; i<LIGHTS_NUM; i++) {
byte pinState = digitalRead(switchPins[i]);
if (pinState == SWITCH_ON_STATE) {
lightTimers[i].begin(LIGHTS_ON_TIME);
digitalWrite(lightPins[i], LIGHT_ON_STATE);
}

if (lightTimers[i].available()) {
lightTimers[i].time(STOP);
digitalWrite(lightPins[i], !LIGHT_ON_STATE);
}
}
}

void setup() {
lightSetup();
}

void loop() {
lightUpdate();
}
19 changes: 19 additions & 0 deletions examples/blink/blink.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Mruga wbudowaną diodą świecącą przy pomocy biblioteki Timers
// Autor: Łukasz Tretyn - http://nettigo.pl

#include <Timers.h>

Timer ledBlinkTimer;

void setup() {
pinMode(LED_BUILTIN, OUTPUT);
ledBlinkTimer.begin(SECS(2));
}

void loop() {
if (ledBlinkTimer.available())
{
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
ledBlinkTimer.restart();
}
}
62 changes: 62 additions & 0 deletions examples/smooth_blink/smooth_blink.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Płynnie zapala i gasi diodę świecącą podłączoną do pinu PWM 3
// Autor: Łukasz Tretyn - http://nettigo.pl

#include <Timers.h>

const byte PWM_LED = 3;
const unsigned long FADE_IN_TIME = SECS(2.25);
const unsigned long FADE_OUT_TIME = SECS(1.8);
const unsigned long CYCLE_DELAY_TIME = SECS(0.75);

Timer timer;

enum class BlinkState : byte {
DELAY,
FADE_IN,
FADE_OUT
} state;

void blinkSetup() {
pinMode(PWM_LED, OUTPUT);
timer.begin(CYCLE_DELAY_TIME);
}

void blinkUpdate() {
byte brightness;

switch (state) {
case BlinkState::DELAY:
if (timer.available()) {
state = BlinkState::FADE_IN;
timer.begin(FADE_IN_TIME);
}
break;

case BlinkState::FADE_IN:
if (timer.available()) {
state = BlinkState::FADE_OUT;
timer.begin(FADE_OUT_TIME);
}
brightness = map(timer.time(), FADE_IN_TIME, 0, 0, 255);
break;

case BlinkState::FADE_OUT:
if (timer.available()) {
state = BlinkState::DELAY;
timer.begin(CYCLE_DELAY_TIME);
}
brightness = map(timer.time(), FADE_OUT_TIME, 0, 255, 0);
break;
}

analogWrite(PWM_LED, brightness);
}

void setup() {
blinkSetup();
}

void loop() {
blinkUpdate();
}

47 changes: 0 additions & 47 deletions examples/timers_test/timers_test.ino

This file was deleted.

Loading