skizziks_53

Two buttons with servos version 1.0

Jan 24th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.51 KB | None | 0 0
  1. /*
  2.    Two buttons with servos version 1.0
  3.  
  4.    This sketch assumes that you are using normally-open buttons connected directly to the arduino input pins,
  5.    and are not using any pulldown resistors. The button pinmodes are declared as INPUT_PULLUP.
  6.  
  7. */
  8.  
  9. #include <Servo.h>
  10. Servo servo1;
  11. Servo servo2;
  12.  
  13. int pushbutton_1_pin = 2; // this is the pin used for button #1
  14. int button_1_state = 0; // this is where to store the pin state of button #1
  15. int servo_1_pin = 3; // This is the PWM pin that servo #1 is connected to.
  16. int servo_1_angles[] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180}; // servo1 array
  17. int servo_1_position = 0; // This is the array index for servo #1.
  18. int servo_1_move_direction = 1; // 1 for ascending, -1 for descending
  19.  
  20. int pushbutton_2_pin = 4; // this is the pin used for button #2
  21. int button_2_state = 0; // this is where to store the pin state of button #2
  22. int servo_2_pin = 5; // This is the PWM pin that servo #2 is connected to.
  23. int servo_2_angles[] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180}; // servo2 array
  24. int servo_2_position; // This is the array index for servo #2.
  25. int servo_2_move_direction = 1; // 1 for ascending, -1 for descending
  26.  
  27. // Below are the variables used for button debouncing.
  28. // This sketch uses a single debounce time for both buttons.
  29. int button_debounce_time = 500; // This is the milliseconds time for button debouncing (the time delay after a button has been pressed).
  30. unsigned long button_previous_time = 0;
  31. unsigned long button_current_time = 0;
  32. bool buttons_enabled = false; // This is a "switch" for enabling or disabling checks for button presses.
  33.  
  34. // When using C/C++ language, always declare your own functions before the main program functions.
  35. // Some compilers will not give error messages but will compile incorrect runtime code if you don't do this!
  36. void perform_button_1_function();
  37. void perform_button_2_function();
  38.  
  39.  
  40. void setup() {
  41.   Serial.begin(9600);
  42.   servo1.attach(servo_1_pin);
  43.   servo1.write(0);
  44.   servo2.attach(servo_2_pin);
  45.   servo2.write(0);
  46.   pinMode(pushbutton_1_pin, INPUT_PULLUP);
  47.   pinMode(pushbutton_2_pin, INPUT_PULLUP);
  48. }
  49.  
  50. void loop() {
  51.  
  52.   if (buttons_enabled == true) {
  53.  
  54.     // The section below is checking for a #1 button press.
  55.     button_1_state = digitalRead(pushbutton_1_pin);
  56.     if (button_1_state == LOW) {
  57.       perform_button_1_function();
  58.     }
  59.  
  60.  
  61.     // The section below is the same as above, but for button #2 & servo #2.
  62.     button_2_state = digitalRead(pushbutton_2_pin);
  63.     if (button_2_state == LOW) {
  64.       perform_button_2_function();
  65.     }
  66.  
  67.   }
  68.   else { // if buttons_enabled = false
  69.     // If a button was pushed then this checks the time to see if the buttons should be re-enabled.
  70.     button_current_time = millis();
  71.     if (button_current_time >= button_previous_time) {
  72.       if (button_current_time >= (button_previous_time + button_debounce_time)) {
  73.         buttons_enabled = true;
  74.       }
  75.     }
  76.     else {
  77.       button_previous_time = millis(); // rollover condition for millis() time.
  78.     }
  79.   }
  80.  
  81. } // end of main loop
  82.  
  83.  
  84.  
  85. void perform_button_1_function() {
  86.   servo_1_position = servo_1_position + servo_1_move_direction; // change to the new value if servo_1_position.
  87.   // check that servo_1_position is still within the array index bounds (0 to 18).
  88.   // change servo_1_position and servo_1_move_direction if servo_1_position has gone out of bounds.
  89.   if (servo_1_position == 19) {
  90.     servo_1_move_direction = -1;
  91.     servo_1_position = 17;
  92.   }
  93.   if (servo_1_position == -1) {
  94.     servo_1_move_direction = 1;
  95.     servo_1_position = 0;
  96.   }
  97.   servo1.write(servo_1_angles[servo_1_position]);
  98.   Serial.print("servo 1:");
  99.   Serial.println(servo_1_angles[servo_1_position]);
  100.  
  101.   buttons_enabled = false; // disable any further button inputs (to debounce)
  102.   button_previous_time = millis(); // start the debounce timer here
  103. }
  104.  
  105.  
  106.  
  107. void perform_button_2_function() {
  108.   servo_2_position = servo_2_position + servo_2_move_direction;
  109.   if (servo_2_position == 19) {
  110.     servo_2_move_direction = -1;
  111.     servo_2_position = 17;
  112.   }
  113.   if (servo_2_position == -1) {
  114.     servo_2_move_direction = 1;
  115.     servo_2_position = 0;
  116.   }
  117.   servo1.write(servo_2_angles[servo_2_position]);
  118.   Serial.print("servo 2:");
  119.   Serial.println(servo_2_angles[servo_2_position]);
  120.  
  121.   buttons_enabled = false; // disable any further button inputs (to debounce)
  122.   button_previous_time = millis(); // start the debounce timer here
  123. }
Advertisement
Add Comment
Please, Sign In to add comment