Skip to content

Commit e307d05

Browse files
add VibrationMotor
1 parent a8465ff commit e307d05

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

VibrationMotor/VibrationMotor.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* Copyright (c) 2022, Collab
2+
* All rights reserved
3+
*/
4+
5+
#include "VibrationMotor.h"
6+
7+
VibrationMotor::VibrationMotor(
8+
int motor_pin,
9+
unsigned int period
10+
) {
11+
_motorPin = motor_pin;
12+
_period = period;
13+
}
14+
15+
void VibrationMotor::begin() {
16+
pinMode(_motorPin, OUTPUT);
17+
}
18+
19+
void VibrationMotor::loop() {
20+
if (_enabled) {
21+
if ((millis() - _enablePoint) >= _period) {
22+
disable();
23+
}
24+
}
25+
}
26+
27+
void VibrationMotor::enable() {
28+
_enabled = true;
29+
_enablePoint = millis();
30+
31+
digitalWrite(_motorPin, HIGH);
32+
}
33+
34+
void VibrationMotor::disable() {
35+
_enabled = false;
36+
37+
digitalWrite(_motorPin, LOW);
38+
}

VibrationMotor/VibrationMotor.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* Copyright (c) 2022, Collab
2+
* All rights reserved
3+
*/
4+
#ifndef VibrationMotor_h
5+
#define VibrationMotor_h
6+
7+
#include <Arduino.h>
8+
9+
class VibrationMotor
10+
{
11+
public:
12+
VibrationMotor(
13+
int motor_pin,
14+
unsigned int period = 250
15+
);
16+
void begin();
17+
void loop();
18+
void enable();
19+
void disable();
20+
21+
private:
22+
int _motorPin;
23+
unsigned int _period;
24+
unsigned int _enablePoint;
25+
26+
bool _enabled = false;
27+
};
28+
29+
#endif

0 commit comments

Comments
 (0)