<While> inside Setup or loop?

Hi,

I'm strugeling almost 4 hours to get a code running for initial setup of my actuator.
I can get the "my work" code to do what it have to do.

But now im trying to ad a code so if arduino gets a reset (power loss) it wil get the actuator shaft back to "home" switch(in my code "crashSwitch2")

i don't know exactly how to do it so i tried putting while loop in de setup but it didn't loop.
Finally i got it to work inside void loop. like this:

#include <AccelStepper.h>

// VREF voltage = 0,65 mag naar 0.8


// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::DRIVER, 9,8); // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
int MODE0 = 13;
int MODE1 = 12;
int MODE2 = 11;

byte directionPin = 8;
byte stepPin = 9;
//int numberOfSteps = 10000;
//int millisbetweenSteps = 1;

//Actuator crashpin 1
const int crashPin1 = 2;
int crashPin1State = 0;


//Actuator crashpin 2 - position
const int crashPin2 = 3;
int crashPin2State =1; //1 = open->niet ingedrukt


////Void setup is not interesting (i think)\\\\


void loop()
{

while(1) {
         crashPin2State = digitalRead(crashPin2);
         Serial.print(crashPin2State);
      stepper.setAcceleration(1000);
      stepper.move(-2000);
      stepper.run();
      if( digitalRead(crashPin2State) != 1) break;
      
}}

But when i insert my "work" into void loop too, the while statement doesn't get executed.
It should move the stepper backword until it hits the crashSwitch2 then exit the loop and wait for crashSwitch1 to be pusht to run the "work" code.

This is the finale code only void loop changed.

void loop()
{

while(1) {
         crashPin2State = digitalRead(crashPin2);
         Serial.print(crashPin2State);
      stepper.setAcceleration(1000);
      stepper.move(-2000);
      stepper.run();
      if( digitalRead(crashPin2State) != 1) break;
      
}
      stepper.setCurrentPosition(0);
      crashPin1State = digitalRead(crashPin1); 
       
    if (crashPin1State == LOW)
   {
          digitalWrite(enable,LOW);
          stepper.setCurrentPosition(0);
          delay(300);
          stepper.setAcceleration(20000.0);
          stepper.runToNewPosition(4900);
          delay(1000);
          stepper.setAcceleration(100000.0);
          stepper.moveTo(9800);
          stepper.runToPosition();
          stepper.runToNewPosition(0);
    }
    else 
{   
    (digitalWrite(enable,HIGH));

}
}

Hope some can explain it to me!

tnx

while(1) {
         crashPin2State = digitalRead(crashPin2);
         Serial.print(crashPin2State);
      stepper.setAcceleration(1000);
      stepper.move(-2000);
      stepper.run();
      if( digitalRead(crashPin2State) != 1) break;
     
}

This is an infinite loop - nothing after it can ever be executed.

OH, sorry - missed the break.

You appear to not understand that loop() is already an infinite loop. Having nothing but an infinite loop inside loop() is pointless.

You could have a while(digitalRead(somePin) == whatever not pressed means) loop in loop().

but with the finale code only the while loop doesn't get excecuted.
The "work" code below while does work?
What am i missing?

PaulS:
You appear to not understand that loop() is already an infinite loop. Having nothing but an infinite loop inside loop() is pointless.

You could have a while(digitalRead(somePin) == whatever not pressed means) loop in loop().

Is that a appropriate way to do an initial setup after Arduino turns ON?
so "while(digitalRead(somePin) == whatever not pressed means) loop" in the void loop or void setup?

The only difference is that setup() runs once, and loop() repeats.
Leo..

I know that a setup runs ones and the loop infinate.

I'm trying to get the while code to run once.
Because i wil use the crashswitch2 in the "work" code to check if it is at his home position to drive something else. otherwise the 2 codes wil get in each others way.

The question is can i get a to run in the setup to initiate position of the actuator and get it to "home" if it is't there? or should i do it in the loop?

tnx for helping me!

Need help loading helloworld for oled test it keeps erroring

Kumalix:
But the question is can i get a to run in the setup to initiate position of the actuator and get it to "home" if it is't there? or should i do it in the loop?

Up to you. Any code can go in setup() and/or loop().
If you want to home an actuator once, you could do that in setup().
If you want to do it often, move it to loop().
Or do it in setup() and loop().
Leo..

