# Problem moving stepper based on measurements with HC-SR04 [SOLVED]

Hey all!

First of all let me introduce myself. My name is Stan. I'm studying Industrial Product Design. And I'm new to this forum.

I'm not completely new to arduino but really experienced neither.

Since a couple of weeks I am working on a project for school where I need to control a DC motor with PWM, read out the speed and need to control a Z-axis (with stepper motor) by reading out the distance with a ultrasonic meter. By pushing a button I can control the Z-axis, and depending on what is measured the stepper will move to the left or right (and so the Z-axis).

The first part of the project is already finished, I can control a DC motor with PWM and read it out with a infrared reflective sensor.

At the end I need to merge the two codes (DC motor PWM, reading speed and Z-Axis) so it all works together.

At this moment I succeeded in making move the stepper motor to the left or right (if measured >60 or <60) by pushing the button. But if I push the button it will keep on measuring.

What I want it to do is by pushing the button it will measure one or two times and based on that measurement move it to a certain direction.

I've did my homework on google before posting it here. But did not get any (good) results compared to my project.

so, here is the code...

``````#include <NewPing.h>
#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 6, 7);

int triq = 3;
int echo = 2;

int stepCount = 0; // number of steps the motor has taken
int motorSpeed = 2;

int time = 0;
int timeWait = 500;
int maxDelay = 500;

long pulse, inches, cm;
//int distance = cm;
boolean distance = false;

const int buttonPin = 8;
int buttonState = 0;

NewPing sonar (3, 2);

return distance;
}

void setup() {
Serial.begin(9600);
time = millis();
pinMode(triq, OUTPUT);
pinMode(echo, INPUT);
pinMode(buttonPin, INPUT);
}

void loop() {

if(buttonState == HIGH){
long duration, distance;
digitalWrite(triq, LOW);
delayMicroseconds(2);
digitalWrite(triq, HIGH);
delayMicroseconds(5);
digitalWrite(triq, LOW);
delayMicroseconds(10);
duration = pulseIn(echo, HIGH);
distance = (duration/2) / 2.91;
Serial.print ("mm ");
Serial.print (distance);
Serial.println();

if (distance > 60){
if (motorSpeed > 0)
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(2);

} else {
if (motorSpeed > 0)
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(-2);
}
// put your main code here, to run repeatedly:
}
}
``````

The way the code is written, the rangefinder will fire over and over all the time while the button is pressed. Use the state change detection method to fire the rangefinder once for each button press (transition from low to high).

Do you have a pulldown resistor on the button input?

Hey groundFungus!

Thank you for your reply. Well I've put a 10K resistor between the button's ground.

So the part of pushing the button and releasing it works fine.

I thought state change was only intended for a push button to remember the state. But I can also use it for the measuring in the loop?

I'll try. Thanks alot

Hey GroundFungus

I did implement the state change function. But now if I press the button the stepper only moves one time and does not keep on moving based on the measurement.

``````void loop() {

if (buttonState != lastButtonState) {
//Compare the buttonState to its previous state
if(buttonState == HIGH){
buttonPushCounter++;
long duration, distance;
digitalWrite(triq, LOW);
delayMicroseconds(2);
digitalWrite(triq, HIGH);
delayMicroseconds(5);
digitalWrite(triq, LOW);
delayMicroseconds(10);
duration = pulseIn(echo, HIGH);
distance = (duration/2) / 2.91;
Serial.print ("mm ");
Serial.print (distance);
Serial.println();
lastButtonState = buttonState;

if (buttonState = HIGH);
if (distance > 60){
if (motorSpeed > 0)
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(2);

} else {
if (motorSpeed > 0)
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(-2);}
delay(50);
}

// save the current state as the last state, for next time through the loop
}
}
``````

and the entire code:

``````#include <NewPing.h>
#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 6, 7);

int triq = 3;
int echo = 2;

int stepCount = 0; // number of steps the motor has taken
int motorSpeed = 20;

int time = 0;
int timeWait = 500;
int maxDelay = 500;

long pulse, inches, cm;
//int distance = cm;
boolean distance = false;

const int buttonPin = 8;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;

NewPing sonar (3, 2);

return distance;
}

void setup() {
Serial.begin(9600);
time = millis();
pinMode(triq, OUTPUT);
pinMode(echo, INPUT);
pinMode(buttonPin, INPUT);
}

