This is my first time program. I try to make my stepper motor (28BYJ=48) move when detect movement using Ultrasonic (HC-S404).
Model : Uno Rev3
[code]
#include <Stepper.h>
#define STEPS 2048
Stepper stepper(STEPS, 8, 10, 9, 11)
//const int dirPin = 2;
//const int stepPin = 3;
//const int enPin = 8;
const int trigPin = 2;
const int echopin = 3;
long duration;
int distancecm;
void setup ()
{
Serial.begin(9600);
//pinMode(dirPin,OUTPUT);
//pinMode(stepPin,OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//digitalWrite(enPin,OUTPUT);
}
void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distancecm = duration * 0.034 / 2; //V=2
if (distancecm <= 10)
{
stepper.setSpeed(15);
stepper.step(2048 * .5);
delay (1000);
stepper.setSpeed(15);
stepper.step(-2048 * .5);
}
}
[/code]Preformatted text