-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#pragma once | ||
#include <Arduino.h> | ||
/* | ||
PWMrelay - библиотека для генерации низкочастотного ШИМ сигнала для реле (для ПИД регуляторов и проч.) | ||
- Настройка периода ШИМ | ||
- Настройка сигнала 0-255 (8 бит) | ||
- Поддержка реле низкого и высокого уровня | ||
- Неблокирующий вызов, не использует таймеры (кроме системного), работает на millis() | ||
*/ | ||
|
||
class PWMrelay { | ||
public: | ||
PWMrelay(byte pin, bool dir, int period); // пин, уровень реле HIGH/LOW, период | ||
void tick(); // тик, вызывать как можно чаще, сам управляет реле | ||
void setPWM(byte duty); // установить величину ШИМ, 0-255. При значении 0 и 255 тик неактивен! | ||
byte getPWM(); // возвращает величину ШИМ | ||
void setPeriod(int period); // установить период ШИМ в миллисек. (по умолч. 1000мс == 1с) | ||
int getPeriod(); // получить период | ||
void setLevel(bool level); // установить установить уровень реле (HIGH/LOW) | ||
|
||
private: | ||
bool _flag = false; | ||
bool _dir = false; | ||
byte _pin = 0; | ||
byte _duty = 0; | ||
int _period = 1000; | ||
int _activePeriod = 0; | ||
uint32_t _tmr = 0; | ||
}; | ||
|
||
PWMrelay::PWMrelay(byte pin, bool dir = false, int period = 1000) { | ||
_pin = pin; | ||
_dir = !dir; | ||
pinMode(_pin, OUTPUT); | ||
digitalWrite(_pin, _dir); // сразу выкл | ||
PWMrelay::setPeriod(period); | ||
} | ||
|
||
void PWMrelay::tick() { | ||
if (_duty != 0 && _duty != 255) { | ||
if (millis() - _tmr >= (_flag ? _activePeriod : (_period - _activePeriod))) { | ||
_tmr = millis(); | ||
_flag = !_flag; | ||
digitalWrite(_pin, _flag ^ _dir); | ||
} | ||
} | ||
} | ||
|
||
void PWMrelay::setPWM(byte duty) { | ||
_duty = duty; | ||
_activePeriod = (long)_duty * _period / 255; | ||
if (_duty == 0) digitalWrite(_pin, _dir); // выкл | ||
if (_duty == 255) digitalWrite(_pin, !_dir); // вкл | ||
} | ||
|
||
byte PWMrelay::getPWM() { | ||
return _duty; | ||
} | ||
|
||
void PWMrelay::setPeriod(int period) { | ||
_period = period; | ||
PWMrelay::setPWM(_duty); // на случай "горячей" смены периода | ||
} | ||
|
||
int PWMrelay::getPeriod() { | ||
return _period; | ||
} | ||
|
||
void PWMrelay::setLevel(bool level) { | ||
_dir = level; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include <PWMrelay.h> | ||
PWMrelay relay(13); // реле на 13 пине | ||
|
||
// или так | ||
// PWMrelay relay(13, HIGH); // реле высокого уровня на 13 пине | ||
// PWMrelay relay(13, HIGH, 2000); // реле высокого уровня на 13 пине, период 2 секунды | ||
|
||
void setup() { | ||
Serial.begin(9600); | ||
|
||
relay.setLevel(HIGH); // можно поменять уровень реле (HIGH/LOW) | ||
|
||
relay.setPeriod(1000); // можно поменять период, миллисекунды | ||
|
||
relay.setPWM(20); // задаём сигнал ШИМ 0-255 | ||
} | ||
|
||
void loop() { | ||
// вызываем в лупе, данная функция сама управляет реле | ||
relay.tick(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
####################################### | ||
# Syntax Coloring Map For PWMrelay | ||
####################################### | ||
|
||
####################################### | ||
# Datatypes (KEYWORD1) | ||
####################################### | ||
|
||
PWMrelay KEYWORD1 | ||
|
||
####################################### | ||
# Methods and Functions (KEYWORD2) | ||
####################################### | ||
tick KEYWORD2 | ||
setPWM KEYWORD2 | ||
getPWM KEYWORD2 | ||
setPeriod KEYWORD2 | ||
getPeriod KEYWORD2 | ||
setLevel KEYWORD2 | ||
|
||
####################################### | ||
# Constants (LITERAL1) | ||
####################################### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
name=PWMrelay | ||
version=1.0 | ||
author=AlexGyver <beragumbo@ya.ru> | ||
maintainer=AlexGyver <beragumbo@ya.ru> | ||
sentence=Low-frequency software PWM for relay | ||
paragraph=Allows to control power of AC heaters, etc. | ||
category=Timing | ||
url=https://github.com/AlexGyver/GyverLibs | ||
architectures=* |