void loop() {

if (buttonState != lastButtonState) {
//Compare the buttonState to its previous state
if(buttonState == HIGH){
buttonPushCounter++;
long duration, distance;
digitalWrite(triq, LOW);
delayMicroseconds(2);
digitalWrite(triq, HIGH);
delayMicroseconds(5);
digitalWrite(triq, LOW);
delayMicroseconds(10);
duration = pulseIn(echo, HIGH);
distance = (duration/2) / 2.91;
Serial.print ("mm ");
Serial.print (distance);
Serial.println();
lastButtonState = buttonState;

if (buttonState = HIGH);
if (distance > 60){
if (motorSpeed > 0)
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(2);

} else {
if (motorSpeed > 0)
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(-2);}
delay(50);
}

// save the current state as the last state, for next time through the loop
}
}
``````

It is not just for buttons. There are many times where you want to sense the transition (off to on or on to off) and/or the polarity of the transition (low to high or high to low).

The code that runs the motors in inside the if block that reads the switch and runs only on the low to high transition. Move it out to where it can run in loop().

``````if (buttonState = HIGH);
``````

= for assignment and == for compare. If statement should not end with semicolon [;].

``````if (motorSpeed > 0)
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(2);
``````

Because there are no curly brackets to enclose this code, only the line after the if is executed conditionally. The myStepper.step executes unconditionally. Same with the if inside the else below.

Sorry, now I just don't get it anymore.

I've tried it but then the stepper motor is keeping running. And now i've disabled the part to first start with the measuring and make it work. But if I now press on the button it only measures ones but after the first time pressing the button it doesn't work anymore.

I can't see what you did so how can I help? When you make changes to the code you need to post the new version. Put in a new post, do not change your original post.

Ipollution:
Sorry, now I just don't get it anymore.

You need to post your program.

My guess is that you need a variable that gets set to true when the button is detected and which signifies that the motor may run (known as a state variable) and when the motor run is finished that variable is set back to false. The variable might simply be called motorOkToRun and the motor code might start with

``````void motorRun() {
if (motorOkToRun == true) {
// code to make the motor move
}
}
``````

...R

Oh oops stupid of me.

First i've tried to place the part of the motor in the loop. But when I did that, the stepper motor kept running.

The code:

``````#include <NewPing.h>
#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 6, 7);

int triq = 3;
int echo = 2;

int stepCount = 0; // number of steps the motor has taken
int motorSpeed = 20;

int time = 0;
int timeWait = 500;
int maxDelay = 500;

long pulse, inches, cm;
//int distance = cm;
boolean distance = false;

const int buttonPin = 8;
int buttonPushCounter = 1;
int buttonState = 0;
int lastButtonState = 0;

NewPing sonar (3, 2);

return distance;
}

void setup() {
Serial.begin(9600);
time = millis();
pinMode(triq, OUTPUT);
pinMode(echo, INPUT);
pinMode(buttonPin, INPUT);
}

void loop(){

if (buttonState == HIGH){
{
if (buttonState != lastButtonState)
//Compare the buttonState to its previous state
buttonPushCounter++;
long duration, distance;
digitalWrite(triq, LOW);
delayMicroseconds(2);
digitalWrite(triq, HIGH);
delayMicroseconds(5);
digitalWrite(triq, LOW);
delayMicroseconds(10);
duration = pulseIn(echo, HIGH);
distance = (duration/2) / 2.91;
Serial.print ("mm ");
Serial.print (distance);
Serial.println();
lastButtonState = buttonState;
}
}

if (buttonState == HIGH);
if (distance > 60);
if (motorSpeed > 0)
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(2);

if (buttonState == HIGH);
if (motorSpeed > 0)
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(-2);

}

// save the current state as the last state, for next time through the loop
``````

Besides that when I tried to push the button a second time it did not measure or react anymore. So later I merged it again together. But if I did that the function of state change did not work anymore.

the code:

``````#include <NewPing.h>
#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 6, 7);

int triq = 3;
int echo = 2;

int stepCount = 0; // number of steps the motor has taken
int motorSpeed = 50;

int time = 0;
int timeWait = 500;
int maxDelay = 500;

long pulse, inches, cm;
//int distance = cm;
boolean distance = false;

int buttonPin = 8;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;

NewPing sonar (3, 2);

return distance;
}

void setup() {
Serial.begin(9600);
time = millis();
pinMode(triq, OUTPUT);
pinMode(echo, INPUT);
//  pinMode(echo, LOW);
pinMode(buttonPin, INPUT);
//  pinMode(6,OUTPUT); // Step
//  pinMode(7,OUTPUT); // Dir
}

