Hello,
I have an old Ultrasonic sensor and a 28BYJ - stepper motor that I wanted to be controlled by the sensor. For some reason the code I have currently does not work.
My goal is for the machine is for the sensor to sense something, then run the stepper motor for 5 full rotations. Afterwards, I want it to delay for 5 minutes, and then only be able to start when the sensor senses something again (close by).
*(currently it will start right up as in the stepper will run or it will hit the delay then start stepper...).
I feel like I am just missing one part? or a bunch, I am just unsure and I have been re-writing this code/messing with the thing for about 5 hours straight! Any help is appreciated!
//defines pins numbers
#include <Stepper.h>
const int stepsPerRevolution = 120; //RPM
int x; // int
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
const int trigPin = 1;
const int echoPin = 2; //Where I connected all the pins on the arduino
const int ledPin = 3;
// defines variables
long duration;
int distance;
int safetyDistance;
int previous = 0;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(ledPin, OUTPUT);
myStepper.setSpeed(120);
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
safetyDistance = distance;
if (safetyDistance <= 5 && x !=10)
{
digitalWrite(ledPin, HIGH);
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
x = x + 1;
}
else
{
digitalWrite(ledPin, LOW);
delay(2000);
x
=0;
}
Ultrasonic.ino (1.28 KB)