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));
}