int trigPin = 11; //Trigger
int echoPin = 12; //Echo (tangkapan)
long duration, cm, inches;
const int step1Pin = 7;
const int dir1Pin = 6;
const int step2Pin = 4;
const int dir2Pin = 3;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(step1Pin,OUTPUT);
pinMode(dir1Pin,OUTPUT);
pinMode(step2Pin,OUTPUT);
pinMode(dir2Pin,OUTPUT);
}
void loop() {
// This Line for Ultrasonic
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
cm = (duration/2) / 29.1;
inches = (duration/2) / 74;
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(250);
//This Line for Motor
digitalWrite(dir1Pin,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 200; x++) {
digitalWrite(step1Pin,HIGH);
delayMicroseconds(500);
digitalWrite(step1Pin,LOW);
delayMicroseconds(500);
}
delay(1000); // One second delay
digitalWrite(dir1Pin,LOW); //Changes the rotations direction
// Makes 400 pulses for making two full cycle rotation
for(int x = 0; x < 400; x++) {
digitalWrite(step1Pin,HIGH);
delayMicroseconds(500);
digitalWrite(step1Pin,LOW);
delayMicroseconds(500);
}
delay(1000);
}