Leo thank for your help.
I would like it to run in de setup.
But have trouble getting out of the loop?

PaulS:
You appear to not understand that loop() is already an infinite loop. Having nothing but an infinite loop inside loop() is pointless.

You could have a while(digitalRead(somePin) == whatever not pressed means) loop in loop().

i now got this. every time i push the switch on the serial monitor i see eighter 1 or 0 never "out of -while- loop". thus this meen that it never get's out of the loop?

void loop()
{

while(digitalRead(crashPin2State) == 1 ) {
  
         crashPin2State = digitalRead(crashPin2);
         Serial.print(crashPin2State);
         Serial.print("\t");
      stepper.setAcceleration(1000);
      stepper.move(-2000);
      stepper.run();
      if( digitalRead(crashPin2State) == 0) 
     {
      
      break;
      Serial.println("out of -while- loop");
     }
}

Make your "return to home" code a function, then call it from setup() at startup and you could call it any time it's needed with a pushbutton.

outsider:
Make your "return to home" code a function, then call it from setup() at startup and you could call it any time it's needed with a pushbutton.

I could make a function and i tried but get a lot of errors.
I think it should work with in the setup.

I now moved the "return to home" code to the void setup.
There i can see that it stays in the while loop and when i press the switch it gets out.
So far so good. Only issue is now that when it is in the while loop the stepper turns very slowly and not listening to the commands at all:
(stepper.setAcceleration(3000);
stepper.moveTo(-2000);
stepper.setSpeed(1000);.

It's turining the wrong way too.

Is this because accelstepper commands can't work in the setup? or?

in the Void setup:

crashPin2State = digitalRead(crashPin2);

      while(crashPin2State == 1  ) {
      stepper.run();
      crashPin2State = digitalRead(crashPin2);
      Serial.print(crashPin2State);
      stepper.setAcceleration(3000); 
      stepper.moveTo(-2000);
      stepper.setSpeed(1000);
      Serial.println("in the move -while- loop");
      if( crashPin2State == 0) break;

Is there someone who can expain why it hapens?

tnx

Kumalix:
Is there someone who can expain why it hapens?

tnx

Can you post your code and some precise observations?

Anshu_Raj:
Yup! Yup! The arduino is doing it's work.
The while loop in your original code is not supposed to work as it's wrong (Well short of)

Now, it's obvious that the Arduino won't run this loop as while loop has a boolean part. But, your's loop only has an integer "1" And Arduino is like "1?? What to do with that? Huh? Maybe I just skip it HA!!"

The while HAS a boolean part - you're spouting rubbish, and demonstrating your ignorance. Please stop

while(1)

In case you need me to narrow it down, the boolean part is the '1'.

In C/C++ ANY non-zero value evaluates to true.

This is nothing new, it's been around since K&R First Edition.

Anshu_Raj:
I went a little too far to keep my point.
Here maybe this will help you:

I tried to run this code on NetBeans:

package main;

public class Main{
   
    public static void main(String[] args) {
        while(1){
            System.out.println("Hello world!");
        }
    }
}




And got this error:


Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types
  required: boolean
  found:    int
at main.Main.main(Main.java:10)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

And now you're compounding the demonstration of your ignorance Java != C/C++

Anshu_Raj:
So, yeah! I'm right at my point and maybe you are at your point.

So, no!
I am right at my point, and in the context of this forum, your point is irrelevant, and a non sequitur.

Guys, can you help with my issue? :-[

So i have MOVED the code to the void SETUP.
And there its perfectly working (without boolean).
Only problem is, like i meantioned in my previous post, the stepper motor is not liscening to the commands? it does move but step by step en in the wrong direction.

Why is that?

CtrlAltElite:
Can you post your code and some precise observations?

As soon as i get home today i will post the whole code.

i now only have this part that works but with the issue described in post above:

in the Void setup:

crashPin2State = digitalRead(crashPin2);

      while(crashPin2State == 1  ) {
      stepper.run();
      crashPin2State = digitalRead(crashPin2);
      Serial.print(crashPin2State);
      stepper.setAcceleration(3000); 
      stepper.moveTo(-2000);
      stepper.setSpeed(1000);
      Serial.println("in the move -while- loop");
      if( crashPin2State == 0) break;

tnx