Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Two buttons with servos version 1.0
- This sketch assumes that you are using normally-open buttons connected directly to the arduino input pins,
- and are not using any pulldown resistors. The button pinmodes are declared as INPUT_PULLUP.
- */
- #include <Servo.h>
- Servo servo1;
- Servo servo2;
- int pushbutton_1_pin = 2; // this is the pin used for button #1
- int button_1_state = 0; // this is where to store the pin state of button #1
- int servo_1_pin = 3; // This is the PWM pin that servo #1 is connected to.
- 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
- int servo_1_position = 0; // This is the array index for servo #1.
- int servo_1_move_direction = 1; // 1 for ascending, -1 for descending
- int pushbutton_2_pin = 4; // this is the pin used for button #2
- int button_2_state = 0; // this is where to store the pin state of button #2
- int servo_2_pin = 5; // This is the PWM pin that servo #2 is connected to.
- 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
- int servo_2_position; // This is the array index for servo #2.
- int servo_2_move_direction = 1; // 1 for ascending, -1 for descending
- // Below are the variables used for button debouncing.
- // This sketch uses a single debounce time for both buttons.
- int button_debounce_time = 500; // This is the milliseconds time for button debouncing (the time delay after a button has been pressed).
- unsigned long button_previous_time = 0;
- unsigned long button_current_time = 0;
- bool buttons_enabled = false; // This is a "switch" for enabling or disabling checks for button presses.
- // When using C/C++ language, always declare your own functions before the main program functions.
- // Some compilers will not give error messages but will compile incorrect runtime code if you don't do this!
- void perform_button_1_function();
- void perform_button_2_function();
- void setup() {
- Serial.begin(9600);
- servo1.attach(servo_1_pin);
- servo1.write(0);
- servo2.attach(servo_2_pin);
- servo2.write(0);
- pinMode(pushbutton_1_pin, INPUT_PULLUP);
- pinMode(pushbutton_2_pin, INPUT_PULLUP);
- }
- void loop() {
- if (buttons_enabled == true) {
- // The section below is checking for a #1 button press.
- button_1_state = digitalRead(pushbutton_1_pin);
- if (button_1_state == LOW) {
- perform_button_1_function();
- }
- // The section below is the same as above, but for button #2 & servo #2.
- button_2_state = digitalRead(pushbutton_2_pin);
- if (button_2_state == LOW) {
- perform_button_2_function();
- }
- }
- else { // if buttons_enabled = false
- // If a button was pushed then this checks the time to see if the buttons should be re-enabled.
- button_current_time = millis();
- if (button_current_time >= button_previous_time) {
- if (button_current_time >= (button_previous_time + button_debounce_time)) {
- buttons_enabled = true;
- }
- }
- else {
- button_previous_time = millis(); // rollover condition for millis() time.
- }
- }
- } // end of main loop
- void perform_button_1_function() {
- servo_1_position = servo_1_position + servo_1_move_direction; // change to the new value if servo_1_position.
- // check that servo_1_position is still within the array index bounds (0 to 18).
- // change servo_1_position and servo_1_move_direction if servo_1_position has gone out of bounds.
- if (servo_1_position == 19) {
- servo_1_move_direction = -1;
- servo_1_position = 17;
- }
- if (servo_1_position == -1) {
- servo_1_move_direction = 1;
- servo_1_position = 0;
- }
- servo1.write(servo_1_angles[servo_1_position]);
- Serial.print("servo 1:");
- Serial.println(servo_1_angles[servo_1_position]);
- buttons_enabled = false; // disable any further button inputs (to debounce)
- button_previous_time = millis(); // start the debounce timer here
- }
- void perform_button_2_function() {
- servo_2_position = servo_2_position + servo_2_move_direction;
- if (servo_2_position == 19) {
- servo_2_move_direction = -1;
- servo_2_position = 17;
- }
- if (servo_2_position == -1) {
- servo_2_move_direction = 1;
- servo_2_position = 0;
- }
- servo1.write(servo_2_angles[servo_2_position]);
- Serial.print("servo 2:");
- Serial.println(servo_2_angles[servo_2_position]);
- buttons_enabled = false; // disable any further button inputs (to debounce)
- button_previous_time = millis(); // start the debounce timer here
- }
Advertisement
Add Comment
Please, Sign In to add comment