-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathServoIn.ino
65 lines (54 loc) · 1.36 KB
/
ServoIn.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
float microsecondsToDegrees( long microseconds );
void setOutVal( float val );
void setOutValFromInVal( float val );
void ICACHE_RAM_ATTR handleServoInChange() {
boolean state = digitalRead(SERVO_IN_PIN);
if( state )
servoInOnUS = micros();
else
servoInOffUS = micros();
}
void ignoreCurrentPulse()
{
servoInOnUS = -1l;
}
void setupServoIn()
{
pinMode(SERVO_IN_PIN, INPUT);
attachInterrupt(SERVO_IN_PIN, handleServoInChange, CHANGE);
}
boolean gotServoIn()
{
return servoInOnUS > -1l && servoInOffUS > -1L;
}
void loopServoIn()
{
long now = micros();
//Serial.print( servoInOnUS );
//Serial.print(" ");
//Serial.println( servoInOffUS );
if( servoInOnUS >0L && now - servoInOnUS > servoTimeoutUS )
{
// timeout, forget the old values
servoInOnUS = -1L;
servoInOffUS = -1L;
}
if( servoInOnUS > -1l && servoInOffUS > -1L )
{
if( servoInOffUS > servoInOnUS ) // between pulses
{
servoInUS = servoInOffUS - servoInOnUS;
servoInDegrees = microsecondsToDegrees( servoInUS );
setOutValFromInVal(servoInDegrees);
//Serial.print( servoInUS );
//Serial.print(" ");
//Serial.println( servoInDegrees );
}
// else we are the middle of a pulse, don't update the time
}
else
{
servoInDegrees = -359.0;
//Serial.print("Servo in "); Serial.println( servoInDegrees );
}
}