# AnalogRead to voltage + Stepper Motor

So I have a stepper motor hooked up to the arduino, and I also have a strain meter outputting voltage to the analog input pin of the arduino. I then have that reading converted to a voltage, which then determines how the stepper motor will move. I took an average of the serial monitor to filter out some noise. My problem is that the stepper motor always moves 1 step counter clockwise even when the voltage is out of it's range from the if statements. Here is the whole code:

``````#include

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*4);

Serial.begin(9600);
}

void loop()
{
float sum = 0;

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

delay(300);
}

float average = sum / 5.0;

float voltage = average * 5.0 / 1023;

Serial.println(voltage, 3);

if (voltage > .86 && voltage <= .88)
{
if (myStepper.update() == 1)
{
myStepper.step(stepsPerRevolution);

delay(1000);
}
}
else if (voltage > .84 && voltage <= .86)
{
if (myStepper.update() == 1)
{
myStepper.step(stepsPerRevolution/2);

delay(1000);
}
}
else if (voltage > .82 && voltage <= .84)
{
if (myStepper.update() == 1)
{
myStepper.step(1);

delay(1000);
}
}
else if (voltage >= .76 && voltage < .78)
{
if (myStepper.update() == 1)
{
myStepper.step(-1);

delay(1000);
}
}
else if (voltage >= .74 && voltage < .76)
{
if (myStepper.update() == 1)
{
myStepper.step(-stepsPerRevolution/2);

delay(1000);
}
}
else if (voltage >= .72 && voltage < .74)
{
if (myStepper.update() == 1)
{
myStepper.step(-stepsPerRevolution);

delay(1000);
}
}
}
``````

When I'm in my safe area (.78V to .82V) the stepper motor stops, which is what I want it to do. It also doesn't move when it's above .88V or under .72V, which is what I also want it to do. But anywhere between .72 and .78 and .82 to .88V, the stepper motor just moves one step counterclockwise. It also doesn't delay 1 second in between moves. It delays the exact same speed as when the serial monitor prints the voltage. How can I fix it so I can take the average readings of the serial monitor, but also control my stepper motor to move how I want it to move? Thanks!

I took an average of the serial monitor to filter out some noise.

What does this mean? You can't average text that is displayed in the serial monitor. Properly describing what you are doing will go a long ways towards making it easier to help you.

What you are doing inside each if block is exactly the same, except that the number of steps is different. The if statements should be defining a value for a variable, numberOfSteps, which should be used in ONE if statement to actually move the stepper.

Where are the print() statements in the various if blocks, confirming that the correct one is selected?

I'm not averaging text, I'm averaging the voltage that is being read. That part seems to be working well. If my strain meter is reading 8 lbs, then the serial monitor will read .80V +- .05V after taking the average. So you're saying I'll have to print in each if block to allow the stepper motor to step how I want it to step?

So you're saying I'll have to print in each if block to allow the stepper motor to step how I want it to step?

No. I'm saying you should print a unique message in each block, to confirm that the right block is being executed.

So I put a serial print into each of the if blocks like this:

``````#include

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*4);

Serial.begin(9600);
}

void loop()
{
float sum = 0;

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

delay(300);
}

float average = sum / 5.0;

float voltage = average * 5.0 / 1023;

Serial.println(voltage, 3);

if (voltage > .86 && voltage <= .88)
{
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 >= .72 && voltage < .74)
{
Serial.println(".72-.74");

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

delay(1000);
}
}
}
``````

and when I bring up the serial monitor after uploading, it does print out the correct label according to the voltage being read. So it is reading the if statements correct, but it does not appear to be executing the inside if statement that controls the motor.

So it is reading the if statements correct, but it does not appear to be executing the inside if statement that controls the motor.

It isn't "reading" the if statements. It is evaluating the if statements. Since it appears to be evaluating the if statements correctly, then the failure must be in the code [u]in[/u] the block.

``````    if (myStepper.update() == 1)
{
myStepper.step(stepsPerRevolution);

delay(1000);
}
``````

The stepper is told to step only if update() returns 1. Clearly, then, update() does not always return 1. In order to determine why, we'd need to know what update() does. In order to do that, we'd need to know where you got the StepperDriver library.

It looks like perhaps StepperDriver is intended to be a non-blocking library. As a result, the update() method returns 0 or 1, depending on whether the stepper has reached the last commanded position. That is, has it completed making the necessary number of steps.

I suspect that you want the regular Stepper library, where step() IS blocking. That is, you don't want to do anything more until the stepper HAS completed n steps.