How to make Stepper+Solenoid moving at the same time on adafruit Motorshield V1

Hello everybody,

I’m working on simple project using a stepper and a solenoid on an Adafruit motorshield V1 clone powered with external 12V.
The set-up is working fine with this code :

#include <AFMotor.h>


AF_Stepper motor(400, 2);
AF_DCMotor sole(2);

void setup() {

  Serial.begin(9600);           // set up Serial library at 9600 bps
  motor.setSpeed(5);  // 10 rpm   
}


void loop() {
motor.step(400, FORWARD, MICROSTEP);

for (int i =0; i < 4; i++) {

  sole.run(FORWARD);
  sole.setSpeed(255);  
  delay(100);


  sole.run(RELEASE);
  sole.setSpeed(255);  
  delay(100);
  }
  
  delay (6000);
    
 
}

My issue is to activate the solenoid while the stepper is still moving, at the moment it would activate just when the stepper has finished his 400 steps cicle.

Any suggestions?

Many thanks!
Andrea

somufc:
My issue is to activate the solenoid while the stepper is still moving, at the moment it would activate just when the stepper has finished his 400 steps cicle.

You need non-blocking code for operating the stepper, Have a look at the AccelStepper library, and particularly its run() and runSpeed() functions.

Alternatively just get the motor to move one step at a time so you can do things between steps.

…R
Stepper Motor Basics
Simple Stepper Code

Thank you Robin2!
I successful make them move togheter modifying the AccelStepper example, having this code:

#include <AccelStepper.h>
#include <AFMotor.h>

AF_Stepper motor(400, 2);
AF_DCMotor sole(2);


// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
void forwardstep() {  
  motor.onestep(FORWARD, SINGLE);
}
void backwardstep() {  
  motor.onestep(BACKWARD, SINGLE);
}

AccelStepper stepper(forwardstep, backwardstep); // use functions to step

void solenoid(){
  sole.run(FORWARD);
  sole.setSpeed(255); 
  //delay(80);


  sole.run(RELEASE);
  sole.setSpeed(255); 
 //delay(80);
}

void setup()
{  
   Serial.begin(9600);           // set up Serial library at 9600 bps
   Serial.println("Stepper test!");
  
   stepper.setSpeed(180);  
}

void loop()
{  
   stepper.runSpeed();
   solenoid();
   
}

but i got an issue with the delay, which is necessary using a solenoid…
with the delay uncommented the stepper would stop each step, solutions for dummies?

Many Thanks!
Andrea

Have a look at how millis() is used to manage timing without blocking in Several things at a time

...R

Yes, delay will break lots of things in a complex sketch, learn not to use it. Delay freezes your code
so nothing else can progress (unless its interrupt-driven).

Thank you very much for your support!!! i'm trying to figure it out.

Many Thanks
Andrea

Thanks, I figured it out!
Even if for nothing probably… i think that when the solenoid activates it draws all the power, so the stepper stops… but at least the code should be correct.

#include <AccelStepper.h>
#include <AFMotor.h>

AF_DCMotor sole(2);
AF_Stepper motor1(400, 2);

unsigned long previousStep = 0;
const long intervalStep = 4000;
unsigned long previousMillis = 0;
const long pulse = 80; 

int soleState = RELEASE;

void forwardstep() {  
  motor1.onestep(FORWARD, SINGLE);
}
void backwardstep() {  
  motor1.onestep(BACKWARD, SINGLE);
}

AccelStepper stepper(forwardstep, backwardstep); 

void setup()
{  
   Serial.begin(9600);          
   Serial.println("Stepper test!");
   stepper.setSpeed(30);  
}

void loop()
{  
   stepper.runSpeed();
   Serial.println(stepper.currentPosition());
   Serial.println(millis());

  unsigned long currentStep = stepper.currentPosition();

if (currentStep - previousStep >= intervalStep) {
    previousStep = currentStep;

    for(int i = 0; i < 4;){ // i < 2 = 1 colpo - i < 4 = 2 colpi ecc
      
      unsigned long currentMillis = millis();
  
      if (currentMillis - previousMillis >= pulse) {
         previousMillis = currentMillis;
    
        if (soleState == RELEASE) {
          soleState = FORWARD;
      } else {
          soleState = RELEASE;
      }
  
        sole.run(soleState);
        sole.setSpeed(255);  
        i++;
        }
    
      }
    }
   }

the solenoid has two cycles (activate-deactivate x2. The number of activation depends on the “for” cycle conditions) after a certain number of stepper’s steps (intervalStep) with a “delay” in between of 80ms (pulse).
Both the activation and the “delay” works on the same “BlinkWithoutDelay” example’s logic.

Many thanks,
Andrea

somufc:
. i think that when the solenoid activates it draws all the power, so the stepper stops...

If you want assistance with that you need to post a clear wiring diagram and details of your power supply. A photo of a pencil drawing will be fine. Do not use Fritzing.

You should NOT be powering either the solenoid or the motor through the Arduino.

...R