Can't seem to get the basic MotorKnob sketch to work right, the problem seems to be that the output never seems to stop, I have it connected to drive a unipolar motor M35SP-9 (through a UNL2003a) am I missing something?
am I missing something?
Some debug prints?
I feel like I should know this but, :~ ...do you mean the serial output from the input (A0)? ...which bounces up and down a digit or two
437
437
437
436
437
436
436
437
437
or the sketch i'm using?;
/*
- MotorKnob
- A stepper motor follows the turns of a potentiometer
- (or other sensor) on analog input 0.
- Stepper - Arduino Reference
- This example code is in the public domain.
*/
#include <Stepper.h>
// change this to the number of steps on your motor
#define STEPS 48 //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()
{
Serial.begin(9600);
// set the speed of the motor to 30 RPMs
stepper.setSpeed(30);
}
void loop()
{
// get the sensor value
int val = analogRead(A0);
// val = constrain(val, 0, 255); //doesn't help
// delay(1); //doesn't help either
// 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;
Serial.println(val);
}
analogRead tends to oscillate a bit as you can see from your debug data. Check the number of steps you're going to do and ignore them unless they're greater than some small threshold (5?). Make sure that your test works with positive and negative numbers - the abs function may be helpful here, though not really necessary.
The below is for servos, but the dead band part might be of use in your stepper setup.
//zoomkat dual pot/servo test 12-29-12
//view output using the serial monitor
#include <Servo.h>
Servo myservo1;
Servo myservo2;
int potpin1 = 0; //analog input pin A0
int potpin2 = 1;
int newval1, oldval1;
int newval2, oldval2;
void setup()
{
Serial.begin(9600);
myservo1.attach(2);
myservo2.attach(3);
Serial.println("testing dual pot servo");
}
void loop()
{
newval1 = analogRead(potpin1);
newval1 = map(newval1, 0, 1023, 0, 179);
if (newval1 < (oldval1-2) || newval1 > (oldval1+2)){
myservo1.write(newval1);
Serial.print("1- ");
Serial.println(newval1);
oldval1=newval1;
}
newval2 = analogRead(potpin2);
newval2 = map(newval2, 0, 1023, 0, 179);
if (newval2 < (oldval2-2) || newval2 > (oldval2+2)){
myservo2.write(newval2);
Serial.print("2- ");
Serial.println(newval2);
oldval2=newval2;
}
delay(50);
}
Thanks guys,
I thought I was >= crazy || ! normal
-"analogRead tends to oscillate a bit as you can see from your debug data"- arg! NOOOO!
...I kinda figured that, one of the problems with high resolution AD conversion and low resolution pots i guess. I tried to stabilize it with capacitors but it didn't really help much.
"if (newval1 < (oldval1-2) || newval1 > (oldval1+2)) "
looks like what I need. I'll give it a try.