 # Motor and ultrasonic sensor question

I have a PING))) ultrasonic sensor and I want my stepper motor to spin clockwise and counter clockwise (full spin 360 degrees). Every time the sensor reads between 20 to 100 cm. I was able to write the code for motor and it works perfectly. However, when I combined the code with the sensor reading the motor spins one step only, what could be the problem? I’m thinking that I need to add some loop to the motorTwoDirections() function, to iterate over 1200 steps clockwise and 1200 steps counter clockwise.

``````int signal=7;
int distance;
int D=0;
unsigned long pulseduration=0;
void setup()
{
pinMode(signal, OUTPUT);
Serial.begin(9600);
int Distance = 0;  // Record the number of steps we've taken

pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
}
void measureDistance()
{
// set pin as output so we can send a pulse
pinMode(signal, OUTPUT);
// set output to LOW
digitalWrite(signal, LOW);
delayMicroseconds(5);

// now send the 5uS pulse out to activate Ping)))
digitalWrite(signal, HIGH);
delayMicroseconds(5);
digitalWrite(signal, LOW);

// now we need to change the digital pin
// to input to read the incoming pulse
pinMode(signal, INPUT);

// finally, measure the length of the incoming pulse
pulseduration=pulseIn(signal, HIGH);
}

void motorTwoDirections()
{
digitalWrite(9, HIGH);
delayMicroseconds(100);
digitalWrite(9, LOW);
delayMicroseconds(100);
D = D + 1;   // record this step

// Check to see if we are at the end of our move
if (D == 1200)
{
// We are! Reverse direction (invert DIR signal)
{
digitalWrite(8, HIGH);
}
else
{
digitalWrite(8, LOW);
}
// Reset our distance back to zero since we're
// starting a new move
D = 0;
// Now pause for half a second
delay(500);

}
}

void loop()
{
// get the raw measurement data from Ping)))
measureDistance();

// divide the pulse length by half
pulseduration=pulseduration/2;

// now convert to centimetres. We're metric here people...
distance = int(pulseduration/29);

// Display on serial monitor
Serial.print("Distance - ");
Serial.print(distance);
Serial.println(" cm");
delay(500);

if (20 < distance && distance < 100)
{
motorTwoDirections();
}
}
``````

If you put a serial print in to print D, you'll see it is incrementing.

I don't have a stepper or a sensor to hand, so I hardcoded a distance of 80 (ie between 20 and 100) and watched leds on pins 8 and 9. I changed 1200 to 12 so as not to wait for ever, and the led on 9 pulses away merrily as D counts up, then the led on 8 toggles for direction and the led on 9 carries on after being reset.

So to me it seems to be working....

Then I guess it's because I didn't tell arduino to stop reading the sensor until the motor finishes rotating right and left. I just don't know how to disable the sensor's pin (pin#7) until the motor is done rotating.

Well my sketch still has the sensor code in, just overwriting the distance with a hardcoded one.

Did you put a serial print in to see what D's doing?

I added a for loop to the function and it seems to work, however, the updated function should make my motor rotates in one direction only, but when tested it the motor goes clockwise then counter clockwise!

``````void motorTwoDirections()
{
for (D = 0; D < 1200; D = D + 1 ){
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
delayMicroseconds(100);
digitalWrite(9, LOW);
delayMicroseconds(100);
}
``````

Why are you using one letter global variable names? The letter D conveys NO information about what the variable is for.

`````` int Distance = 0;  // Record the number of steps we've taken
``````

A local variable with the same name as a global variable is not the mark of the sharpest crayon in the box.

Distance is NOT number of steps.

`````` // divide the pulse length by half
pulseduration=pulseduration/2;
``````

Comments that state the obvious are useless. Comments should explain WHY the code is doing what it is doing, not what the code is doing.

When making code changes, repost ALL of your code. Use Tolls + Auto Format to fix your less-than-stellar indenting.