/
BrushedESC.ino
48 lines (41 loc) · 955 Bytes
/
BrushedESC.ino
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
/*
* Brushed ESC
* @author TheHLab
* @created Otc 10, 2019
* This code for ATtiny85 with Arduino IDE, 8Mhz
* Demo: https://youtu.be/hYDtW29BTBY
*/
#define MIDDLE_POINT 1500
#define OUT1 0
#define OUT2 1
#define IN 0
volatile int pwm_value = 0;
volatile int prev_time = 0;
void setup() {
// when pin IN goes high, call the rising function
attachInterrupt(IN, rising, RISING);
}
void loop() { }
void rising() {
attachInterrupt(IN, falling, FALLING);
prev_time = micros();
}
void falling() {
attachInterrupt(IN, rising, RISING);
pwm_value = micros()-prev_time;
PWM_out(pwm_value);
}
void PWM_out(int speeds){
// speed
int tempSpeeds = map(abs(speeds-MIDDLE_POINT), 0, 500, 0, 255);
// direction
if( speeds > MIDDLE_POINT ){
analogWrite(OUT1, tempSpeeds );
pinMode(OUT2, OUTPUT);
digitalWrite(OUT2, LOW);
} else {
pinMode(OUT1, OUTPUT);
digitalWrite(OUT1, LOW);
analogWrite(OUT2, tempSpeeds );
}
}