Dog treadmill with stepper motor drive and ultrasound sensor for centering dog

I am trying to build a dog treadmill which keeps him centered, by detecting his position using an ultrasound sensor and altering the speed of the treadmill accordingly: a) When he is centered, the speed is at a set point b) As he begins to tire and fall back, the speed of the treadmill will slow c) When he disappears from the treadmill, it will stop completely

Pls kindly advise: 1. For controlling the stepper motor, is it better to use a dedicated Stepper Motor Driver (like the EasyDriver from SaprkFun), or to drive it directly from Arduino through a transistor?

If it is the latter, is it better to use the the Stepper Motor Library , or use the analogWrite(9,ldr) PWM command or similar?

Do you think the following sketch will work? Driving stepper motor speed according to ultrasound distance

include

int ledPin 13; int v = 0; const int pingPin = 7; // Pin no. of the ultrasound sensor's output Stepper myStepper(200, 8,9,10,11); // initialize the Stepper library

void setup() { Serial.begin(9600); // Initialize the Serial port pinMode(ledPin, OUTPUT); // set up the LED pin }

void loop() { long duration, inches, cm; // establish variables for duration of the ping, and the distance result in inches and centimeters

pinMode(pingPin, OUTPUT); digitalWrite(pingPin, LOW); // The PING))) is triggered by a HIGH pulse of >=2 ms. Give a short LOW pulse beforehand to ensure a clean HIGH pulse delayMicroseconds(2);

digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, LOW); pinMode(pingPin, INPUT); // The same pin reads the signal from the PING))) duration = pulseIn(pingPin, HIGH); // a HIGH pulse, whose duration = time (ms) from sending of the ping to the reception of its echo off an object cm = microsecondsToCentimeters(duration); // convert the time into a distance Serial.print(cm); Serial.print("cm"); Serial.println(); delay(100); if (cm < 50) v = v+10;
if (cm > 70) v = v-10;
if (cm > 80) v = 0;
myStepper.setSpeed(v); // set the motor speed at v RPMS }

// PING))) Datasheet : Speed of sound = 340 m/s (29ms/s) or 73.746ms/in (113’/s); /2 to get distance of obstacle.
long microsecondsToCentimeters(long microseconds) // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf { return microseconds / 29 / 2; // divide by 2 to get the distance of object }

Thanks for your kind assistance, in advance.

Wei

  1. For controlling the stepper motor, is it better to use a dedicated Stepper Motor Driver (like the EasyDriver from SaprkFun), or to drive it directly from Arduino through a transistor?

Personally, I don't think a stepper motor is appropriate, at all. Stepper motors are designed for accurate positioning, as is required in plotters and NC machines. Accurate positioning of the treadmill belt hardly seems like a necessity.

A DC motor and driver shield should be more than adequate, and a whole lot cheaper.

Do you think the following sketch will work?

Compile and link and try it. Although there are several things about that sketch that I do not like. Multiple statements on one line make the logic much harder to follow, and make changes more difficult.

The ping code belongs in a different function. Comments that state the obvious need to be deleted. Comments that describe what the program is trying to do should be added.

In that code, the final brace for the loop function is after the comment on the previous line, making it invisible to the compiler.

Setting the speed of the stepper motor is only one part of making it move. All that it accomplishes is defining the time between steps. Since you never actually step the motor, it won't ever actually move.

Finally, mixing the code to set the speed with the code for reading the distance is a recipe for disaster.

yuwshin:
Do you think the following sketch will work?

If you have the ultrasound sensor, why not give it a go?

Can you read a range from the sensor? Does the range vary sensibly based on what's in front of the sensor? Does you sketch choose a sensible speed corresponding to that distance?

I guess you'll arrange the treadmill so that the dog moves towards the sensor as it walks along the belt and the belt moves to move him back away from the sensor. I hope you'll arrange it so that the dog could, if necessary, run past the sensor and off the front of the belt without coming to any harm.

You're using the ping ultrasonics? Basicaly every single ultrasonic rangefinder out there is between 40 and 42khz. Dogs hear above that range. I haven't been able to locate reasonbly-priced ultrasonics in the 60khz+ range.

If the u/s thing doesn't work, you might try light blocking. Setup some Leds
with narrow beams at the top [can use light tubes], and pickup sensors at the
bottom. Pickups mounted to the sides, Leds mounted in the middle and
pointing outwards at an angle towards the pickups. If the dog strays from
center, it blocks the light paths. Just a thought.