const int enPin = 8; const int stepXPin = 2; //X.STEP const int dirXPin = 5; // X.DIR int stepPin = stepXPin; int dirPin = dirXPin; const int stepsPerRev = 200; int pulseWidthMicros = 100; // microseconds int millisBtwnSteps = 1000; #include Servo myservo; // create servo object to control a servo // twelve servo objects can be created on most boards int pos = 0; // variable to store the servo position const int buttonPin = 11; int buttonState = 0; void setup() { Serial.begin(9600); pinMode(enPin, OUTPUT); digitalWrite(enPin, LOW); pinMode(stepPin, OUTPUT); pinMode(dirPin, OUTPUT); Serial.println(F("CNC Shield Initialized")); myservo.attach(9); pinMode(buttonPin, INPUT); } void loop() { buttonState = digitalRead(buttonPin); Serial.println(buttonState); if (buttonState == HIGH) { Serial.println("Running clockwise"); for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees. If we had made a Z-axis, it woud get lowered here // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15 ms for the servo to reach the position } digitalWrite(dirPin, HIGH); // Enables the motor to move in a particular direction. // Makes 200 pulses for making one full cycle rotation for (int i = 0; i < stepsPerRev * 5; i++) { //The blade will cut through the paper digitalWrite(stepPin, HIGH); // delayMicroseconds(pulseWidthMicros); digitalWrite(stepPin, LOW); delayMicroseconds(millisBtwnSteps); } for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees. The iamginary Z-axis would rise myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15 ms for the servo to reach the position } Serial.println(F("Running counter-clockwise")); digitalWrite(dirPin, LOW); //Changes the rotations direction // Makes 400 pulses for making two full cycle rotation for (int i = 0; i < stepsPerRev*5; i++) { //The blade will go back to starting point. digitalWrite(stepPin, HIGH); delayMicroseconds(pulseWidthMicros); digitalWrite(stepPin, LOW); delayMicroseconds(millisBtwnSteps); } } }