void loop() {
{
if (buttonState == HIGH){
if (buttonState != lastButtonState){
//Compare the buttonState to its previous state
buttonPushCounter++;
long duration, distance;
digitalWrite(triq, LOW);
delayMicroseconds(2);
digitalWrite(triq, HIGH);
delayMicroseconds(5);
digitalWrite(triq, LOW);
delayMicroseconds(10);
duration = pulseIn(echo, HIGH);
distance = (duration/2) / 2.91;
Serial.print ("mm ");
Serial.print (distance);
Serial.println();
lastButtonState = buttonState;

return distance;
if (distance > 80){
if (motorSpeed > 0)
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(4);

} else {
if (motorSpeed > 0)
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(-4);
}
// put your main code here, to run repeatedly:
}
}
}
``````

If you use the AutoFormat tool to indent your program it makes the related parts much easier to see. Have a look at this (which I did manually)

``````#include <NewPing.h>
#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 6, 7);

int triq = 3;
int echo = 2;

int stepCount = 0; // number of steps the motor has taken
int motorSpeed = 50;

int time = 0;
int timeWait = 500;
int maxDelay = 500;

long pulse, inches, cm;
//int distance = cm;
boolean distance = false;

int buttonPin = 8;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;

NewPing sonar (3, 2);

return distance;
}

void setup() {
Serial.begin(9600);
time = millis();
pinMode(triq, OUTPUT);
pinMode(echo, INPUT);
//  pinMode(echo, LOW);
pinMode(buttonPin, INPUT);
//  pinMode(6,OUTPUT); // Step
//  pinMode(7,OUTPUT); // Dir
}

void loop() {
{
if (buttonState == HIGH){
if (buttonState != lastButtonState){
//Compare the buttonState to its previous state
buttonPushCounter++;
long duration, distance;
digitalWrite(triq, LOW);
delayMicroseconds(2);
digitalWrite(triq, HIGH);
delayMicroseconds(5);
digitalWrite(triq, LOW);
delayMicroseconds(10);
duration = pulseIn(echo, HIGH);
distance = (duration/2) / 2.91;
Serial.print ("mm ");
Serial.print (distance);
Serial.println();
lastButtonState = buttonState;

return distance;

if (distance > 80){
if (motorSpeed > 0)
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(4);

} else {
if (motorSpeed > 0) {
myStepper.setSpeed(motorSpeed);
}
// step 1/100 of a revolution:
myStepper.step(-4);
}
// put your main code here, to run repeatedly:
}
}
}
``````

First thing to say is that the code does not seem to be complete as the opening { in loop() has no closing }. Or maybe there is just one { too many.

Second thing, the line return distance; in loop() means that the rest of the code in loop() will not be reached.

Third thing (but a lot less important) - it makes no sense to return a value from loop() because there is nothing to receive the value.

Assuming those things are sorted out it does not IMHO make any sense to have all of the code in the

``````IF (buttonState != lastButtonState) {
``````

The test for the button should just set a variable that can be used by other parts of the program so that they can run when the button is released.

Hey Robin2,

And yes I know, my coding is a bit crappy. I nearly do not know what I'm doing.
But the first sketch was a sketch that was working. The only problem was that when it reached the 60 it got back to the other way (so the Z-axis will move up again).

I want it to going down if button pushed, and when the Z-axis is down and you push again on the button the Z-axis will move up based on the calculation (distance = <60).

I just don't know how to do it. And I'm working a pretty long time on it and not moving forward.

Here is the last update from what i've been doin. But now the problem is that it only will measure once, and after pushing the button doesn't measure anymore and by pushing the button the stepper motor will move but doesn't move based on the result of the distance.

the code:

``````#include <NewPing.h>
#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 6, 7);

int triq = 3;
int echo = 2;

int stepCount = 0; // number of steps the motor has taken
int motorSpeed = 50;

int time = 0;
int timeWait = 500;
int maxDelay = 500;

long pulse, inches, cm;
//int distance = cm;
boolean distance = false;

int buttonPin = 8;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
int motorOkToRun = 0;

NewPing sonar (3, 2);

return distance;
}

void motorRun() {
if (motorOkToRun == true) {
// code to make the motor move
}
}

void setup() {
Serial.begin(9600);
time = millis();
pinMode(triq, OUTPUT);
pinMode(echo, INPUT);
pinMode(buttonPin, INPUT);
}

void loop() {
{
if (buttonState == HIGH){
if (buttonState != lastButtonState){
//Compare the buttonState to its previous state
buttonPushCounter++;
long duration, distance;
digitalWrite(triq, LOW);
delayMicroseconds(2);
digitalWrite(triq, HIGH);
delayMicroseconds(5);
digitalWrite(triq, LOW);
delayMicroseconds(10);
duration = pulseIn(echo, HIGH);
distance = (duration/2) / 2.91;
if (lastButtonState = buttonState);
Serial.print ("mm ");
Serial.print (distance);
Serial.println();
}

{    if (buttonPushCounter >= 2);
buttonPushCounter = 0;

if (motorOkToRun == true);
if (distance > 80){
if (motorSpeed > 0)
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(4);

} else {
if (motorSpeed > 0)
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(-4);
}

// put your main code here, to run repeatedly:
}
}
}
}
``````

Please use the AutoFormat tool to indent your code for easier reading and re-post the code. I did it once but I'm not doing it a second time. And it will be a big benefit for you also.

...R

oh oops sorry!

What I want it to do is that when you press the button and hold it the stepper motor moves to a certain way based on one (or two) measurements from the ultrasonic meter. And in the first code it keeps on measuring when you hold the button. So the only problem was that when it reached the 60 it got back to the other way (so the Z-axis will move up again).

So here is the first code:

``````#include <NewPing.h>
#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 6, 7);

int triq = 3;
int echo = 2;

int stepCount = 0; // number of steps the motor has taken
int motorSpeed = 50;

int time = 0;
int timeWait = 500;
int maxDelay = 500;

long pulse, inches, cm;
boolean distance = false;

int buttonPin = 8;
int buttonState = 0;

NewPing sonar (3, 2);

return distance;
}

void setup() {
Serial.begin(9600);
time = millis();
pinMode(triq, OUTPUT);
pinMode(echo, INPUT);
pinMode(buttonPin, INPUT);
}

void loop() {

if (buttonState == HIGH) {
long duration, distance;
digitalWrite(triq, LOW);
delayMicroseconds(2);
digitalWrite(triq, HIGH);
delayMicroseconds(5);
digitalWrite(triq, LOW);
delayMicroseconds(10);
duration = pulseIn(echo, HIGH);
distance = (duration / 2) / 2.91;
Serial.print ("mm ");
Serial.print (distance);
Serial.println();

if (buttonState == HIGH) {
if (distance > 60) {
if (motorSpeed > 0)
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(2);

} else {
if (motorSpeed > 0)
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(-2);
}
// put your main code here, to run repeatedly:
}
}
}
``````

And here you have the code where I did my last changes on (and maybe do not really know anymore what).

``````#include <NewPing.h>
#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 6, 7);

int triq = 3;
int echo = 2;

int stepCount = 0; // number of steps the motor has taken
int motorSpeed = 50;

int time = 0;
int timeWait = 500;
int maxDelay = 500;

long pulse, inches, cm;
//int distance = cm;
boolean distance = false;

int buttonPin = 8;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
int motorOkToRun = 0;

NewPing sonar (3, 2);

return distance;
}

