skizziks_53

Reddit servo + motor driver v1.0

Apr 3rd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. /*
  2.    April 3, 2019
  3.    Reddit motor + servo driver
  4.    Board = Uno
  5.  
  6.    This sketch sets a DC motor to spin at a single speed, and moves a servo back and forth between two settings.
  7.    The DC motor speed is assumed to be set by PWM also.
  8.  
  9. */
  10.  
  11. int dc_motor_pin = 3; // The PWM input to the DC motor controller is connected here.
  12.  
  13. int dc_motor_speed = 128; // This is the speed setting for the DC motor, that can range from zero to 255.
  14. // This assumes that you are using a DC motor controller that allows setting the motor speed with a PWM value.
  15.  
  16. int servo_pin = 5; // The servo control pin is connected here.
  17.  
  18. int servo_interval_time = 100; // This is the time interval that the servo will change at, in milliseconds.
  19. int servo_change_value = 5; // This is the amount that the servo will change, at each change interval.
  20.  
  21. int servo_current_value = 32; // This is the starting point for the servo. It must be a number between the minimum and maximum value.
  22.  
  23. int servo_minimum_value = 32; // This is the low end of where you want the servo to stop at. Zero is the lowest value you can put here.
  24. int servo_maximum_value = 200; // This is the high end of where you want the servo to stop at. 255 is the highest value you can put here.
  25.  
  26. int servo_direction = 1; // This variable will flip back and forth between 1 and -1.
  27. // Setting it to 1 will make the servo begin by turning upwards, and setting it to -1 will make the servo begin by turning downwards.
  28.  
  29.  
  30.  
  31. void setup() {
  32.   analogWrite(dc_motor_pin, dc_motor_speed); // This sets the DC motor on at a fixed speed.
  33. }
  34.  
  35.  
  36.  
  37. void loop() {
  38.   change_servo_value(servo_change_value, servo_direction);
  39.   check_current_servo_value();
  40.   analogWrite(servo_pin, servo_current_value);
  41.   delay(servo_interval_time);
  42. }
  43.  
  44.  
  45. void change_servo_value(int changeValue, int servoDirection) {
  46.   // This changes the servo value up or down, depending on if servoDirection is 1 or -1.
  47.   servo_current_value = servo_current_value + (changeValue * servoDirection);
  48. }
  49.  
  50.  
  51. void check_current_servo_value() {
  52.   // This prevents the servo from turning past the minimum and maximum values,
  53.   // and changes the direction when it hits either of the end values.
  54.   if (servo_current_value < servo_minimum_value) {
  55.     servo_current_value = servo_minimum_value;
  56.     flip_servo_direction();
  57.   }
  58.   if (servo_current_value > servo_maximum_value) {
  59.     servo_current_value = servo_maximum_value;
  60.     flip_servo_direction();
  61.   }
  62. }
  63.  
  64. void flip_servo_direction() {
  65.   // Because the direction control value is 1 or -1, you can change it to the "other" value just by multiplying it by -1.
  66.   servo_direction = servo_direction * -1;
  67. }
Add Comment
Please, Sign In to add comment