Stepper Motor Help Please!

I have a NEMA 17 stepper motor and an EasyDriver connected to an arduino uno. I also have a strain meter that outputs the force of a load cell I have in terms of voltage. I want the stepper motor to move certain ways according to the force applied to the load cell. I wanted to filter out some noise of the load cell, so I take an average of 1000 samples from the serial monitor, which is working to perfection. The problem that I am having with, is that the stepper motor does not move at all with the code I have (see below). Before I decided to add in the average voltage value from the serial monitor, my stepper motor commands were working well. It was just moving crazy because of all the noise.

#include <StepperDriver.h>

int sensorPin = 0; 

int dirPin = 8;

int stepPin = 9;

const int stepsPerRevolution = 200;

StepperDriver myStepper; 

void setup()
{  
  myStepper.setStep(stepsPerRevolution, dirPin, stepPin);
  
  myStepper.setSpeed(50);
  
  myStepper.step(stepsPerRevolution*8);
  
  Serial.begin(9600);
}

void loop() 
{
  float sum = 0;
  
  for(int i=0; i< 1000; i++)
  {
    sum = sum + analogRead(sensorPin);
  }

  float average = sum / 1000;
  
  float voltage = average * 5.0 / 1023;

  Serial.println(voltage, 3);
  
   
  if (voltage > .86 )
  {
//    Serial.println(".86-.88");
    
    if (myStepper.update() == 1)
    {
      myStepper.step(stepsPerRevolution);
      
      delay(1000);
    }
  }
  else if (voltage > .84 && voltage <= .86)
  {
//    Serial.println(".84-.86");
    
    if (myStepper.update() == 1)
    {      
      myStepper.step(stepsPerRevolution/2);
    
      delay(1000);
    }
  }
  else if (voltage > .82 && voltage <= .84)
  {
//    Serial.println(".82-.84");
    
    if (myStepper.update() == 1)
    { 
      myStepper.step(1);
    
      delay(1000);
    }
  }
  else if (voltage >= .76 && voltage < .78)
  {
//    Serial.println(".76-.78");
    
    if (myStepper.update() == 1)
    { 
      myStepper.step(-1);
    
      delay(1000);
    }
  }
  else if (voltage >= .74 && voltage < .76)
  {
//    Serial.println(".74-.76");
    
    if (myStepper.update() == 1)
    { 
      myStepper.step(-stepsPerRevolution/2);
    
      delay(1000);
    }
  }
  else if (voltage < .74)
  {
//    Serial.println(".72-.74");
    
    if (myStepper.update() == 1)
    { 
      myStepper.step(-stepsPerRevolution);
    
      delay(1000);
    }
  }
}

This is the part that is not working inside each if block:

if (myStepper.update() == 1)
    { 
      myStepper.step(-stepsPerRevolution);
    
      delay(1000);

Edit: Actually, my stepper motor does move, but it's so slow and can barely see it move. It seems like it moves how it is supposed to, but once the serial monitor prints another value, it has to stop and then start again.

Everything works except this?

  else if (voltage < .74)
  {
//    Serial.println(".72-.74");
    
    if (myStepper.update() == 1)
    { 
      myStepper.step(-stepsPerRevolution);
    
      delay(1000);
    }
  }

LarryD:
Everything works except this?

  else if (voltage < .74)

{
//    Serial.println(".72-.74");
   
    if (myStepper.update() == 1)
    {
      myStepper.step(-stepsPerRevolution);
   
      delay(1000);
    }
  }

I assume you have tested your basic setup with the easydriver and that everything works as expected ? Have you adjusted the current trim pot on the easydriver as appropriate for your particular motor ?

Can you remove the Strain guage and averaging from the equation and instead use an external variable pot to simplify things initially ?

Craig

You need to call stepper.update() frequently - but you are not calling it at all inside the averaging loop
which will take 0.1 seconds to complete roughly, so you limit the step rate to about 10 steps a second.

Add a call to update inside that loop?

Ok this is where I'm at now. I added:

for(int i=0; i< 1000; i++)
  {
    sum = sum + analogRead(sensorPin);
    myStepper.update();
  }

Do I just put myStepper.update(); like that? I don't think that can be right because when I run my code now, the serial monitor prints every second, and the stepper motor stops every time it prints. I'm not sure how to let the stepper motor finish it's command completely and then start to read the serial monitor again.

ekraft:
Ok this is where I'm at now. I added:

for(int i=0; i< 1000; i++)

{
    sum = sum + analogRead(sensorPin);
    myStepper.update();
  }



Do I just put myStepper.update(); like that? I don't think that can be right because when I run my code now, the serial monitor prints every second, and the stepper motor stops every time it prints. I'm not sure how to let the stepper motor finish it's command completely and then start to read the serial monitor again.

Comment out all of your serial printing and see what happens

Also as per my previous post can you remove the strain gauge and add a POT ?

You might want to look at the blink without delay sketch to get an idea of the behaviour you are trying to achieve.

Craig