Skip to content

Instantly share code, notes, and snippets.

@carneeki
Created January 18, 2020 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carneeki/3a1459278b184c06be53f69aa0ab7743 to your computer and use it in GitHub Desktop.
Save carneeki/3a1459278b184c06be53f69aa0ab7743 to your computer and use it in GitHub Desktop.
David Bike Controller.ino
/* Send an RC-style PWM signal to motor controller
* given analog input. 2 inputs to permit joystick
*
* Designed for use on ATTiny85 w/ Arduino
*/
#include <Servo.h>
#define PIN_PWM 0 // motor controller pin
#define PIN_IN A1 // analog input pin
#define DEADBAND 16 // deadband for analog value when mapped from 0 to 1023
#define DELAYMS 20 // delay between loop runs
Servo motor; // RC servo object
int val; // val is position of joystick
void setup() {
pinMode(PIN_IN, INPUT);
motor.attach(PIN_PWM);
}
void loop() {
val = analogRead(PIN_IN); // read PIN_IN value
val = (abs(val - 512) <= DEADBAND ) 0 : val; // apply dead band filter
motor.write(map(val, 0, 1023, 0, 180)); // send val to motor
delay(DELAYMS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment