#include "SR04.h" // changed for Wokwi
#include <Stepper.h>
//www.elegoo.com
//2018.10.25
/*
Stepper Motor Control - one revolution
This program drives a unipolar or bipolar stepper motor.
The motor is attached to digital pins 8 - 11 of the Arduino.
The motor should revolve one revolution in one direction, then
one revolution in the other direction.
*/
#define TRIG_PIN 12
#define ECHO_PIN 13
SR04 sr04 = SR04(ECHO_PIN, TRIG_PIN);
long a;
const int stepsPerRevolution = 2048; // change this to fit the number of steps per revolution
const int rolePerMinute = 15; // Adjustable range of 28BYJ-48 stepper is 0~17 rpm
int stepCount = 0;
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);
void setup() {
myStepper.setSpeed(rolePerMinute);
// initialize the serial port:
Serial.begin(9600);
}
void loop() {
a = sr04.Distance();
Serial.print(a);
Serial.println("cm");
delay(1000);
// Roatate Stepper if distance is more than 10cm
if (a <= 20)
{ // Rotate CW 1/2 turn slowly
myStepper.step(1);
Serial.print("steps:");
Serial.println(stepCount);
stepCount++;
delay(500);
}
//myStepper.setSpeed(rolePerMinute);
//myStepper.step(stepsPerRevolution);
// Serial.println("clockwise");
// myStepper.step(stepsPerRevolution);
}