Pages: [1]   Go Down
Author Topic: incrementing ?  (Read 802 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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 smiley-grin
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46209
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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.

Logged

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

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

0
Offline Offline
God Member
*****
Karma: 2
Posts: 596
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd have a look at

http://arduino.cc/en/Reference/Stepper

:-)
Logged

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

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:

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
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46209
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
God Member
*****
Karma: 2
Posts: 596
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Probably the SupaDupa dual processor Arduino 5120 :-P
Logged

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

like I said... theorethical smiley-wink


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!

Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46209
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
motor[0].step(someNumberOfSteps);
Logged

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

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

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46209
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

Pages: [1]   Go Up
Jump to: