Need help with an idea

I’m working on a stepper motor project and I need to orient the stepper when it is powered on.

I’ve got the majority of my code working but each time I power up or re-upload new code my stepper is out of position. I need a way to orient the stepper to the same position at program start or power up.

Now since I’m working with mirrors and lasers with this project I know what I could do to orient the position; but not 100% sure how to accomplish it in the program.

Using a phototransitor I want to write a section of code that slowly rotates the stepper until the laser is focused on the phototransitor. I’ll work in a light shield of some sort so I am 100% sure I’m in the same position each time. Once it’s detected I will rotate the stepper to the position I need it to be in for my main program.

The problem I forsee is this should run once and not be needed again. Can I put something like this routine in a Setup() section? I’m way to new at writing code like this to be sure.

TIA

The purpose of the setup() function is to establish the environment in which the program operates. It might set the state of sensors, controller pins, data connections, window handles, etc. This is probably where your "targeting" function should go. The strange thing about setup(), however, is that you should never "call" it in your own code. It automatically gets called on power-on and is never called again unless there is a reset or it is re-powered. You can see how this is coded in main.cpp, which you can find on your IDE path:

  • hardware\arduino\avr\cores\arduino\main.cpp*

econjack:
The strange thing about setup(), however, is that you should never "call" it in your own code. It automatically gets called on power-on and is never called again unless there is a reset or it is re-powered.

There is not a reason in the world setup cannot, or even should not, be called by the main program, as long as it is not doing anything that would cause incorrect operation of that program. And that is entirely within the control of the programmer. There is nothing at all special or different about setup - it's just another function that happens to get called by init(), but in many cases could also be called from anywhere else within the program if it makes sense to do so.
Regards,
Ray L.

You could write a function called home(), where the motor is turned until a sensor of your choosing is triggered. You can call it from setup or from the loop.

it's just another function that happens to get called by init(),

You misspelled main(), which incidentally also calls init().

Hi,
Simply fit a limit switch at the position you need the stepper to start at.
Then when your code starts in setup.
Check to see if limit is activated.
If not execute a homing function to bring the stepper home.

It would not be a good idea on power up using your laser, as you have no idea where it is pointed.

If the limit switch will not give you the position accuracy you need, then use the limit switch to pull the stepper to a safe position that you know you can power up the laser, then do the mirror alignment method.

Tom... :slight_smile:

Thanks all. I had thought about a limit switch; and I may end up going that direction.
As for how I will accomplish the code once; and on startup I believe I've figured that out. Just using a variable I will initially set to zero; but once calibration is done set it to 1; and i'll perform that function only when the variable is zero.

Forgive my lack of probable 'incorrect' wording for variables/methods/etc. Still pretty new at this programming stuff. Haven't done this since high school using old basic! LOL

Also; I should have gotten back to update sooner; but for some reason I can't figure out how to see my posts or when replies are posted. And yes, I have 'notify me of replies' checked..

Thx All!

RayLivingston:
There is not a reason in the world setup cannot, or even should not, be called by the main program, as long as it is not doing anything that would cause incorrect operation of that program. And that is entirely within the control of the programmer. There is nothing at all special or different about setup - it's just another function that happens to get called by init(), but in many cases could also be called from anywhere else within the program if it makes sense to do so.
Regards,
Ray L.

I disagree. The purpose of setup() is to establish the operation environment for the program. If you stay with the intended use of setup(), there is no reason to call it a second time. If you want to play games with setup() and use it for something beyond, or instead of, what it's intended for, that's your prerogative. However, I do not think that calling it a second time, if it's already accomplished its intended purpose, is good coding practice. Indeed, I don't think I've ever seen someone explicitly call setup().

It's just a function that gets called from main. Just because it is named setup doesn't make it magically have some new coding practice rule that doesn't apply to any other function you might call at the beginning of main. I've written tons of code where the first function called from main gets called again somewhere.

Or is it just the name "setup" that you think imbues it with this new property of being intended for only one acceptable purpose?

Delta_G:
It's just a function that gets called from main. Just because it is named setup doesn't make it magically have some new coding practice rule that doesn't apply to any other function you might call at the beginning of main. I've written tons of code where the first function called from main gets called again somewhere.

Or is it just the name "setup" that you think imbues it with this new property of being intended for only one acceptable purpose?

Did you call a function that was in main() or main() itself? There's a difference. A function like ShowMenu() might be called in setup() and be called again many times in loop(). I get that. However, main() is the entry point for most programs operating in a C environment. Once the program has started, however, do you call main() again? In the context of the Arduino IDE environment, the developers of the IDE chose to hide main() and have setup() be the entry point.
You can take main.cpp and do anything you want to with it. You and Ray have been posting code here for at least 5 or 6 years that I've seen and, if you believe what you say and aren't just arguing for the sake of arguing, why have I never seen code from you that calls setup() multiple times in the same program?

econjack:
I disagree. The purpose of setup() is to establish the operation environment for the program. If you stay with the intended use of setup(), there is no reason to call it a second time. If you want to play games with setup() and use it for something beyond, or instead of, what it's intended for, that's your prerogative. However, I do not think that calling it a second time, if it's already accomplished its intended purpose, is good coding practice. Indeed, I don't think I've ever seen someone explicitly call setup().

