Vous êtes sur la page 1sur 5

#include<AccelStepper.

h>
#include <Servo.h>

AccelStepper stepperX(1, 7, 8); // 1 = Easy Driver interface


// UNO Pin 2 connected to STEP pin of Easy Driver
// UNO Pin 3 connected to DIR pin of Easy Driver

AccelStepper stepperY(1, 5, 6); // 1 = Easy Driver interface


// UNO Pin 5 connected to STEP pin of Easy Driver
// UNO Pin 6 connected to DIR pin of Easy Driver

AccelStepper stepperZ(1, 11, 12); // 1 = Easy Driver interface


// UNO Pin 8 connected to STEP pin of Easy Driver
// UNO Pin 9 connected to DIR pin of Easy Driver

Servo myservo;

unsigned long sbl = 0;


long TravelX; // Used to store the X value entered in the Serial
Monitor
long TravelY; // Used to store the Y value entered in the Serial
Monitor
long TravelZ; // Used to store the Z value entered in the Serial
Monitor
long Led1;//data digital dari labview, spy 1 grup
long Servoku;

int move_finished = 1; // Used to check if move is completed


int Zsignal;

const int buttonPin1 = 32;//alamat limit switch


const int buttonPin2 = 33;
const int buttonPin3 = 34;
const int buttonPin4 = 35;
const int buttonPin5 = 36;
const int buttonPin6 = 37;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
int buttonState6 = 0;
int limit1;
int limit2;
int limit3;
int limit4;
int limit5;
int limit6;

void setup() {
Serial.begin(9600); // Start the Serial monitor with speed of 9600
Bauds
stepperX.setMaxSpeed(10000.0); // Set Max Speed of X axis
stepperX.setAcceleration(5000.0); // Acceleration of X axis
stepperY.setMaxSpeed(10000.0); // Set Max Speed of Y axis
stepperY.setAcceleration(5000.0); // Acceleration of Y axis
stepperZ.setMaxSpeed(10000.0); // Set Max Speed of Z axis
slower for rotation
stepperZ.setAcceleration(5000.0); // Acceleration of Z axis
pinMode(buttonPin1, INPUT); //pengenalan limit switch
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
pinMode(buttonPin6, INPUT);
pinMode(40, OUTPUT);//motordc
pinMode(41, OUTPUT);
pinMode(42, OUTPUT);//spare
pinMode(43, OUTPUT);
myservo.attach(9);//servo
}

void serialEvent() {//labview to arduino


while (Serial.available() > 0) { // Check if values are available
in the Serial Buffer
TravelX = Serial.parseInt(); // Put First numeric value from
buffer in TravelX variable
TravelY = Serial.parseInt(); // Put First numeric value from
buffer in TravelY variable
TravelZ = Serial.parseInt(); // Put Second numeric value from
buffer in TravelZ variable
Servoku = Serial.parseInt();//baca data servo dr labview
Led1 = Serial.parseInt();
if (Serial.read() == char(13)) {
move_finished = 0; // Set variable for checking move of the
Steppers
stepperX.moveTo(TravelX); // Set new move position for X
Stepper
stepperY.moveTo(TravelY); // Set new move position for Y
Stepper
stepperZ.moveTo(TravelZ); // Set new move position for Z
Stepper
myservo.write(Servoku);
digitalWrite(40, bitRead(Led1, 0));
digitalWrite(41, bitRead(Led1, 1));
digitalWrite(42, bitRead(Led1, 2));
digitalWrite(43, bitRead(Led1, 3));
}
}
}

void loop() {//pergerakan stepper


if ((stepperX.distanceToGo() != 0) || (stepperY.distanceToGo() !=
0) || (stepperZ.distanceToGo() != 0)) {
stepperX.run(); // Move Stepper X into position
stepperY.run(); // Move Stepper Y into position
stepperZ.run(); // Move Stepper Z into position
}
if ((move_finished == 0) && (stepperX.distanceToGo() == 0) &&
(stepperY.distanceToGo() == 0) && (stepperZ.distanceToGo() == 0)) {
move_finished = 1; // Reset move variable
}
if ((stepperZ.distanceToGo() > 0) && (stepperZ.distanceToGo() !=
0)){
Zsignal=1;
} else {
Zsignal=0;
}

buttonState1 = digitalRead(buttonPin1);//limitswitch
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
buttonState5 = digitalRead(buttonPin5);

if (buttonState1 == HIGH) {
limit1=1;
} else {
limit1=0;
}
if (buttonState2 == HIGH) {
limit2=1;
} else {
limit2=0;
}
if (buttonState3 == HIGH) {
limit3=1;
} else {
limit3=0;
}
if (buttonState4 == HIGH) {
limit4=1;
} else {
limit4=0;
}
if (buttonState5 == HIGH) {
limit5=1;
} else {
limit5=0;
}
if (buttonState6 == HIGH) {
limit6=1;
} else {
limit6=0;
}
unsigned long skr = millis();
if (skr - sbl >= 100) { //Arduino ke labview
sbl = skr;
Serial.print(TravelX);
Serial.print(",");
Serial.print(TravelY);
Serial.print(",");
Serial.print(TravelZ);
Serial.print(",");
Serial.print(Zsignal);
Serial.print(",");
Serial.print(limit1);
Serial.print(",");
Serial.print(limit2);
Serial.print(",");
Serial.print(limit3);
Serial.print(",");
Serial.print(limit4);
Serial.print(",");
Serial.print(limit5);
Serial.print(",");
Serial.println(limit6);
}
}

Vous aimerez peut-être aussi