void motorRun() {
if (motorOkToRun == true) {
// code to make the motor move
}
}

void setup() {
Serial.begin(9600);
time = millis();
pinMode(triq, OUTPUT);
pinMode(echo, INPUT);
pinMode(buttonPin, INPUT);
}

void loop() {
{
if (buttonState == HIGH) {
if (buttonState != lastButtonState) {
//Compare the buttonState to its previous state
buttonPushCounter++;
long duration, distance;
digitalWrite(triq, LOW);
delayMicroseconds(2);
digitalWrite(triq, HIGH);
delayMicroseconds(5);
digitalWrite(triq, LOW);
delayMicroseconds(10);
duration = pulseIn(echo, HIGH);
distance = (duration / 2) / 2.91;
if (lastButtonState = buttonState);
Serial.print ("mm ");
Serial.print (distance);
Serial.println();
}

{ if (buttonPushCounter >= 2);
buttonPushCounter = 0;

if (motorOkToRun == true);
if (distance > 80) {
if (motorSpeed > 0)
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(4);

} else {
if (motorSpeed > 0)
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(-4);
}

// put your main code here, to run repeatedly:
}
}
}
}
``````

You really need to study an introductory C/C++ tutorial to learn the basics of IF statements and the use of braces {}

For example this does not make sense

``````      if (buttonState != lastButtonState) {
//Compare the buttonState to its previous state
buttonPushCounter++;
long duration, distance;
digitalWrite(triq, LOW);
delayMicroseconds(2);
digitalWrite(triq, HIGH);
delayMicroseconds(5);
digitalWrite(triq, LOW);
delayMicroseconds(10);
duration = pulseIn(echo, HIGH);
distance = (duration / 2) / 2.91;
if (lastButtonState = buttonState);
Serial.print ("mm ");
Serial.print (distance);
Serial.println();
}
``````

