Line endings

This commit is contained in:
Chris Giacofei 2022-01-14 13:50:41 -05:00
parent f9d9005c3b
commit ff48bcb52d
2 changed files with 42 additions and 39 deletions

1
.gitattributes vendored Normal file
View File

@ -0,0 +1 @@
* text=auto

View File

@ -1,39 +1,41 @@
#ifndef SLOWPWM_h #ifndef SLOWPWM_h
#define SLOWPWM_h #define SLOWPWM_h
#include <Arduino.h> #include <Arduino.h>
class slowPWM { class slowPWM {
private: private:
byte outputPin; byte outputPin;
byte dutyCycle; unsigned long period;
unsigned long period; unsigned long lastSwitchTime;
unsigned long lastSwitchTime; byte outputState;
byte outputState;
public:
public: void begin(byte pin, unsigned long per) {
void begin(byte pin, unsigned long per) { outputPin = pin;
outputPin = pin; period = per;
period = per; lastSwitchTime = 0;
lastSwitchTime = 0; outputState = LOW;
outputState = LOW; pinMode(pin, OUTPUT);
pinMode(pin, OUTPUT); Serial.println("Setup PWM");
} }
void compute(byte duty) { void compute(byte duty) {
unsigned long onTime = (dutyCycle * period) / 100; unsigned long onTime = (duty * period) / 100;
unsigned long offTime = period - onTime; unsigned long offTime = period - onTime;
unsigned long currentTime = millis(); unsigned long currentTime = millis();
if (outputState == HIGH && (currentTime - lastSwitchTime >= onTime)) { if (duty == 0) {
lastSwitchTime = currentTime; outputState = LOW;
outputState = LOW; } else if (outputState == HIGH && (currentTime - lastSwitchTime >= onTime)) {
} lastSwitchTime = currentTime;
if (outputState == LOW && (currentTime - lastSwitchTime >= offTime)) { outputState = LOW;
lastSwitchTime = currentTime;
outputState = HIGH; } else if (outputState == LOW && (currentTime - lastSwitchTime >= offTime)) {
} lastSwitchTime = currentTime;
digitalWrite(outputPin, outputState); outputState = HIGH;
} }
}; digitalWrite(outputPin, outputState);
#endif }
};
#endif