Stepper motor with ultrasonic sensor and tact switch

Hello, I'm writing because I have a problem coding.
First of all, this is my first writing. If there is anything against the regulations in this article, please leave a comment. Also, I am not good at English, so I am using a translator. If there's anything you don't understand, please also leave a comment.
I'm planning a machine that uses a stepper motor, a tact switch, and an ultrasonic sensor. This machine consists of 'open' mode and 'closed' mode. You can switch modes by clicking the tact switch once.
If you click the tact switch and change to 'closed' mode, the ultrasonic sensor measures the distance from the floor and moves the stepper motor by that distance. It stops afterwards. If the 'open' mode is changed by clicking the tact switch one more time, the stepper motor returns to its original position. What I'm having trouble with here is the part where the ultrasonic sensor measures the distance.
This is the code that I wrote.

#include <AccelStepper.h>
#define trigPin 1
#define echoPin 2
#define dirPin 7
#define stepPin 6
#define buttonPin 3
int buttonReading = HIGH;
int state = 0;
int previous = HIGH;
AccelStepper stepper(1, stepPin, dirPin);
double dist(void) {

  double duration, dis;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  dis = duration * 17 / 100;

  return dis;

}

void setup()
{
  Serial.begin(9600);
  digitalWrite(trigPin, OUTPUT);
  digitalWrite(echoPin, INPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  stepper.setMaxSpeed(200);
  stepper.setCurrentPosition(0);
}
void loop()
{
  buttonReading = digitalRead(buttonPin);
  if (buttonReading == LOW && previous == HIGH) {
    if (state == 0 || 3) {
      state = 1;
    }

    else if (state == 1 || 2) {
      state = 0;
    }
  }
  previous = buttonReading;
  double distance = dist();
  double bla = 15.7;
  int rotation = distance / bla;
  int nstep = rotation * 200;


  if (state == 1) {
    for (int a = 0; a < 1; a++) {
      stepper.move(nstep);
      stepper.setSpeed(200);
      stepper.runSpeedToPosition();
      state = 2;
    }  Serial.println("open");
    Serial.println(rotation);
    Serial.println(state);

  }
  else if (state == 2) {
    Serial.println("open");
    Serial.println(rotation);
    Serial.println(state);
  }
  else if (state == 0) {
    while (stepper.currentPosition() != 0)
    { stepper.move(0);
      stepper.setSpeed(-200);
      stepper.runSpeedToPosition();
      state = 3;
    }
    Serial.println("closed");
    Serial.println(state);
  }
  else if (state == 3) {
    Serial.println("close");
    Serial.println(state);
  }
}

It became too complicated as I added modifications from time to time. First, we set up a function to measure distance so that you can measure distance only once. (Of course, it failed.) Also, I added 2 and 3 to the state so that it would stop after the operation. However, the stepper motor did not work, the ultrasonic sensor is measuring the distance in real time, and no matter how hard you press the tack switch, it does not return from 'closed' mode to 'open'. It seems certain that there is a problem with the code. Could you tell me which part I need to modify?

The devices I use are HC-SR04, SM-42BYG011-25, DRV-8825, and arduino uno.

You are trying to get your processor to do more than one thing at the 'same' time, i.e. multi-tasking.

multitaskingDiagramSmall

See my tutorial on Multi-tasking in Arduino which as a complete stepper motor example that is controlled by both use input and a sensor.

A wiring diagram or, preferably, a schematic would also help us to help you.

On an Uno, pin 1 is the hardware serial TX pin.

Serial uses Pin 0 and Pin 1. Pick a different pin for your 'trigPin'.

Hi, @bass_hongki
Welcome to the forum.

Have you been able to control the motors?
Have you got code that just controls the motor?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
A Fritzy type picture is not preferred.

Tom.... :grinning: :+1: :coffee: :australia:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.