and I'm not sure where to start.

This line if (lastButtonState = buttonState); makes no sense where it is because the code can only get there if lastButtonState is not the same as buttonState. And the trouble is I don't know what you really intended.

Maybe there should have been a } before that line to close off the if (buttonState != lastButtonState) {

But then there is another problem because the semi-colon at the end of if (lastButtonState = buttonState); closes that off before it can do anything. Probably the ; should be a {

It may be that there are other similar issues.

Moving on ... you have now created a little function motorRun() but it is not called from the code anywhere and as far as I can see the code that actually runs the motor is still inside some of the IF clauses.

...R

I know and I understand you, I did study an introductory before starting with it. I've tried a couple of different times different ways to put the { or } but it just doesn't matter. There are still being problems

If put it for the if (lastButtonState = buttonState) it won't complete the loop and the part of the stepper motor doesn't work anymore.

I don't know enough about it to know where to put it right and how to do it right but I try every time different things. I've did readed the introductory but I just do not get it.

Also the function motorRun() i've tried it but just do not get it. In the last one its not visible because I've deleted it again, beceause it won't work or do anything

The part of the first sketch that I got it working is just a miracle to me.

But I was thinking maybe its easyer to put a time limit on it. So it measures the distance for about a half second and stops with measuring. But then the stepper motor needs to keep on going?

Have you looked at the link I gave you in Reply #9 and studied how that program is constructed and how the parts interact?

The reason the function motorRun() does not work is for two reasons {A} you have not put any useful code inside it and {B} you do not call it from any other part of your program.

At the moment you seem to be treating your program like a fruit-cake where everything is thrown into the mixing bowl and vigorously stirred. But programming is not like that. It is more like building a house in which you have a series of individual bricks with special shapes for particular purposes. The bricks can each be made separately and confirmed to be the right size and shape before being brought to the building site. Think of functions as the equivalent of my bricks.

...R

Hey Robin2!

Yes I've looked at it, but i've still need to say that all that information is still a bit confusing to me beceause of some words that are used and how certain things are called.

But I've managed it and I've done it!

The reason the function motorRun() does not work is for two reasons {A} you have not put any useful code inside it and {B} you do not call it from any other part of your program.

And except for the fact I've did not do that I've did it in a other way and managed it. I'm still gladd you called it!

At the moment you seem to be treating your program like a fruit-cake where everything is thrown into the mixing bowl and vigorously stirred. But programming is not like that. It is more like building a house in which you have a series of individual bricks with special shapes for particular purposes. The bricks can each be made separately and confirmed to be the right size and shape before being brought to the building site. Think of functions as the equivalent of my bricks.

Whahaha well I can only say that you are true but I think that is because I'm studying for Product Design and not for Programming.

But what I've did is put the part of measuring outside the loop, add a state change to it and now it works!

So here is the end code:

``````#include <NewPing.h>
#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 6, 7);

int triq = 3;
int echo = 2;

int stepCount = 0; // number of steps the motor has taken
int motorSpeed = 50;

int time = 0;
int timeWait = 500;
int maxDelay = 500;

long pulse, inches, cm;
//boolean distance = false;

int buttonPin = 8;
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;
int lastButtonState = 0;     // previous state of the button

boolean buttonActive = false;
boolean longPressActive = false;

//int current = 0;
//unsigned long buttonPress = 250;

//long buttonTimer = 0;
//long buttonPressTimer = 250;
long duration, distance;

NewPing sonar (3, 2);

return distance;
}

void setup() {
Serial.begin(9600);
time = millis();
pinMode(triq, OUTPUT);
pinMode(echo, INPUT);
pinMode(buttonPin, INPUT);
}

void measure()  {
if (buttonState != lastButtonState) {

digitalWrite(triq, LOW);
delayMicroseconds(2);
digitalWrite(triq, HIGH);
delayMicroseconds(5);
digitalWrite(triq, LOW);
delayMicroseconds(10);
duration = pulseIn(echo, HIGH);
distance = (duration / 2) / 2.91;
//  delay(1000);
Serial.print ("mm ");
Serial.print (distance);
Serial.println();
lastButtonState = buttonState;
}

}

void loop() {
if (buttonPushCounter % 1 == 0)
measure();
{
if (buttonState == HIGH) {
if (distance > 60) {
//        while (buttonState == HIGH){
if (motorSpeed > 0)
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(2);

} else {
if (motorSpeed > 0)
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(-2);
}
}
// put your main code here, to run repeatedly:
}
}
``````

Thank you for all you help!