I am using Ultrasonic Sensor (HC-SR04), a stepper motor, and two limit switch in the end of the motor track.
my plan is to control the stepper motor using the ultrasonic sensor.
if the distance is more than 20 cm, it should moves to the left (DIR HIGH).
if the distance is less than 20 cm, it should moves to the right (DIR LOW).
and if the motor moves way too far (it touchs the limit switch), it should stops the movement.
i've been working on this program for a long time, but for some reason the code that i made doesn't work. My stepper motor doesn't move at all, i don't know why.
could you guys kindly check my program and help me with this issues?
// Define stepper motor and ultrasonic sensor connections:
#define echoPin 6 // attach pin D6 Arduino to pin Echo of HC-SR04
#define trigPin 7 // attach pin D7 Arduino to pin Trig of HC-SR04
#define dirPin 10 // attach pin D2 Arduino to pin DIR+
#define stepPin 11 // attach pin D3 Arduino to pin STEP+
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement
unsigned long curMillis;
unsigned long prevStepMillis = 0;
unsigned long millisBetweenSteps = 25; // milliseconds
int limitswitch1 = 0;
int limitswitch2 = 0;
const int button8 = 8; // right limit switch (pin8)
const int button9 = 9; // left limit switch (pin9)
void setup() {
// Declare pins as output:
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
Serial.begin(9600);
pinMode(button8, INPUT_PULLUP); // right limit switch as input
pinMode(button9, INPUT_PULLUP); // left limit switch as input
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT);
limitswitch1 = digitalRead(button8); // limit swith kanan pada PIN 8
limitswitch2 = digitalRead(button9); // limit swith kiri pada PIN 9
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
curMillis = millis();
reading();
action();
}
void reading() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
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; // Speed of sound wave divided by 2 (go and back)
// Displays the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
}
void action(){
if(distance > 20 && limitswitch1 == 1 && limitswitch2 == 1) {
digitalWrite(dirPin, HIGH);
startmotor();}
if(distance < 20 && limitswitch1 == 1 && limitswitch2 == 1) {
digitalWrite(dirPin, LOW);
startmotor();}
if (digitalRead (button8) == 0) // if right limit switch is LOW (on)
{
digitalWrite (stepPin, LOW);
}
if (digitalRead (button9) == 0) // if left limit switch is LOW (on)
{
digitalWrite (stepPin, LOW);
}
}
void startmotor(){
if (curMillis - prevStepMillis >= millisBetweenSteps) {
prevStepMillis = curMillis;
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
}
}
That function is indeed problem. As it stands it will never start the motor because it looks only once at the millis counter, and if it has not advanced enough it will simply return. It needs to wait until the time has expired and then give a pulse. Your use of millis here is wrong it might as well be a simple delay.
The loop function resets the value of the current millis variable every time so your motors never get stepped.
Hi,
Has the suggested reason in post#6 fix your problem.
If not;
Have you developed your code in stages?
Have you written code that JUST tests the ultrasonic and displays the result in the IDE monitor?
Have you written code that JUST makes your stepper move back and forth.
Have you written code that JUST reads your limit switch and displays the result in the IDE monitor?
Have you checked all your I/O indepedently to prove your circuit?
Can you please post a circuit diagram, not a Fritzy image?
i've tried it separately,
ultrasonic and the result is fine.
limit switch works as it should be.
stepper motor can move back and forth when it use delayMicrosecond.
the problem is-- if im using the delayMicrosecond to step my stepper, it will interrupt my ultrasonic sensor (the sensor waits until the delay ends).
so then im using millis, and i think i didn't use it properly.
i really need help for using millis to combine ultrasonic and stepper motor
@adhityaj you ticked solution on reply #6, that means you consider the problem is solved. If that is not the case untick that box and carry on asking questions.
So post the code again where you have included the advise you have already been given.
The problem would seem to be that both the stepping and the distance sensor needs to be written as a state machine in order for it to multitask successfully.
You are using pulseIn which is blocking code.
That is the maximum speed you can generate pulses for your motor. If the motor will move that fast is another matter. That depends on a lot of factors, like the stall torque and the load on the motor as well as how it is powered.
With a stepping motor the faster you pulse it the lower the torque is. The torque is a maximum when it is not moving. You can pulse it so fast that there comes a point where the actual torque is below the stall torque and then it will not move any more.