0

I am trying to control a stepper motor's movements with a pressure sensor input (0 to 5 V digital). For example, if the input is 4 V then the stepper motor should move forward 4 steps. If the input is 3 V then it should come backward 1 step. Please suggest a command in Arduino to keep record of the previous sensor value and steps taken by stepper motor.

This is the code I have written for simple rotation of the motor:

const int stepPin = 8;
const int dirPin = 9;
int sensorValue=A0;
int i;

void setup() {
  Serial.begin(9600);
  pinMode(sensorValue, INPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
}

void loop() {
  sensorValue = analogRead(A0);
  float voltage = sensorValue * (5.0 / 1023.0);
  if(voltage==1) {
    digitalWrite(dirPin, HIGH);
    for(int x = 0; x < 200; x++) {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(5000);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(5000);
    }
    delay(1000); // One second delay
  }
  digitalWrite(dirPin, LOW);
  for(int x = 0; x < 200; x++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
  }
  delay(1000);
  while(1);//only once
}
  • When the sensor is pressed constantly your algorithm moves some steps, waits a second and moves some steps and waits a second again. Is this your desired behavior or maybe you want the speed of the stepper depend on pressure? – Filip Franik Aug 23 '17 at 8:09
  • No, i dont want the speed of the stepper depend on sensor ,speed would be constant.only when the sensor gives some input stepper motor should move few steps and should stop there until it don't get another input .and if it gets input as previous input then it should return back to that position. – shweta S Aug 24 '17 at 3:40
  • So if the sensor gives out constant value you just want to move some steps and then stop? If sensor gives 4V and after a second gives 3V you want the stepper to move 4 steps, wait and then move 1 step backward? – Filip Franik Aug 24 '17 at 6:22
  • yes ,i wanted the same. – shweta S Aug 24 '17 at 8:53
  • Then use s global variable to save previous state of the sensor. Every cycle check if the new one is different. If it's not do nothing. If it is them move the motor and update the previous state of the sensor. – Filip Franik Aug 24 '17 at 9:53
1

I analyzed your code written in the comment and assuming that it works I prepared a modification that should prevent the motor from moving when the sensor is pressed the same way all the time. The noise values you have to adjust yourself. Make it as low as you can.

I cannot test this code, because I don't have your hardware. Treat it as pseudo code.

#include <Stepper.h>
// change this to the number of steps on your motor 
#define STEPS 200
#define NOISE 0.1

Stepper stepper(STEPS, 8, 9);
int previous = 0;
void setup() {
  // set the speed of the motor to 30 RPMs 
  stepper.setSpeed(100);
}

void loop() {
  // get the sensor value 
  float val = analogRead(A0);

  float difference = previous - val; 

  if(difference < -NOISE || difference > NOISE)
  {
    stepper.step(val - previous);
    previous = val;
  }
}
  • i have tested te code with hardware its working f9 now .thanks for the help :) – shweta S Aug 26 '17 at 6:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.