51 lines
1.0 KiB
C++
51 lines
1.0 KiB
C++
#include <Servo.h>
|
|
|
|
enum BarrierState {OPEN, CLOSED};
|
|
|
|
class Barrier {
|
|
|
|
private:
|
|
Servo servo;
|
|
int pin;
|
|
BarrierState state = CLOSED;
|
|
|
|
public:
|
|
|
|
int open_time_seconds;
|
|
|
|
Barrier(int pin, int open_time_seconds = 3){
|
|
this->pin = pin;
|
|
this->close();
|
|
this->open_time_seconds = open_time_seconds;
|
|
}
|
|
|
|
void configure_pins(){
|
|
this->servo.attach(pin);
|
|
this->servo.write(90);
|
|
this->state = CLOSED;
|
|
}
|
|
|
|
void open(){
|
|
if (this->state != OPEN){
|
|
this->state = OPEN;
|
|
this->servo.write(0);
|
|
}
|
|
}
|
|
|
|
void close(){
|
|
if(this->state != CLOSED){
|
|
this->state = CLOSED;
|
|
this->servo.write(90);
|
|
}
|
|
}
|
|
|
|
bool is_open() const {
|
|
return this->state == OPEN;
|
|
}
|
|
|
|
bool is_closed() const {
|
|
return this->state == CLOSED;
|
|
}
|
|
|
|
};
|