Encoder, Recording the position, speed and direction over time<-----possible?

Hi all I have been a non Arduino using, forum 'lurker' for a while and now i ordered one and im on my way to excting proects.

I need some help, im not a programmer, nor am i a electronics engineer, so excuse my inexperience, i am learning so be gentle. I would like to control a stepper motor with a rotory encoder, this bit i have worked out my self, its absolute position control but the problem is i need to record the motion of the encoder to then play back the motion while i move and record the next axis and so forth for 3 or 4 axis.

is this possible maybe saving to the sd card at about 60 or 120hz? can i record 2 axis simultaneously?

where do i start?

i have so many questions. :fearful:

thanks simon

How many distinct position/time values do you expect to record in total?

Why do you need an encoder in a stepper motor? High load application? You can probably get away with a proximity switch and a flag. Or a fixed position calibration point.

hi peter.

let me give you an example of use so you can get a clearer perspective on what im trying to achieve.

the time sequences to record range from 1 - 5 minutes

during that time i wish to manually control stepper motors and "teach" the arduino, in real time, in actual 1:1 time, where those stepper should be what speed they are going ETC... here is a graph of what i mean http://www.flickr.com/photos/74137762@N04/6681603765/

make sense?

thanks

the key x,y,z is for the motors.

im not looking for 1/100000 sec intervals of resolution maybe max 120 times a second

That all sounds very feasible. The quantity of data you're dealing with is far too much to be stored in EEPROM so you'll need to add an SD card as you suggest. It all looks completely feasible to me.

i wouldnt know where to start! lol

any help would be appreciated.

thanks simon

Take small steps. Start out by transferring the data to the serial port. That way you can examine it and make sure you are coming up with the right information. Then add a shield with an SD slot on it and learn to talk to the SD card. then start saving data to the SD card.

would there be any chance of some help to get me started. i would appreciate any assistance.

thanks simon

srosefx: i would appreciate any assistance.

Here is my assistance:

Start by doing something simple. Run the standard examples. Get used to the process of checking and uploading them. Look at the code and try to figure out how they work. Pick a very small function related to your overall target project, and try to implement it. For example, write some messages to the serial port and watch them appear in the IDE. Figure out how to connect one of your input devices to the Arduino, modify your sketch to initialise the pins and read values from your device and spit the results out to the serial port. And so on, gradually taking on more ambitious functions until you are ready to tackle the whole thing.

What I suggest you [u]don't[/u] do is look at the entire project, be so intimidated you don't know where to start, and come here and say you've got nowhere and don't know where to start. Show that you're making an effort to do this yourself. And if you get stuck on something specific, there are loads of helpful people here who I'm sure will help you get moving again.

hi

well this is the 3rd time i have written an update, and my mouse will not freak out again and close the browser after 30 mins of typing!!
so im going to make this short.
(im a slow typer!!)
okay, here is my code;

#include <AFMotor.h>
#include <AccelStepper.h>

AF_Stepper motor1(200, 1);
int analogPin = 0; // potentiometer wiper (middle terminal)
//connected to analog pin 3
// outside leads to ground and +5V
int val = 0; // variable to store the value read
int prepos = 0;
int pos = 1023; //0 to 200 .
void setup()
{
motor1.setSpeed(100); // 30 rpm
Serial.begin(9600);
}

void loop() {

prepos = pos;
val = analogRead(analogPin); // read the input pin

pos = 0;
for (int ctr = 0; ctr < 100; ctr++) {
val = analogRead(analogPin); // read the input pin
pos = pos + map (val, 0, 1023, 0, 2000);
}
pos = pos / 100;

pos = map (val, 0, 1023, 0, 2000);

if (pos != prepos){
int diststep = pos - prepos;
if (diststep < 0) {
motor1.step(-diststep, BACKWARD, INTERLEAVE);
}
if (diststep > 0) {
motor1.step(diststep, FORWARD, INTERLEAVE);
}
int sensorvalue = analogRead(A0);
Serial.println(sensorvalue);
delay(10);

}
Serial.flush();

}

this is what i have come up with, with the generous help and guidance of others.
im quite impressed and jazzed, learning this alien language was so hard, and its only hard if it puts you off, stick with it and it rewards!!

my next level of the programme is hard.
i have searched all over for example code to help me get started with writing and storing data then recalling and implementing that data to an analog device.
i thought i would start slow, and small.
i thought that if i had a recall button, just a switch that when pressed it lights up an led over another switch confirming its saved an encoder positon.
then i move the motor and i press the recall button again, then the second led lights up over another switch etc ect…
maybe 3 of these to start.

then when i press any one of those 3 buttons it moves the motor to that position.

i have an sd card im learning how to push data to this card but im alittle stumped at the moment.
any help would be muchly appreciated.

int pos = 1023; //0 to 200 .

Useful comment. Too bad it’s wrong.

  val = analogRead(analogPin);    // read the input pin

 pos = 0;
  for (int ctr = 0; ctr < 100; ctr++) {
    val = analogRead(analogPin);    // read the input pin
    pos = pos + map (val, 0, 1023, 0, 2000);
  }
  pos = pos / 100;

The the analog pin, and throw the value away. Why?

Then, read the value of the pin in a loop. Map each value read, and add up the total. Then divide the total by the number of readings.

Let’s imagine that the value read from the pin was in the middle of the range, so about 512. This will be mapped to about 1000. Summing up 100 values of 1000 will give a total around 100000. That will not fit in an int. So, you have the wrong type for pos.

  pos = map (val, 0, 1023, 0, 2000);

Then, throw away the 100 readings, using just the last one. Why?

  int sensorvalue = analogRead(A0);
  Serial.println(sensorvalue);

Then, read another value from the same pin, using a different name, and print that value. Why?

  Serial.flush();

Why? If you are using 0023 or earlier, this deletes any incoming serial data that has not been read. Since you aren’t reading serial data, anyway, why do you care whether there is room in the serial buffer, or not?

If you are using 1.0+, this blocks until all serial data has been sent. Why is that necessary?

i have an sd card im learning how to push data to this card but im alittle stumped at the moment.
any help would be muchly appreciated.

Do you hope to become thoroughly stumped? If that is not the case, you have to give us just a tad more to go on. What part of writing to an SD card are you having problems with?