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.
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?
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?
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?
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;
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
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.