Pages: [1]   Go Down
Author Topic: need some help with project  (Read 975 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

could some please help with this project 

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.

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

« Last Edit: December 04, 2013, 04:56:05 pm by daniellyall » Logged

UK
Offline Offline
Faraday Member
**
Karma: 46
Posts: 3483
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

...R
Logged

UK
Offline Offline
Faraday Member
**
Karma: 46
Posts: 3483
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok will do that

The only evidence I can see is that you have done NONE of the things I suggested or asked.

...R
Logged

UK
Offline Offline
Faraday Member
**
Karma: 46
Posts: 3483
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you some principled objection to putting your code between code tags so that it is easier for others to read?

I think it's three times now that you have failed to do so - it's not too late to go back and tidy up your earlier posts.

...R
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

help please
Logged

UK
Offline Offline
Faraday Member
**
Karma: 46
Posts: 3483
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, you have used code tags - the first of the items in my Reply #1.

If you can answer or comment on the other three points I made we may be able to make some progress.

...R
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 220
Posts: 6587
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You seem to have two sets of code in loop() trying to do the same thing. Try:

Code:
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;
}

Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 46
Posts: 3483
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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).

...R
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 46
Posts: 3483
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

...R
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
[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
Logged

UK
Offline Offline
Faraday Member
**
Karma: 46
Posts: 3483
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I guess everything is OK now?

...R
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes  for now thanks for help
Logged

Pages: [1]   Go Up
Jump to: