Go Down

Topic: MotorKnob Tutorial (Read 658 times) previous topic - next topic

jfranswa

Aug 28, 2011, 03:25 am Last Edit: Aug 28, 2011, 04:12 am by jfranswa Reason: 1
I want to use a sharp IR range sensor to control a stepper motor using the code from the MotorKnob example in the Arduino library.  The problem is when I turn on the arduino the stepper motor automatically starts turning a few times and then begins to turn according to the reading on the sensor (or potentiometer as used in the example).  Why does this happen and is there a way to eliminate this issue?

The example:
http://arduino.cc/en/Tutorial/MotorKnob

The code:
Code: [Select]
/*
* MotorKnob
*
* A stepper motor follows the turns of a potentiometer
* (or other sensor) on analog input 0.
*
* http://www.arduino.cc/en/Reference/Stepper
* This example code is in the public domain.
*/

#include <Stepper.h>

// change this to the number of steps on your motor
#define STEPS 100

// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to
Stepper stepper(STEPS, 8, 9, 10, 11);

// the previous reading from the analog input
int previous = 0;

void setup()
{
 // set the speed of the motor to 30 RPMs
 stepper.setSpeed(30);
}

void loop()
{
 // get the sensor value
 int val = analogRead(0);

 // move a number of steps equal to the change in the
 // sensor reading
 stepper.step(val - previous);

 // remember the previous value of the sensor
 previous = val;
}

Go Up