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?