More 2 stepper and ultrasonic problems

I've pretty much decided to switch to dc motors but I can't seem to let go of the problem which is:

I'm using two ULN2003 boards and 28BYJ-48 stepper motors running together. They work fine, a bit slow but torque is OK. However, when I add in an ultrasonic range finder and routine, the motors run erratically sometimes stopping. What I think is happening is the stepper code is somehow being interferred with by the ultrasonic unit. It must be generating some RF that is perhaps messing up the Arduino loop routine. Another possibility is the extra delay added by the ultrasonic routine is the problem. I've experimented with the stepper speed but no luck. I wish I hadn't sold my scope. It would have been nice to see the pulse forms.

I'd really like to solve this but I'm out of ideas. Help would be appreciated.

The Code:

// This sketch for the Arduino is to drive
// two BYJ28-48 stepper motors using ULN2003 interface
// boards as a potential use on a small robot. An ultrasonic
// sensor is included for range finding. Note: the sensor uses
// independent conections for trigger and echo.

// For public domain. 

// Ultrasonic sensor pin connections.
const int pingoutPin = 2;
const int pinginPin = 3;

// Motor pin variables.
 int motorPin5 = 4;    
 int motorPin6 = 5;    
 int motorPin7 = 6;    
 int motorPin8 = 7;                        
 int motorPin1 = 8;    
 int motorPin2 = 9;    
 int motorPin3 = 10;    
 int motorPin4 = 11;
 
 int motorSpeed = 1300;  // Variable to set stepper speed.
           
 // Binary output for motor drive.
 int lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};
 
 void setup() {
  
   pinMode(motorPin1, OUTPUT);
   pinMode(motorPin2, OUTPUT);
   pinMode(motorPin3, OUTPUT);
   pinMode(motorPin4, OUTPUT);
   pinMode(motorPin5, OUTPUT);
   pinMode(motorPin6, OUTPUT);
   pinMode(motorPin7, OUTPUT);
   pinMode(motorPin8, OUTPUT);
   
 
 }
//////////////////////////////////////////////////////////////////////////////
 void loop(){
   
  // Establish variables for duration of the ultrasonic pulse, 
  // and the distance result in inches:
  long duration, inches;
  
  // The ultrasonic sensor is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingoutPin, OUTPUT);
  digitalWrite(pingoutPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingoutPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingoutPin, LOW);
  
  // Get the pulse return time.
  pinMode(pinginPin, INPUT);
  duration = pulseIn(pinginPin, HIGH);

  // Convert the time into distance.
  inches = microsecondsToInches(duration);
  
  // Check distance and turn if too close to object.
   if(inches <= 12)
     pivotright();
    else
       forward(); 
 }

 //Sound travels one inch in 73.746 microseconds.
 //Get distance from ultrasonic sensor to object by dividing by 2.
 long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

//////////////////////////////////////////////////////////////////////////////
 // Set pins to ULN2003 high in sequence from 1 to 4.
 // Delay "motorSpeed" between each pin setting (to determine speed):
 
 void pivotright() // Turn routine.
 // Stop motor 2, reverse motor one.
 {
   for(int i = 0; i < 8; i++) 
   {
     setOutput2(i);
     delayMicroseconds(motorSpeed);
   }
 }
 
void forward() // Forward routine.
 {
   for(int i = 7; i >= 0; i--)
   {
     setOutput(i);
     delayMicroseconds(motorSpeed);
   }
 }
 
void setOutput(int out) //Forward output.
 {
   // Motor one output.
   digitalWrite(motorPin1, bitRead(lookup[out], 3));
   digitalWrite(motorPin2, bitRead(lookup[out], 2));
   digitalWrite(motorPin3, bitRead(lookup[out], 1));
   digitalWrite(motorPin4, bitRead(lookup[out], 0));
   // Motor two output.
   digitalWrite(motorPin5, bitRead(lookup[out], 3));
   digitalWrite(motorPin6, bitRead(lookup[out], 2));
   digitalWrite(motorPin7, bitRead(lookup[out], 1));
   digitalWrite(motorPin8, bitRead(lookup[out], 0)); 
 }
 void setOutput2(int out) //Turn output.
 {
   digitalWrite(motorPin5, bitRead(lookup[out], 3));
   digitalWrite(motorPin6, bitRead(lookup[out], 2));
   digitalWrite(motorPin7, bitRead(lookup[out], 1));
   digitalWrite(motorPin8, bitRead(lookup[out], 0));
   
 }

I just finished playing with the same two motors and figured out using a 74HC595 shit register between the Arduino and driver boards made things a lot easier. It requires 3 digital outputs from the Arduino; data, clock, and latch. The shiftOut command is used to transfer an 8bit word is to the shift register and then all the bits are sent to the motor driver at the same time when the Latch is set HIGH.

Stepper motor 2 - 74hc595.JPG

and figured out using a 74HC595 shit register

Is that how the supplier defined it?

Help would be appreciated.

Why is all the code in loop? Move the ping code to a function. That either works or it doesn't.

There is a Stepper library. Why aren't you using it?

You realize, I hope, that pulseIn() is a blocking function. That means that there will be possibly significant, variable length delays between calls to forward() or pivotright(). (Camel case for function names!).

Perhaps using the NewPing which uses interrupts instead of thumb-twiddling to read the ping sensors would make your code behave better.

PaulS:
There is a Stepper library. Why aren't you using it?

@pmlapl (easy name) use this: "two BYJ28-48 stepper motors using ULN2003 interface"
and I think is this: http://www.ebay.it/itm/5V-Geared-Stepper-Motor-28BYJ-48-ULN2003-Driver-Arduino-/171120983423?pt=UK_BOI_Electrical_Components_Supplies_ET&hash=item27d79afd7f
a very poor stepper (and strange) that have many problem with Stepper library.
On forum I have seen a user that made working this stepper/module and Stepper library with this strange parameters:

const int stepsPerRevolution = 2037;  // change this to fit the number of steps per revolution
Stepper myStepper(stepsPerRevolution, 8,10,11,9); // initialize the stepper library on pins 8 through 11:

Thanks to all,

It's been a while since I posted this question. I've since moved on to successfully using some contunuous servos. I also have been playing with gearmotors and an IR sensor that I finally got working. Thanks Paul.

nid69ita (hard name). You're correct that the stepper motors are a poor choice. They were cheap but we get what we pay for. They now sit in my pile of parts for potential future use.

Why is all the code in loop? Move the ping code to a function. That either works or it doesn't.

Paul, my Arduino programming experience started in July of this year. I'm still learning the ins and outs. I'll remember this "in or "out".

There is a Stepper library. Why aren't you using it?

I did initially but it didn't work very well. The stepper motors were more erratic than with using the binary method. After thinking about it, the problem might have been caused by low votage. I was powering the Arduino, motor controllers and sensor from the same source. I may go back and up the voltage to see what I get.

You realize, I hope, that pulseIn() is a blocking function.

No I didn't. That's another one of those Arduino function idiosyincracies. The info regarding it is probably noted somewhere on this forum, but I know not where. It would be nice if there was a single list of functions and how they affect the Arduino somewhere here. Though, perhaps there is. At the time I wrote the sketch my knowedge base was pretty limited. Now, I'll remember to do some function checking first.

Perhaps using the NewPing which uses interrupts instead of thumb-twiddling to read the ping sensors would make your code behave better.

I didn't know it existed. I'll do a search and give it a try, though I should probably do some study on interupts first.

(Camel case for function names!).

Once again, still learning the proprieties.

Pack007, I don't know if I want to learn to use shiFt registers just yet. Thanks though.

Regards to all,
Pierre