Stuck - Please Help

Okay - I'm a complete nOOb with programming - at least since high school some 20+yrs ago! HA!
For whatever reason what I want to do sounds simple but I just cannot wrap my brain around it and haven't been able to get it to work.

I'm running a stepper motor CCW until it trips a switch (pin8). Once at that position I want to rotate CW 10 steps. And this needs to happen once at the start of the program. Once that is done it will not need to be done again unless the program restarts. Although I will probably write in a resetbutton switch that will force the program to re-run that section of code.

Simple; I think it is.. But why can I not seem to understand getting it to work?

Anyone have any ideas? Pointers? Sample code?!? LOL

thanks!

HI,
Post the code you have and explain what its doing wrong.

What stepper motor do you have?
What stepper motor driver do you have?
What model Arduino do you have?

Tom… :slight_smile:

ac5ff:
Okay - I'm a complete nOOb with programming - at least since high school some 20+yrs ago! HA!
For whatever reason what I want to do sounds simple but I just cannot wrap my brain around it and haven't been able to get it to work.

I'm running a stepper motor CCW until it trips a switch (pin8). Once at that position I want to rotate CW 10 steps. And this needs to happen once at the start of the program. Once that is done it will not need to be done again unless the program restarts. Although I will probably write in a resetbutton switch that will force the program to re-run that section of code.

Simple; I think it is.. But why can I not seem to understand getting it to work?

Anyone have any ideas? Pointers? Sample code?!? LOL

thanks!

uint8_t motorHome; // C clears the vars to zero each time program starts

if (motorHome == 0) { // if var is zero then motor was not zeroed yet
    // rotate motor to limit switch
    motorHome = 1; // flag "motor is homed"
}

See what happens here?

When your sketch first starts (or you press reset and restart the sketch), C startup clears all vars to zero.

Therefore, if "motorHome" is zero, you know the stepper has not yet been "homed" in this reboot.

After you home / calibrate the stepper, you set "motorHome" to 1 and it stays that way until you restart the Arduino.

So, by checking if "motorHome" is 0 or 1, you know if your stepper is calibrated already or if you need to do it now.

Hope this helps.

const byte HomeSwitch = 8;
void setup() {
  .
  .
  .
  // running a stepper motor CCW until it trips a switch (pin8). 
  while (digitalRead(HomeSwitch)) {
    // Switch open, so step
    step_one_step_CCW();
  }
  // Once at that position I want to rotate CW 10 steps.
  for (int i=0 ; i < 10; i++) {
    step_one_stap_CW();
  }
}

Typically the homing should start with moving AWAY from home if the home switch is already pressed:

  while (!digitalRead(HomeSwitch)) {
    // Switch closed, so step away
    step_one_step_CW();
  }

It was late last night when I posted this and I was tired and frustrated. LOL I should have added more details. Sorry!

Below is a look at what I’ve currently written and trying. What I found was I was putting for loops within for loops which wouldn’t compile. So I thought I would do part as a function. Didn’t work! LOL
What is this code doing? I’m not sure. The remaining part of the program basically moves the stepper back and forth 10 positions. Before adding all of this; that part of the code works great.

When I add this the stepper moves but is not smooth; it jerks backwards a few steps then forward a couple times, and slowly moves the center of the rotation CCW… Not sure how else to explain that.

Tom:
I’ve got a Nema17 Stepper using the stepper.h library and Arduino Uno

krupski:
Not sure I understand this line:
uint8_t motorHome; // C clears the vars to zero each time program starts
I’ll have to read up on that one. But what you are describing is pretty much what I’m trying to do.

John:
I understand what you are saying about moving away from the home position to start. That’s something I’d considered but haven’t implemented yet as I’ve always pre-positioned the stepper motor myself before starting the program. I’d agree that this should be in the working program but at this point I would just like to get this section working to where I need it to be…

void MirrorCenter()        // Function to rotate CCW until switch is triggered
  {
  MirrorPinRead = digitalRead(MirrorPin)
  while ((MirrorPositioned = 0) && (MirrorPinRead = LOW));
      myStepper.step(-1);
      delay(200);
  }
    

void loop()  {

  MirrorCenter();
  
  if (MirrorPositioned = 0) {       // run this section ONCE ONLY; ignore it on every loop except at startup
      for (int x=0; x<10; x++);
        myStepper.step(1);
  }
  MirrorPositioned = 1;
}

Quick question on my code above:

Will this while statement:
MirrorPinRead = digitalRead(MirrorPin)
while ((MirrorPositioned = 0) && (MirrorPinRead = LOW));
MirrorPinRead = digitalRead(MirrorPin);
myStepper.step(-1);
delay(200);

loop over and over until the MirrorPinRead = HIGH?

Or, will it only run once though move stepper 1 position then leave the while loop; run the rest of the program, and loop back to the beginning and re-run this function because both variables in the while statement are still true?

If I had to guess; it would be the 2nd option.... based on how the stepper appears to be operating...

while ((MirrorPositioned = 0) && (MirrorPinRead = LOW));Because of the semi colon at the end this single line is a complete while loop. Neither of the variables in the while condition are tested in the while loop so it will loop forever once it is entered.

All your conditional tests in the if() and while() blocks need == which is for comparison instead of = which is for assignement.

Was looking through code just a few minutes ago; I noticed the ";" on the while statement. That's going to be change to bracket the statemetns I want to run "{ ... }"

and thank you for the explination on = and == ... I was not aware of the difference. (read nOOb lol)

(read nOOb lol)

Yes n00b, go read. At least hit the reference section of this site (under learning) and read that stuff. A basic C++ tutorial (try googling) would also be of great benefit to you.

That's going to be change to bracket the statemetns I want to run "{ ... }"

I think that it is a good idea to always use curly brackets around the code blocks following conditional statements even if the code to be executed is only one statement. Personally I also always put each curly bracket on its own line and indent the code between them.

Delta_G:
Yes n00b, go read. At least hit the reference section of this site (under learning) and read that stuff. A basic C++ tutorial (try googling) would also be of great benefit to you.

As well, the example sketches that ship with the IDE.