Fix my code for proximity sensor and stepper motors

#include <Stepper.h>

int STEPS = 200;       // change this to the number of steps on your motor

Stepper stepper(STEPS, 4, 5, 6, 7);        //create a stepper class and set pins
Stepper stepper2(STEPS, 8, 9, 11, 12);

const int TRIG_PIN = 2;                                       // Pins

const int ECHO_PIN = 3;
const unsigned int MAX_DIST = 23200;               // Anything over 400 cm (23200 us pulse) is "out of range"

void setup() {
 digitalWrite(TRIG_PIN, LOW);

void loop() {

 unsigned long t1, t2, pulse_width;
 float cmValue, inchesValue;

 digitalWrite(TRIG_PIN, HIGH);                                            // Hold the trigger pin high for at least 10 us
 digitalWrite(TRIG_PIN, LOW);

 while ( digitalRead(ECHO_PIN) == 0 );                               // Wait for pulse on echo pin
 t1 = micros();
 while ( digitalRead(ECHO_PIN) == 1);                                 //end of echo
 t2 = micros();
 pulse_width = t2 - t1;                                                          //calculate time between send pulse and returned pulse
 cmValue = pulse_width / 58.0;                                           //convert time into cm
 inchesValue = pulse_width / 148.0;                                   //convert time into inches

if ( pulse_width <= MAX_DIST ) {
   for (int i =0; i<50; i++);
 } else {
   Serial.print(cmValue);                                                     //print distance in cm
   Serial.print(" cm \t");
   Serial.print(inchesValue);                                               //print distance in inches
   Serial.println(" in");
}Preformatted text

Your post was MOVED to its current location as it is more suitable.

Do we get a clue as to what is wrong?

What Arduino board?

Read the forum guidelines for some hints on how to get the most from this forum.

I'm guessing the compiler didn't like that.

Not sure what that's for (no pun intended)

Would that line even survive the compiler optimization since it does nothing?

see pulseIn()

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