skizziks_53

Reddit motor + servo w/buttons v 1.0

Apr 4th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.82 KB | None | 0 0
  1. /*
  2.    April 4, 2019
  3.    Reddit motor + servo driver with buttons for each
  4.    Board = Uno
  5.  
  6.    This sketch has two buttons.
  7.    The buttons are set to function so that they will ONLY change when going from the unpressed-to-pressed state.
  8.  
  9.    One button turns a DC motor on and off, and the other button turns a servo on and off.
  10.    When the servo is on, it moves back and forth between two settings.
  11.  
  12. */
  13.  
  14. bool use_servo_change_messages = true; // This sketch prints some serial messages for testing purposes.
  15. // The servo will print a message when it changes value and when it reverses.
  16. // If the serial messages causes the servo to run jittery then you can turn those serial messages off by setting this value to false.
  17. // The other serial messages don't turn off but they should not cause any issues.
  18.  
  19. int dc_motor_button_pin = 7; // This is where the button for the DC motor is connected.
  20. int servo_button_pin = 8; // This is where the button for the servo is connected.
  21. // Both above pins are using INPUT_PULLUP, so they should connect to a ground pin when pressed.
  22.  
  23. int dc_motor_pin = 3; // The digital output to the DC motor controller is connected here.
  24. int servo_pin = 5; // The servo control pin is connected here.
  25.  
  26. int servo_interval_time = 350; // This is the time interval that the servo will change at, in milliseconds.
  27. int servo_change_value = 10; // This is the amount that the servo position will change, at each servo_interval_time interval.
  28. int servo_current_value = 30; // This is the starting point for the servo. It should be a number between the minimum and maximum servo values below.
  29. int servo_minimum_value = 30; // This is the low end of where you want the servo to stop at. Zero is the lowest value you can put here.
  30. 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.
  31. int servo_direction = 1; // This variable will flip back and forth between 1 and -1.
  32. // Setting it to 1 will make the servo begin by turning upwards (in position value) and setting it to -1 will make the servo begin by turning downwards.
  33.  
  34. int button_debounce_time = 300; // This is to prevent the buttons from bouncing.
  35.  
  36. // The variables above, you can change the values of.
  37. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  38. // The variables below, do not change the values of.
  39.  
  40. int dc_motor_button_previous_state = 0;
  41. int dc_motor_button_current_state = 0;
  42. int servo_button_previous_state = 0;
  43. int servo_button_current_state = 0;
  44. bool buttons_enabled = true;
  45. unsigned long button_press_begin_time = 0;
  46. unsigned long button_press_current_time = 0;
  47. int dc_motor_enabled = 0;
  48. int servo_enabled = 0;
  49. bool servo_interval_enabled = true;
  50. unsigned long servo_interval_begin_time = 0;
  51. unsigned long servo_interval_current_time = 0;
  52.  
  53.  
  54.  
  55. void setup() {
  56.   Serial.begin(9600);
  57.   pinMode(dc_motor_button_pin, INPUT_PULLUP);
  58.   pinMode(servo_button_pin, INPUT_PULLUP);
  59.  
  60.   analogWrite(servo_pin, servo_current_value); // This causes the servo to immediately go to its initial position.
  61.  
  62.   pinMode(dc_motor_pin, OUTPUT);
  63.   digitalWrite(dc_motor_pin, LOW);
  64.   Serial.println("Exiting setup()");
  65. }
  66.  
  67.  
  68.  
  69. void loop() {
  70.   if (buttons_enabled == true) {
  71.  
  72.     dc_motor_button_current_state = 0;
  73.     if (digitalRead(dc_motor_button_pin) == LOW) {
  74.       dc_motor_button_current_state = 1;
  75.     }
  76.  
  77.     if (dc_motor_button_previous_state != dc_motor_button_current_state) {
  78.       button_pressed();
  79.     }
  80.  
  81.     if (dc_motor_button_previous_state == 0) {
  82.       if (dc_motor_button_current_state == 1) {
  83.         if (dc_motor_enabled == 1) {
  84.           dc_motor_enabled = 0;
  85.           Serial.println("DC motor = OFF");
  86.         }
  87.         else {
  88.           dc_motor_enabled = 1;
  89.           Serial.println("DC motor = ON");
  90.         }
  91.         digitalWrite(dc_motor_pin, dc_motor_enabled);
  92.         //button_pressed();
  93.       }
  94.     }
  95.     dc_motor_button_previous_state = dc_motor_button_current_state;
  96.  
  97.     servo_button_current_state = 0;
  98.     if (digitalRead(servo_button_pin) == LOW) {
  99.       servo_button_current_state = 1;
  100.     }
  101.     if (servo_button_previous_state != servo_button_current_state) {
  102.       button_pressed();
  103.     }
  104.     if (servo_button_previous_state == 0) {
  105.       if (servo_button_current_state == 1) {
  106.         if (servo_enabled == 1) {
  107.           servo_enabled = 0;
  108.           Serial.println("servo = OFF");
  109.         }
  110.         else {
  111.           servo_enabled = 1;
  112.           Serial.println("servo = ON");
  113.         }
  114.         //button_pressed();
  115.       }
  116.     }
  117.     servo_button_previous_state = servo_button_current_state;
  118.   }
  119.   else {
  120.     button_press_current_time = millis();
  121.     if (button_press_current_time > button_press_begin_time) {
  122.       if (button_press_current_time >= (button_press_begin_time + button_debounce_time)) {
  123.         buttons_enabled = true;
  124.       }
  125.     }
  126.     else {
  127.       button_press_begin_time = millis();
  128.     }
  129.   }
  130.  
  131.   if (servo_enabled == 1) {
  132.     if (servo_interval_enabled == true) {
  133.       change_servo_value(servo_change_value, servo_direction);
  134.       check_current_servo_value();
  135.       analogWrite(servo_pin, servo_current_value);
  136.       if (use_servo_change_messages == true) {
  137.         Serial.print("servo value = ");
  138.         Serial.println(servo_current_value);
  139.       }
  140.       servo_interval_begin_time = millis();
  141.       servo_interval_enabled = false;
  142.     }
  143.     else {
  144.       servo_interval_current_time = millis();
  145.       if (servo_interval_current_time >= servo_interval_begin_time) {
  146.         if (servo_interval_current_time >= (servo_interval_begin_time + servo_interval_time)) {
  147.           servo_interval_enabled = true;
  148.         }
  149.       }
  150.       else {
  151.         servo_interval_begin_time = millis();
  152.       }
  153.     }
  154.   }
  155. } // end of main loop()
  156.  
  157.  
  158. void button_pressed() {
  159.   buttons_enabled = false;
  160.   button_press_begin_time = millis();
  161. }
  162.  
  163.  
  164. void change_servo_value(int changeValue, int servoDirection) {
  165.   // This changes the servo value up or down, depending on if servoDirection is 1 or -1.
  166.   servo_current_value = servo_current_value + (changeValue * servoDirection);
  167. }
  168.  
  169.  
  170. void check_current_servo_value() {
  171.   // This prevents the servo from turning past the minimum and maximum values,
  172.   // and changes the direction when it hits either of the end values.
  173.   if (servo_current_value <= servo_minimum_value) {
  174.     servo_current_value = servo_minimum_value;
  175.     flip_servo_direction();
  176.   }
  177.   if (servo_current_value >= servo_maximum_value) {
  178.     servo_current_value = servo_maximum_value;
  179.     flip_servo_direction();
  180.   }
  181. }
  182.  
  183. void flip_servo_direction() {
  184.   // Because the direction control value is 1 or -1, you can change it to the "other" value just by multiplying it by -1.
  185.   servo_direction = servo_direction * -1;
  186.   if (use_servo_change_messages == true) {
  187.     Serial.println("servo = reversing");
  188.   }
  189. }
Add Comment
Please, Sign In to add comment