1-minute beginner question

Hi guys,
I'm new in this Forum because i stuck with my Project:
I want to get a motorposition with an LDR
code is:

void setup(){
stepper.setSpeed(5);
stepper.setAcceleration(50);
while (analogRead(1) < 600)
{stepper.runToNewPosition(1);}
}

code is in "void Setup" because i Need this function only one time per start.
I want to turn the Motor an stop it, if the LDR shows values more then 600.
Til now it turns 1 step and stop. While i put the LDR to 600, the next code in "Void Loop" starts up.

whats wrong with my "while"
would be great to have an answer.

Thanks
Thofi2014

You need to post the whole code, as well as a pic of how the project is wired. Difficult to comment otherwise.

Also, when you post code, select it and hit the # button above the :wink: and :sweat_smile: smilies, so the code look...

like this

Hi jimboZA,
thanks for your fast reply and sorry for the "bad" post :wink:

this is the complete code:

#include <AccelStepper.h>
AccelStepper stepper (AccelStepper::HALF4WIRE, 8, 10, 9, 11);

void setup(){  

  stepper.setSpeed(5); 
  stepper.setAcceleration(50);
  while (analogRead(1) < 600)
  {stepper.runToNewPosition(1);}
  }

void loop() {     
  
  stepper.setMaxSpeed(50);  
  stepper.runToNewPosition(10);    
  stepper.runToNewPosition(-10);    

}

in the loop scetion there is only a placeholdercode to see, if the script is running.

Sounds to me the while is doing exactly what it should: it sits inside the while until the ldr reads over 600, then it exits the while. Only thing it can do next, is move into the loop().

I'm not sure what you actually want it to do if not that?

the intention was to let the stepper steps further until the LDR gets over 600.
the accelstepper library has no code to drive the stepper free without new position.
i need a function where the LDR is monitored and if the LDR goes over 600. the stepper ist stopping.

I've never used that library... presumably, {stepper.runToNewPosition(1);} moves it TO 1, not BY 1?

If so, and you want it to keep moving, you'll have to have a variable which increments each time it goes through the loop. Something like this, off the top of my head....

void setup(){  
   int poscounter = 1; //<<<<<<<<<<<< new
  stepper.setSpeed(5); 
  stepper.setAcceleration(50);
  while (analogRead(1) < 600)
  {stepper.runToNewPosition(poscounter); //<<<<<<<<<<<, changed
    poscounter = poscounter + 1;  //<<<<<<<<<, new
} 
  }

Hi JimboZA,

thanks for your idea, i will try it. May be you are right with the "TO-BY"-thing. check it too and post the resume.
Thankyou till now and greetings from germany.

Thofi2014

it works!!!! :stuck_out_tongue_closed_eyes:

thankyou, i will read some tutorials and maybe trying an easyer project..:slight_smile:

Bye, Thofi2014

Glad to help