Start loop based on analog input

Hello, and I apologize is this a newbie questions, but that is exactly what I am.

Anyway, I am stuck at a point in the project, where I am trying to get a motor to rotate when analog input stops that I am using to rotate a second motor. I can get it to do once, but I am not sure how to restart the whole loop for the motor to swing back after the input stops again.

#include <AccelStepper.h>

AccelStepper stepperspin1(4, 8,9,10,11);
//AccelStepper stepperspin2(4, 4,5,6,7);
AccelStepper stepperswing(4, 0,1,2,3);


int swingarmmove=100;

 


void setup()
{  
   stepperspin1.setMaxSpeed(900);
   stepperspin1.setAcceleration(100.0); 
   stepperspin2.setMaxSpeed(900); 
   stepperspin2.setAcceleration(100.0); 
   stepperswing.setMaxSpeed(900);
   stepperswing.setAcceleration(400);
   //Serial.begin  (9600); 
}

void loop()
{  
  
int analog=analogRead(A0);  
  
if (analog==0) 

  {   
  stepperswing.runToNewPosition(swingarmmove);
  stepperswing.stop();
  }


else
  {
  stepperspin1.setSpeed(-1*analog*17);
  stepperspin1.runSpeed();
   analog=0;
 
  }
 
swingarmmove=-swingarmmove;

}

Any helps or tricks would be appreciated, starting to wish I took C++ in school.

You don't need to do anything the loop function will automatically repeat, several thousand time a second in fact.

I apologize, I should clarify.

The final intentions are for there to be five motors in the mechanism. Two are what I call "spin motors" they are the ones that will be rotating when there is analog input, and they will alternate based on the position of a third one that I call the "swing motor" which will rotate 180 degrees every time analog stops.

Two more motors will be added later that will also rotate 180 based on another input, but will halt the rotation of the swing motor until completed.

Anyway, that's the long story, but my current problem is getting the swing motor to rotate 180 after the analog stops, then wait for the analog to stop again to go back. I can either get it to rotate back and forth constantly, or do it once, and then nothing no matter how many times analog is started and stopped.

So is there any way for the swing motor to start at end of analog, wait for start and end of analog, then swing back again to restart the cycle?

What do you mean by:-

after the analog stops,

The analogue input can never stop.

You don't seem to be using that library correctly. The call stepperswing.runToNewPosition requires a long but you are giving it an int.

The stepperswing motor seems to use pins 0 & 1, there are coms pins and will stop the proper functioning of the upload.

What drivers are you using?

Sorry again, I should have said when analog goes to 0. I am using a DC motor as a sensor for the entire rig.

Should I just type a number in as opposed to using a variable for stepperswing.runToNewPosion?

I'll change the pins around, I didnt realize that would be a problem.

As for drivers, I am using a pair of H-bridges.

Also wanted to say, thank you for taking the time to help me out with all this.

I would forget about the control bit first and just make sure you can get the motors running how you want. In that way you will get to learn how to use the library correctly. Look at the examples that came with it.

Only then look at the control. Doing things when an analogue reading reaches zero is not very good practice you should uses something like less than two.

The motors seemed to be running at the speed needed and in the right directions. The speed of the spinning motor was perfectly proportional to the spin of the DC motor used as sensor, and that is why I decided to move unto activating the swing motor based on lack or presence of analog input.

I will try your trick of using analog read of two. I tried using 1 out of desperation, but I guess I didnt go high enough, since I was still having the same problems.