Hi,
as i mentioned earlier i’m new to this platform. After combining two different codes for HC-SR04 & Stepper, I could get a partial result of my requirement. I have only connected one Stepper motor at the moment (Providing external power supply). I can run the Stepper and when the distance is <10cm, stepper is stopped as expected.
However one issue i’m facing at the moment is that there is a delay of almost 5 secs between 2 distance measurement readings on SERIAL MONITOR for distances more than 10cm.
As soon as it comes to less than 10cm reading, the stepper is getting stopped as expected, and the subsequent distance measurements are pretty fast (in intervals of millisecs).
Slow readings occur only when the distance is more than 10 cm. Here’s the code:
```
*#include <NewPing.h>
//Include the Arduino Stepper Library
#include <Stepper.h>
// Define Constants
// Number of steps per internal motor revolution
const float STEPS_PER_REV = 32;
// Amount of Gear Reduction
const float GEAR_RED = 64;
// Number of steps per geared output rotation
const float STEPS_PER_OUT_REV = STEPS_PER_REV * GEAR_RED;
// Define Variables
// Number of Steps Required
int StepsRequired;
// Create Instance of Stepper Class
// Specify Pins used for motor coils
// The pins used are 8,9,10,11
// Connected to ULN2003 Motor Driver In1, In2, In3, In4
// Pins entered in sequence 1-3-2-4 for proper step sequencing
Stepper steppermotor(STEPS_PER_REV, 8, 10, 9, 11);
// defines pins numbers for ultrasonic sensor
const int trigPin = 6;
const int echoPin = 7;
// defines variables for ultrasonic sensor
long duration;
int distance;
void setup()
{
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
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;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
// Roatate Stepper if distance is more than 10cm
if(distance>=10)
{// Rotate CW 1/2 turn slowly
StepsRequired = 2048;
steppermotor.setSpeed(700);
steppermotor.step(StepsRequired);}
}*
```
Could someone help me with this…why the readings are as slow as 5 Sec when the sensor is clear of obstructions?
Thanx in advance