#include #include // Define pin connections & motor's steps per revolution const int dirPin = 2; const int stepPin = 3; const int swPin = 7; const int servoPin = 9; const int ledRed = 12; // sw_on = ture; const int ledWhite = 13; // sw_on = false; with Uno #13 LED Servo myservo; // change this to the number of steps on your motor #define STEPS 200 // const int stepsPerRevolution = 800; Stepper stepper(STEPS, dirPin, stepPin); int sw = 1; // switch value (default:1 = off) int sv_pos = 0; // currrent servo position int step_pos = 0; // current steps position int x = 0; // x steps per loop boolean sw_on = false; // current switch status boolean dir_up = true; // current direction const int steps_begin = 0; const int steps_end = 3200; void lightLED(){ if (sw_on){ digitalWrite(ledWhite, LOW); digitalWrite(ledRed, HIGH); } else { digitalWrite(ledWhite, HIGH); digitalWrite(ledRed, LOW); } } void setup(){ pinMode(swPin, INPUT_PULLUP); pinMode(dirPin, OUTPUT); myservo.attach(servoPin); // set the speed of the motor to 500 RPMs stepper.setSpeed(500); x = STEPS/100; Serial.begin(115200); } void loop(){ // Checkn switch and toggle sw = digitalRead(swPin); // Toggle LED only when sw_on status is changed if ((sw == 0 ) && (sw_on == false)){ sw_on = true; lightLED(); Serial.println("Switch ON"); delay(500); } else if ((sw == 0 ) && (sw_on )){ sw_on = false; lightLED(); Serial.println("Switch OFF"); delay(1000); } // Switch ON if (sw_on) { // check steps, rotate servo and toggle direction // only on steps_bigin or steps_end position if (step_pos <= steps_begin) { Serial.print("step_pos: "); Serial.println(step_pos); step_pos = steps_begin; // Set motor direction clockwise // digitalWrite(dirPin, HIGH); dir_up = true; Serial.print("dir_up"); delay(500); } else if (steps_end <= step_pos) { Serial.print("step_pos: "); Serial.println(step_pos); step_pos = steps_end; // Rotate servo motor up (Raise racket) for (sv_pos = 90; sv_pos >= 0; sv_pos -= 1) { myservo.write(sv_pos); delay(10); } delay(2000); // Rotate servo motor down for (sv_pos = 0; sv_pos <= 90; sv_pos += 1) { myservo.write(sv_pos); delay(10); } // Set motor direction counterclockwise // digitalWrite(dirPin, LOW); dir_up = false; Serial.println("dir_up: false"); delay(500); } // step by STEPS(steps pre rev.)/100 if (dir_up == true) { stepper.step(x); step_pos = step_pos + x; } else { stepper.step(-x); step_pos = step_pos -x; } // Switch OFF } else { Serial.print("step_pos: "); Serial.println(step_pos); Serial.print("dir_up: "); Serial.println(dir_up); // Homing stepper if (step_pos != 0) { stepper.step(-step_pos); step_pos = 0; } delay(1000); } }