Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h> //Servo library to control your ESC.
- Servo esc; //Declare the servo name as 'esc'.
- int ch1; //PPM input from the Rx (RC receiver) goes here.
- int throttle = 0; //Serial input is mapped onto this value, should be within the range 0 to 179.
- int count = 0; //Variable to delay activation until ESC has bound.
- int lowerSwitchState = 0; //Variable to track the status of the lower limit switch.
- int upperSwitchState = 0; //Variable to track the status of the upper limit switch.
- void setup() {
- pinMode(5, INPUT); //Input from Rx
- pinMode(2, INPUT); //Lower switch (grey wire)
- pinMode(3, INPUT); //Upper switch (green wire)
- Serial.begin(9600); //Start the serial output (for debugging purposes).
- esc.attach(9); //Bind the ESC to the arduino, just as it would to a receiver.
- }
- void loop() {
- lowerSwitchState = digitalRead(2); //Read pin 2 (the lower limit switch).
- upperSwitchState = digitalRead(3); //Read pin 3 (the upper limit switch).
- ch1 = pulseIn(5, HIGH, 25000); //Read the PPM input from the Rx data line.
- if(ch1 > 1056){ /*Reject any PPM values that are outside the accepted range.
- I found the motor tends to jitter horribly if I don't have this line, as for some reason,
- 0 is sent through every other time it reads.*/
- throttle = map(ch1, 1057, 1880, 80, 100); /*Map the values between 1057 and 1880
- (observed upper and lower limits of the throttle signal from the Rx) to the
- upper and lower limits of the ESC signal. These can be in the range 0-179, where
- 90 is the middle position (0 movement if your ESC is reversible).
- Here it's only mapped from 80-100 in order to slow the motor down.
- You can adjust this to suit your motor or application.*/
- //NOTE: If your ESC only supports one-way movement, you'll need to
- //change any '90's in this program to '0' (for no movement) and adjust other values accordingly.
- }
- if(count < 500){
- esc.write(90); //Essentially wait for 500 loops to ensure the ESC has bound.
- count ++;
- }else if(lowerSwitchState == LOW && throttle < 90){
- esc.write(90); //If the signal is trying to push the actuator
- //past its lower limit, set the output to 90 for no movement.
- }else if(upperSwitchState == LOW && throttle > 90){
- esc.write(90); //Same as above, but with upper limit.
- }else{
- esc.write(throttle); //If all is well, relay the input Rx signal
- //to the ESC.
- }
- //NOTE: If your motor still tries to push past the limits, remember
- //to check that your motor (and actuator) direction is congruent with the
- //numbers here (0 for full reverse, 179 for full forwards).
- //If it isn't, you can switch two of your three motor leads around, or
- //swap the '<' and '>' in the above two 'if' statements.
- }
Add Comment
Please, Sign In to add comment