There's a big difference between "I believe its bad form", which is what you're saying now, and "you should never call it", which is what you said earlier. Functionally, there is no reason you can't, or even shouldn't, do it. And I can imagine circumstances where it might be a reasonable thing to do, whether it fits your concept of good style or not.

Regards,
Ray L.

RayLivingston:
Functionally, there is no reason you can't, or even shouldn't, do it. And I can imagine circumstances where it might be a reasonable thing to do, whether it fits your concept of good style or not.

Regards,
Ray L.

I can't find the quote: "I believe its it's bad form" in any of my posts?
Given that I've never seen you do it, or anyone else...ever...suggests it's not just my concept of good style, but a whole bunch of other people as well.

ac5ff:
The problem I forsee is this should run once and not be needed again. Can I put something like this routine in a Setup() section? I'm way to new at writing code like this to be sure.

Yup, it belongs in setup().

Unless … unless at some point in the future you might want to supply your device with a "re-home" button.

I believe I may have mis represented what I wanted to do; and I apologize for creating a discussion that seems to be an issue with some conflicting ideas. I don't think I ever wanted to call Setup() from my main program. Matter of fact I never even connected Setup() as a function itself until reading these comments! I did learn a little! :slight_smile:

Let me try and clear things up a little bit. As I learn more about C++ programming and the arduino I may be able to better articulate my questions. :slight_smile:

Am I correct in assuming I can create a function ... we'll call it MirrorHome() ... that will run a set of commands every time it's called. I.E. I want to rotate a stepper CW until I have an input pin go high. Then I want to rotate the stepper CCW 150 steps. That in itself (to me at least) is a whole program. So, could that be a 'function'?

Secondly; lets say I don't want a function at all. But in the Setup() section I have that same bit of code to rotate the mirror/etc. Will that work?

My other thought was to set an int MirrorHome ==0 in the setup. Then in the loop() the first bit of code would be an If(MirrorHome==0); then position the mirror home, then set MirrorHome ==1 That way every time the code looped it would skip this section; but if the program restarts it would reset the mirror to home before beginning.

Hope that made sense.. :slight_smile:

That's exactly what a function is supposed to do: Some specific task. By checking the current mirror position, you can either call the function again if the position is not where you want it, or bypass the call using your if statement.

econjack:
Did you call a function that was in main() or main() itself?

No didn't call main itself. But I have had a function that was the first thing called from main and have had that function called again later. And that's exactly the case with setup. Setup is NOT the entry point if you want to be technical. init() gets called before setup does. Whether or not the programmer ever has to look at main doesn't change the fact that it is there and certainly doesn't move the entry point for the program. All C/C++ programs enter at main AFAIK.

Now I'm with you that if you catch yourself calling setup later in the program that you could probably write it better. But I wouldn't go so far as to say you should never under any circumstance do that. If you had something like this:

void setup(){
    someFunction();
}

void loop(){

///  some code

   someFunction();

///  some more code
}

then I would say it looks silly to have a function that does nothing other than call a second function. Here we would actually be just as well off to write someFunction into setup and just call setup. One less call on the stack at that point if nothing else.

To further illustrate, if I had this:

int main(void){

     someFirstFunction();

     // some code

     ////////  Need to do the same stuff in someFirstFunction again

    // some more code
}

Would you have me write a second function identical to someFirstFunction just to avoid calling my first function again? That would be totally wasteful wouldn't it?

ac5ff:
Am I correct in assuming I can create a function ... we'll call it MirrorHome() ... that will run a set of commands every time it's called. I.E. I want to rotate a stepper CW until I have an input pin go high. Then I want to rotate the stepper CCW 150 steps. That in itself (to me at least) is a whole program. So, could that be a 'function'?

Yes.

ac5ff:
Secondly; lets say I don't want a function at all. But in the Setup() section I have that same bit of code to rotate the mirror/etc. Will that work?

Yes.

ac5ff:
My other thought was to set an int MirrorHome ==0 in the setup. Then in the loop() the first bit of code would be an If(MirrorHome==0); then position the mirror home, then set MirrorHome ==1 That way every time the code looped it would skip this section; but if the program restarts it would reset the mirror to home before beginning.

Yes. This is the "state machine" approach. I would use something like

boolean needToHomeMirror = true;

void loop() {
  if(needToHomeMirror) {
    home_the_mirror();
  }

  // etc, etc
}

void home_the_mirror() {
  // do whatever you need to do to home the mirror

  needToHomeMirror = false; 
}

Thanks Paul!
I believe I have it solved now! :slight_smile: Need to test a bit of the code tonight - but I think it'll work!

Delta_G:
No didn't call main itself. But I have had a function that was the first thing called from main and have had that function called again later. And that's exactly the case with setup. Setup is NOT the entry point if you want to be technical. init() gets called before setup does.

then I would say it looks silly to have a function that does nothing other than call a second function. Here we would actually be just as well off to write someFunction into setup and just call setup. One less call on the stack at that point if nothing else.

Absolutely agree with you and that's one reason I mentioned main.cpp in my first post on this thread. I guess it would be more correct had I said that setup() is the IDE's "visible" entry point for the program. My guess is that most Arduino programmers probably don't use main() as the entry point.