1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| #define EN 8 #define X_DIR 5 #define Y_DIR 6 #define Z_DIR 7 #define X_STP 2 #define Y_STP 3 #define Z_STP 4 char num; void step(boolean dir, byte dirPin, byte stepperPin, int steps) { digitalWrite(dirPin, dir); delay(50); for (int i = 0; i < steps; i++) { digitalWrite(stepperPin, HIGH); delayMicroseconds(800); digitalWrite(stepperPin, LOW); delayMicroseconds(800); } } void setup(){ Serial.begin(9600); pinMode(X_DIR, OUTPUT); pinMode(X_STP, OUTPUT); pinMode(Y_DIR, OUTPUT); pinMode(Y_STP, OUTPUT); pinMode(Z_DIR, OUTPUT); pinMode(Z_STP, OUTPUT); pinMode(EN, OUTPUT); digitalWrite(EN, LOW); } void loop(){ digitalWrite(EN, HIGH); while(Serial.available()>0) { num=Serial.read(); switch(num) { case '1': Serial.println(num); digitalWrite(EN, LOW); step(false, X_DIR, X_STP, 1600); delay(1000); step(true, X_DIR, X_STP, 1600); break; case '2': digitalWrite(EN, LOW); Serial.println(num); step(false, Y_DIR, Y_STP, 1600); delay(1000); step(true, Y_DIR, Y_STP, 1600); break; case '3': Serial.println(num); digitalWrite(EN, LOW); step(false, Z_DIR, Z_STP, 1600); delay(1000); step(true, Z_DIR, Z_STP, 1600); break; } Serial.println(num); delay(1000); } }
|