How to dermine the relative move of a stepper?

Hi to all... I got this drink roulette thing going on... and the code is almost complete, but I cant move the servo to a relative position...

I got this mini sketch doing a set of numbers and a set of mapped values.

``````#include <Stepper.h>

const byte noise = A0;
const byte led = 13;

#define count 12
int  analog_seed;
int  random_number = 0;
int  motor_number = 0;
byte getNumber = 0;

int list[count] = {1,6,9,8,2,11,0,4,3,7,10,5};

void  setup(){
Serial.begin(9600);

for(int k; k<12; k++){
Serial.print("Random number is    "); Serial.println(list[getNumber]);
motor_number = map(list[k], 0,13, 0,49);
Serial.print("Steps for the motor "); Serial.println(motor_number);
Serial.println();
getNumber++;

delay(50);
}

}

void loop(){

}
``````

And this is the output;

``````Random number is    1
Steps for the motor 3

Random number is    6
Steps for the motor 22

Random number is    9
Steps for the motor 33

Random number is    8
Steps for the motor 30

Random number is    2
Steps for the motor 7

Random number is    11
Steps for the motor 41

Random number is    0
Steps for the motor 0

Random number is    4
Steps for the motor 15

Random number is    3
Steps for the motor 11

Random number is    7
Steps for the motor 26

Random number is    10
Steps for the motor 37

Random number is    5
Steps for the motor 18
``````

How to make for example the stepper goes to the rigth position, instead of starting to count 26, or 10 steps from the current position.

Like a clok, each time 5 goes out the dial go to the 5 position, the random is given by the order of the numbers.

-Alex.

Are you trying to move a stepper or a servo? The title says one thing. The post says the other.

but I cant move the servo to a relative position...

Relative to what?

How to make for example the stepper goes to the rigth position, instead of starting to count 26, or 10 steps from the current position.

You have to know/remember where you are. Going somewhere else, then, is a matter of so many steps per increment times the number of increments.

PaulS:
Are you trying to move a stepper or a servo? The title says one thing. The post says the other.

Yes is a stepper motor, Im sorry.

Relative, ahmm Im trying to divide the steps of the motor lets say 48 steps /12 number = 4steps per number.

The idea is even when the order of the numbers are random... they always turn to the same position(like the dial in a clock but in random order)

Since the order of the numbers is random... I cant figure out how to make the stepper goes from 2 to 6 per example

2 = 8 steps // if the start position is 0 we move 8 steps to here 6 = 24 steps

16 steps difference

So I guess If Im in the number two and want to move to the number 6 I have to move 16 steps instead of 24.

But Will be sufficient with the difference between the past and the current steps or I need a reference?

Like

Abs_position = 0;
Number 2 is 8 steps from 0
number 6 is 24 stepes from 0
??
Im litte confused here.

So I guess If Im in the number two and want to move to the number 6 I have to move 16 steps instead of 24.

Yes.

Im litte confused here.

Write some code that you think will move the device from 2 to 6. See what it actually does. Then, make it move from 6 to 12. Does that work?

Hi @PaulS

I follow you advice, I got this thing working!

I got this peace of code here;

``````#include <Stepper.h>

const byte noise = A0;
const byte led = 13;

#define count 12
int  analog_seed;
int  random_number = 0;
int  motor_number = 0;
byte getNumber = 0;
int  abs_position = 0;

int  relative_position = 0;
int  actual_position = 0;
int  number_steps = 0;                        //VAR TO SAVE THE NUMBER EQUIVALENT IN STEPS
int  list[count] = {9,7,8,6,1,0,2,5,4,11,3,10};

const int stepsPerRevolution = 48;                 //steps per revolution
Stepper myStepper(stepsPerRevolution, 8,9,10,11);  //Pins to the stepper

void  setup(){
Serial.begin(9600);
myStepper.setSpeed(50);

for(int k; k<12; k++){
Serial.print("Random number is    ");

Serial.print("Relative position is "); Serial.println(relative_position);
Serial.print("Actual position      "); Serial.println(actual_position);
Serial.println();

delay(1000);
}

}

void loop(){
if(Serial.available() > 0){
if(Serial.peek() == 'c'){
if(getNumber <= 12){
getNumber++;
number_steps = map(list[getNumber], 0,12, 0,48);
Serial.println(list[getNumber]);
relative_position = number_steps - actual_position ;
actual_position   = number_steps;

myStepper.step(relative_position);
}

else getNumber = 0;
}
Serial.read();                             //wipe the c byte from the serial port
}
}
``````

Tested and works properly, I will mention on the finish post … thank you!
-Alex.