I have got the code running to move a steeper with pot control and using another pot to control speed the problem I have is the stepper moves 2 rotations to one rotation of the pot it a 10k 1 turn pot, can anyone help to fix it so if I move the pot one turn the stepper moves the same.
i found a workaround but it makes it run a bit ruff.
/* MotorKnob - slightly modified
*
* A stepper motor follows the turns of a potentiometer
* (or other sensor) on analog input 0.
*/
#include <Stepper.h>
// the number of steps on your motor
#define STEPS 49
// 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);
//int analogPin = 0; // potentiometer wiper (middle terminal) connected to analog pin 0
int val = 0;
int stepCount = 0; // number of steps the motor has taken
// the previous reading from the analog input
int long previous = STEPS / 2;
void setup(){
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
void loop(){
// get the sensor value. This equation maps the
// analogRead range to the number of steps, so the
// motor nicely tracks a potentiometer's position
int long val = (long)analogRead(A0) * STEPS / 2048;
// move a number of steps equal to the change in the
// sensor reading
stepper.step(val - previous);
// read the input on analog pin 0:
int sensorval = analogRead(A0);
// read the sensor value:
int sensorReading = analogRead(1);
// map it to a range from 0 to 100:
int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// set the motor speed:
if (motorSpeed > 0)
stepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
stepper.step(STEPS/100);
// 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;
}
First, you didn't put your code within code tags - the # button above the editor screen. You can go back and change this as it makes it much easier to read code.
Second, does you code compile correctly and what does it do compared to what you want to do? Actually it can't compile because it has two setup() routines and two loop() routines.
i suspect you are starting in the middle of a complex project without understanding how to do each of the separate parts - how to read a potentiometer, how do make a stepper move, how to send information to an LCD screen etc.
Write a short sketch that will allow you to learn each of the parts separately and when you understand all the pieces you should find it easy to bring them together in a single sketch. It will also be easier to get help here if you are just working on small pieces.
You seem to have two sets of code in loop() trying to do the same thing. Try:
const int potPin = ... ; // insert correct analog pin number here
const int stepsPerRevolution = ... ; // insert correct value here
// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to
Stepper stepper(stepsPerRevolution, 8, 9, 10, 11);
// insert setup() code here as before
void loop(){
int val = map(analogRead(potPin), 0, 1023, 0, stepsPerRevolution );
// set step speed here if necessary, or set it in setup() instead
stepper.step(val - previous);
previous = val;
}
thanks for that.
I am still learning only been at this for two weeks now.
Robin2
I have done what you suggested to do I got it working with the different parts I wont to be in the code, as you can see I have put it together, it works with the problem of steeper just moving to far.
daniellyall:
I have done what you suggested to do I got it working with the different parts I wont to be in the code, as you can see I have put it together, it works with the problem of steeper just moving to far.
I'm sorry - (and I'm not trying to be difficult) - but I can't see because you haven't posted your revised code and you haven't described exactly what happens now and what you would like to happen.
(By the way the word "wont" (more correctly "won't") is short for "will not" and obviously has a quite different meaning to "want" which is similar to "wish". In this case using the wrong word is confusing).
the code I am running now is posted as it say the stepper is moving two rotations to one turn of the pot. I would like one turn of pot to = one turn of stepper. i.e. one turn of pot, one full rotation of the stepper.
at this stage everything just needs to be simple so I can get on with making the parts of the test rig to see if I can do what I require.
all I wont is a code that will control the movement of a stepper motor with a pot, I need the movement of the pot to be the same as the movement of the stepper. and I would also like to control the speed of the stepper with a pot, it just need to be simple for now.
if it works out I will be using something else to control the movement of the stepper not sure what yet.
this project is for me to make life easer for me as being disabled sucks.
daniellyall:
the code I am running now is posted as it say the stepper is moving two rotations to one turn of the pot. I would like one turn of pot to = one turn of stepper. i.e. one turn of pot, one full rotation of the stepper.
Sorry, but where have you posted it? I can't see it on my screen.
I have changed the file to not include a pot for speed control as don't really need it,the steppers will only move at the speed the pot for position is moving at just need to set maxim speed.
this is it
[code]/* MotorKnob - slightly modified
*
* A stepper motor follows the turns of a potentiometer
* (or other sensor) on analog input 0.
*/
#include <Stepper.h>
// the number of steps on your motor
#define STEPS 49
// 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);
int val = 0;
int stepCount = 0; // number of steps the motor has taken
// the previous reading from the analog input
long previous = STEPS / 2;
void setup()
{
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// set the speed of the motor to 30 RPMs
stepper.setSpeed(60);
}
void loop()
{
// get the sensor value. This equation maps the
// analogRead range to the number of steps, so the
// motor nicely tracks a potentiometer's position
long val = (long)analogRead(0) * STEPS / 1024;
// move a number of steps equal to the change in the
// sensor reading
stepper.step(val - previous);
// read the input on analog pin 0:
int sensorval = analogRead(A0);
// print out the value you read:
Serial.print(sensorval/2.84 );
Serial.print("steps: ");
delay(100); // delay in between reads for stability
Serial.println(sensorval/20.876 );
Serial.print("angle: ");
delay(100); // delay in between reads for stability
// remember the previous value of the sensor
previous = val;
}
[/code]
also I have decide to go with servos not stepper as the size stepper I would use is a bit big and heavy, using a servo makes more senses