Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- April 4, 2019
- Reddit motor + servo driver with buttons for each
- Board = Uno
- This sketch has two buttons.
- The buttons are set to function so that they will ONLY change when going from the unpressed-to-pressed state.
- One button turns a DC motor on and off, and the other button turns a servo on and off.
- When the servo is on, it moves back and forth between two settings.
- */
- bool use_servo_change_messages = true; // This sketch prints some serial messages for testing purposes.
- // The servo will print a message when it changes value and when it reverses.
- // If the serial messages causes the servo to run jittery then you can turn those serial messages off by setting this value to false.
- // The other serial messages don't turn off but they should not cause any issues.
- int dc_motor_button_pin = 7; // This is where the button for the DC motor is connected.
- int servo_button_pin = 8; // This is where the button for the servo is connected.
- // Both above pins are using INPUT_PULLUP, so they should connect to a ground pin when pressed.
- int dc_motor_pin = 3; // The digital output to the DC motor controller is connected here.
- int servo_pin = 5; // The servo control pin is connected here.
- int servo_interval_time = 350; // This is the time interval that the servo will change at, in milliseconds.
- int servo_change_value = 10; // This is the amount that the servo position will change, at each servo_interval_time interval.
- 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.
- 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.
- 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.
- int servo_direction = 1; // This variable will flip back and forth between 1 and -1.
- // 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.
- int button_debounce_time = 300; // This is to prevent the buttons from bouncing.
- // The variables above, you can change the values of.
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // The variables below, do not change the values of.
- int dc_motor_button_previous_state = 0;
- int dc_motor_button_current_state = 0;
- int servo_button_previous_state = 0;
- int servo_button_current_state = 0;
- bool buttons_enabled = true;
- unsigned long button_press_begin_time = 0;
- unsigned long button_press_current_time = 0;
- int dc_motor_enabled = 0;
- int servo_enabled = 0;
- bool servo_interval_enabled = true;
- unsigned long servo_interval_begin_time = 0;
- unsigned long servo_interval_current_time = 0;
- void setup() {
- Serial.begin(9600);
- pinMode(dc_motor_button_pin, INPUT_PULLUP);
- pinMode(servo_button_pin, INPUT_PULLUP);
- analogWrite(servo_pin, servo_current_value); // This causes the servo to immediately go to its initial position.
- pinMode(dc_motor_pin, OUTPUT);
- digitalWrite(dc_motor_pin, LOW);
- Serial.println("Exiting setup()");
- }
- void loop() {
- if (buttons_enabled == true) {
- dc_motor_button_current_state = 0;
- if (digitalRead(dc_motor_button_pin) == LOW) {
- dc_motor_button_current_state = 1;
- }
- if (dc_motor_button_previous_state != dc_motor_button_current_state) {
- button_pressed();
- }
- if (dc_motor_button_previous_state == 0) {
- if (dc_motor_button_current_state == 1) {
- if (dc_motor_enabled == 1) {
- dc_motor_enabled = 0;
- Serial.println("DC motor = OFF");
- }
- else {
- dc_motor_enabled = 1;
- Serial.println("DC motor = ON");
- }
- digitalWrite(dc_motor_pin, dc_motor_enabled);
- //button_pressed();
- }
- }
- dc_motor_button_previous_state = dc_motor_button_current_state;
- servo_button_current_state = 0;
- if (digitalRead(servo_button_pin) == LOW) {
- servo_button_current_state = 1;
- }
- if (servo_button_previous_state != servo_button_current_state) {
- button_pressed();
- }
- if (servo_button_previous_state == 0) {
- if (servo_button_current_state == 1) {
- if (servo_enabled == 1) {
- servo_enabled = 0;
- Serial.println("servo = OFF");
- }
- else {
- servo_enabled = 1;
- Serial.println("servo = ON");
- }
- //button_pressed();
- }
- }
- servo_button_previous_state = servo_button_current_state;
- }
- else {
- button_press_current_time = millis();
- if (button_press_current_time > button_press_begin_time) {
- if (button_press_current_time >= (button_press_begin_time + button_debounce_time)) {
- buttons_enabled = true;
- }
- }
- else {
- button_press_begin_time = millis();
- }
- }
- if (servo_enabled == 1) {
- if (servo_interval_enabled == true) {
- change_servo_value(servo_change_value, servo_direction);
- check_current_servo_value();
- analogWrite(servo_pin, servo_current_value);
- if (use_servo_change_messages == true) {
- Serial.print("servo value = ");
- Serial.println(servo_current_value);
- }
- servo_interval_begin_time = millis();
- servo_interval_enabled = false;
- }
- else {
- servo_interval_current_time = millis();
- if (servo_interval_current_time >= servo_interval_begin_time) {
- if (servo_interval_current_time >= (servo_interval_begin_time + servo_interval_time)) {
- servo_interval_enabled = true;
- }
- }
- else {
- servo_interval_begin_time = millis();
- }
- }
- }
- } // end of main loop()
- void button_pressed() {
- buttons_enabled = false;
- button_press_begin_time = millis();
- }
- void change_servo_value(int changeValue, int servoDirection) {
- // This changes the servo value up or down, depending on if servoDirection is 1 or -1.
- servo_current_value = servo_current_value + (changeValue * servoDirection);
- }
- void check_current_servo_value() {
- // This prevents the servo from turning past the minimum and maximum values,
- // and changes the direction when it hits either of the end values.
- if (servo_current_value <= servo_minimum_value) {
- servo_current_value = servo_minimum_value;
- flip_servo_direction();
- }
- if (servo_current_value >= servo_maximum_value) {
- servo_current_value = servo_maximum_value;
- flip_servo_direction();
- }
- }
- void flip_servo_direction() {
- // Because the direction control value is 1 or -1, you can change it to the "other" value just by multiplying it by -1.
- servo_direction = servo_direction * -1;
- if (use_servo_change_messages == true) {
- Serial.println("servo = reversing");
- }
- }
Add Comment
Please, Sign In to add comment