AcelStepper HELP

Hi All,

I tried to find a clue on how to solve the problem below but it seems I am stuck. I would appreciate much any help.
I’ll make my story simpler. I am trying to operate a valve driven by a small stepper. At the closed position the valve hits a limit switch. I am using a switch to open or close the valve. EasyDrive board is used for driving the stepper. I would like to implement the following logic: In case an open command is received the motor should turn 8000 steps to one side. In case a close command is received the motor should turn 8000 steps to the other side. Then, in case the limit switch is not activated I would like to turn an additional say 1000 steps. I would like to use AccelStepper library because I plan to operate more than one valve in parallel.
Anyhow the code without the limit switch seems to work fine, see below:

void loop ( )  {
  cmdVent = digitalRead (cmdPinVent); //Read the Vent Valve command to OPEN (LOW) or to CLOSE (HIGH)
   if (ventValve1.distanceToGo ( ) == 0) {
    //Check for OPEN command
    if ( cmdVent == LOW && cmdVentOld == HIGH ) {
      ventValve1.move (8000);
      cmdVentOld = cmdVent;
    }
    //Check for CLOSE command
    if ( cmdVent == HIGH && cmdVentOld == LOW) {
     ventValve1.move (-8000);
     cmdVentOld = cmdVent;
      switchVent1=digitalRead (switchPinVent1);
      if (switchVent1 != HIGH) {
      ventValve1.move (-1000);
      }
    }
  }
  ventValve1 .run( );
}

But I am not able to implement the switch to work correctly
Looking forward for anyone assistance

Best,

Morel

It's very hard to follow that much code when you haven't put it in code tags so it looks like this. Please go back to your post and modify it. The instructions are in the How to use this forum Thread

From your description of the problem you need to have some variables that record the state of things - whether the limit switch is pressed, and which command is selected. If it was my project I would separate my code into 2 functions - one to deal with the switches and one to deal with the motor commands. It will make the logic easier to see. Your loop() function would probably look like this

void loop() {
   readAndSaveSwitches();
   updateMotorCommands();
   ventValve1.run( );
}

...R

Hi All,

I am sorry for the previous post, I corrected per your advice. To make the code easier to review I removed the part of the code which deals with definition of the variables, etc.
Anyhow, I think that I miss the method to activate the stepper in the loop based on a condition, this is what I tried to do based on the limit switch and it seems it doesn't work.

void loop ( )  {
  cmdVent = digitalRead (cmdPinVent); //Read the Vent Valve command to OPEN (LOW) or to CLOSE (HIGH)
   if (ventValve1.distanceToGo ( ) == 0) {
    //Check for OPEN command
    if ( cmdVent == LOW && cmdVentOld == HIGH ) {
      ventValve1.move (stepNumber);
      cmdVentOld = cmdVent;
    }
    //Check for CLOSE command
    if ( cmdVent == HIGH && cmdVentOld == LOW) {
     ventValve1.move (-stepNumber);
     cmdVentOld = cmdVent;
      switchVent1=digitalRead (switchPinVent1);
      if (switchVent1 != HIGH) {
      ventValve1.move (-1000);
      }
    }
  }
  ventValve1 .run( );
}

Something seems wrong with the method I try to take into account the status of the limit switch and perform the additional steps.

Thank you all for the help

Morel

You seem to have most of the code inside if (ventValve1.distanceToGo ( ) == 0) {

Is that what you intend?

What is supposed to happen if ventValve1.distanceToGo ( ) is greater or less than 0?

I really do think the logic would be clearer if you reorganize the code into functions.

...R

Thank you very much for your help Robin2!

Indeed, as you say all my code is inside if(ventValve1.distanceToGo ()== 0 {
I did this per the example 3 here: Easy Driver Examples

To your question, as I understand the AccelStepper library (and it seems I don't...) if ventValve is greater than zero the motor will run as ventValve1.run ( ) is called periodically by the main loop . Thus only when the stepper doesn't turn, i.e. when vent.distanceToGo == 0 the target of how many steps to turn should be updated per the command or per the limit switch situation.

What do I miss in the way I use the AccelStepper Library? How would you arrange this very short code into functions as there is not much I am doing?

Appreciate much your help.

Morel

I can't show you the code reorganized in functions because you are not showing the full code - I thought you had that in your first post.

accelStepper.run is like sending a clock tick to library. It "enables" the library to make the motor move IF a move is necessary. A move would not be necessary if the time for the next step had not arrived or if the motor has already reached its destination. In general accelStepper.run should be called as often as possible.

If you have told accelStepper that you want the motor to do (say) 271 steps you don't have to check and tell accelStepper to stop when that is done - it will look after that itself.

If for other purposes of your project you want to know that the move is complete I think there is another function that will tell you that.

...R

Hi Robin2,

Following your comment and if OK with you I would add the code and all in the Thread I opened in the motor and mechanics section.
I thank you for the help.
Best,

Morel