-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAMAPWM.cpp
51 lines (45 loc) · 829 Bytes
/
AMAPWM.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "AMAPWM.h"
using namespace AMACar;
/*
* Generates a PWM with a specified duty cycle on a specified pin
*/
AMAPWM::AMAPWM(byte pin, byte dutyCycle)
{
this->pin = pin;
this->dutyCycle = dutyCycle;
pinMode(pin, OUTPUT);
}
/*
* Sets a new pin for PWM
*/
void AMAPWM::setPin(byte pin)
{
if(this->pin != pin)
{
digitalWrite(this->pin, LOW);
this->pin = pin;
pinMode(pin, OUTPUT);
generate();
}
}
/*
* Sets a new duty cycle
*/
void AMAPWM::setDutyCycle(byte dutyCycle)
{
if(this->dutyCycle != dutyCycle)
{
this->dutyCycle = dutyCycle;
generate();
}
}
/*
* Generates the PWM
*/
void AMAPWM::generate()
{
analogWrite(pin, LOW);
delay(5);
byte byteValue = (byte) (2.55f * dutyCycle);
analogWrite(pin, byteValue);
}