Go Down

Topic: incrementing ? (Read 1 time) previous topic - next topic

tomperdarwin

Hi

My laptop decided to go to ground on me, taking most of my sweat and tears of recent weeks with it. (as all computers are inherently evil, they always die at the moment you think "i really should make a new backup next weekend")

But I digress before I made my point.

I was working on code to drive multiple stepping motors (which I'll have to write entirely again, most likely), managing to keep the code clean by putting most variables in arrays where position 0 = motor one, pos1 = motor two and so on...

But this still left me with mutiple instances of

(and I type it knowing full well this isn't even remotly realistic like this)

Code: [Select]

for (x=0; x<5, x++){
  if timearray(x) = time to step { // nevermind how the time is decided, that works
    if (x==0) motor1.step();  //number of steps comes from another array , that isnt a problem either
    if (x==1) motor2.step();
    ...
  }
}



The question hence  = How can I "Array" (or something like it ?  some form of enum ?) the terms 'motorX' so there don't need to be 5 instances of nearly the same IF ?


I do hope this makes sense :s  If it doesn't I'm sure some of you will beat me into humility again :D

PaulS

You can create an array of Motor objects. Exactly how depends on the class (specifically, the constructor arguments). Post more of your code so we can help you.

tomperdarwin

well like I said, the code is gone, I need to write it again.  which isn't bad, since i learn all the time ( I think) so i can make it "cleaner" in teh second writing


it is teh basic stepper library from the IDE

stepper Motor1 = stepper(x,y,z) where x = steps per 360°, y, z = output pins.


tomperdarwin

Now that you mention it, since the "stepper" library is blocking...  maybe it's better to drive them one step each iteration of the loop (taking a reasonable delay to actually allow the motor to keep up) instead of using the library to step them X steps when it's time...


( but that's another thing entirely ... unless I can figure out how the digitalwrite High/low works when step() is called ... 

I'm thinking out loud... sorry )

mromani


tomperdarwin

Yes.  I have. 

But I can't find the bit where it explains on how to getting multiple motors named in a way that allows me to use their different functions through a variable ?
Code: [Select]


stepper motor0 = stepper(80,5,6);
stepper motor1 = stepper(75,9,10);
and so on... let's say 56 motors so last is motor55


for x<55, x++
motor(x).setspeed( arrayspeed[x]  )
motor(x).step( arrayStepSize[x] )

for now it's more of a theorethical question.. while I brood on the price of laptops these days

PaulS

Stepper motors[] =
{
   stepper(80,5,6),
   stepper(75,9,10),
};
will create an array of Stepper objects.

Quote
and so on... let's say 56 motors so last is motor55

Which Arduino are you using that has 112+ pins?

mromani

Probably the SupaDupa dual processor Arduino 5120 :-P

tomperdarwin

like I said... theorethical ;)


It's THAT simple ?  ok i feel like an idiot now (as well as look like one I guess),..

I would never have expected that to work when adding the ".setspeed" or ".step" part...  to the point of not bothering to test it...

thanks!


PaulS

Quote
I would never have expected that to work when adding the ".setspeed" or ".step" part...  to the point of not bothering to test it...

You need to use the correct notation, to use the methods of an object in an array. To move the first motor:
Code: [Select]
motor[0].step(someNumberOfSteps);

8bar


Stepper motors[] =
{
  stepper(80,5,6),
  stepper(75,9,10),
};
will create an array of Stepper objects.


Shouldn't those two "stepper"s be capitalized? i.e., Stepper(80,5,6), Stepper(75,9,10)

PaulS

Quote
Shouldn't those two "stepper"s be capitalized? i.e., Stepper(80,5,6), Stepper(75,9,10)

Yes.

8bar

OK, thanks for clarification. It took me forever to find this solution, so to help others find it, I'll post:

initialize array of Stepper objects
initialize array of Stepper instances
make array of Stepper objects
make array of Stepper instances

as search phrases that will hopefully help others find this!

Stepper motors[] =
{
   Stepper(80,5,6),
   Stepper(75,9,10)
};

Go Up