Offline
Newbie
Karma: 0
Posts: 27
|
 |
« on: January 13, 2012, 08:34:24 am » |
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) 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 
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #1 on: January 13, 2012, 08:46:57 am » |
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
Newbie
Karma: 0
Posts: 27
|
 |
« Reply #2 on: January 13, 2012, 08:52:01 am » |
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
Newbie
Karma: 0
Posts: 27
|
 |
« Reply #3 on: January 13, 2012, 09:02:18 am » |
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
God Member
Karma: 0
Posts: 596
Arduino rocks
|
 |
« Reply #4 on: January 13, 2012, 09:58:21 am » |
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 27
|
 |
« Reply #5 on: January 13, 2012, 10:15:12 am » |
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 ? 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
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #6 on: January 13, 2012, 10:27:46 am » |
Stepper motors[] = { stepper(80,5,6), stepper(75,9,10), }; will create an array of Stepper objects. and so on... let's say 56 motors so last is motor55 Which Arduino are you using that has 112+ pins?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
God Member
Karma: 0
Posts: 596
Arduino rocks
|
 |
« Reply #7 on: January 13, 2012, 10:37:33 am » |
Probably the SupaDupa dual processor Arduino 5120 :-P
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 27
|
 |
« Reply #8 on: January 13, 2012, 10:41:23 am » |
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!
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #9 on: January 13, 2012, 03:36:23 pm » |
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: motor[0].step(someNumberOfSteps);
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #10 on: March 11, 2012, 07:41:42 am » |
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
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #11 on: March 11, 2012, 07:51:27 am » |
Shouldn't those two "stepper"s be capitalized? i.e., Stepper(80,5,6), Stepper(75,9,10) Yes.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 11
|
 |
« Reply #12 on: March 11, 2012, 08:02:54 am » |
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
|
|
|
|
|
|