#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 const int stepsPerRevolution = 800; Servo myservo; int sw = 1; int pos = 0; boolean sw_on = false; 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(stepPin, OUTPUT); pinMode(dirPin, OUTPUT); myservo.attach(servoPin); Serial.begin(115200); } void loop() { sw = digitalRead(swPin); if ((sw == 0 ) && (sw_on == false)){ sw_on = true; } else if ((sw == 0 ) && (sw_on )){ sw_on = false; } Serial.println(sw_on); lightLED(); if (sw_on) { // Set motor direction clockwise digitalWrite(dirPin, HIGH); // Spin stepper motor for(int x = 0; x < stepsPerRevolution; x++) { digitalWrite(stepPin, HIGH); delayMicroseconds(1000); digitalWrite(stepPin, LOW); delayMicroseconds(1000); } // Rotate servo motor for (pos = 0; pos <= 90; pos += 1) { myservo.write(pos); delay(10); } delay(500); for (pos = 90; pos >= 0; pos -= 1) { myservo.write(pos); delay(10); } // Set motor direction counterclockwise digitalWrite(dirPin, LOW); // Spin motor quickly for(int x = 0; x < stepsPerRevolution; x++) { digitalWrite(stepPin, HIGH); delayMicroseconds(1000); digitalWrite(stepPin, LOW); delayMicroseconds(1000); } delay(500); } // wait for checking "sw_on" flag delay(1000); }