Skip to content

Instantly share code, notes, and snippets.

@ondrejh
Last active May 2, 2020 15:10
Show Gist options
  • Save ondrejh/5801313ae0f0df3830fb3b79784535fc to your computer and use it in GitHub Desktop.
Save ondrejh/5801313ae0f0df3830fb3b79784535fc to your computer and use it in GitHub Desktop.
Servo limiter - limits servo signal to 25% (HW - Arduino Pro Mini)
/**
* Program reads servo signal on "input", changes its aplitude by 25% and sends it to "output".
* It is used for limiting RC car power - for kids.
*/
// input and output pin
const int input = 2;
const int output = 3;
const int led = LED_BUILTIN;
// servo signal timing
#define CENTER 750 // should be 1500, but my pro mini runs half clock
#define DIV 4 // 1/4 ~ 25%
void setup() {
// put your setup code here, to run once:
pinMode(led, OUTPUT);
digitalWrite(led, 0);
pinMode(input, INPUT);
pinMode(output, OUTPUT);
Serial.begin(115200); // its 57600 actually (half clock speed)
}
void loop() {
while(digitalRead(input) == LOW);
int32_t i = micros();
while(digitalRead(input) == HIGH);
i = micros()-i;
int32_t o = (i - CENTER) / DIV + CENTER;
uint32_t t = micros();
digitalWrite(output, HIGH);
while ((micros() - t) < o);
digitalWrite(output, LOW);
digitalWrite(led, !digitalRead(led));
Serial.print(i);
Serial.print(" ");
Serial.println(o);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment