Go Down

Topic: Stop stepper motor when condition satisfy (Read 187 times) previous topic - next topic


Hi to everyone, I am doing for a project a with stepper motor and I'm a little stuck. I want to stop stepper motor when condition satisfies. Actually, rotating the stepper motor and detecting signals simultaneously in voltage with analog port A0. I want to stop the motor when the value is 0.03 or less than it. But the code does not work. Does anyone have a solution?


For short programs it is much easier for people to help if you include the code in your Post so they don't have to download it
Code: [Select]
const float referenceVolts = 5.0;        // the default reference on a 5-volt board
const int batteryPin = 0;                // battery is connected to analog pin 0
const int stepsPerRevolution = 200;      // change this to the number of steps on your motor
Stepper myStepper(stepsPerRevolution, 9, 10, 11, 12); // input pin in the stepper motor (you change this pin also)
int stepCount = 0;                        // initial condition for stepper motor
unsigned long int milli_time;    //variable to hold the time 
void setup()
  Serial.begin(9600);                 // for serial monitor & plotter(Data accumulation)
  Serial.println("CLEARDATA");        //This string is defined
  // to get data in excel sheet using PLX DAQ
  Serial.println("LABEL,Computer Time,Time (Milli Sec.),Stepcount,Degree,Deg,Voltage"); 
void loop()
  int val = analogRead(batteryPin); // read the value from the sensor
  float volts = (val / 1023.0) * referenceVolts; // calculate the ratio
  myStepper.setSpeed(30);               // speed control of stepper motor
  myStepper.step(1);                   // initilazie the step of stepper motor
   milli_time = millis();              // take time if need if not then off this line
  Serial.print("DATA,TIME,");          // to get data in excel sheet using PLX DAQ
  Serial.print(stepCount);             // step of stepper motor
  Serial.print(1.8*stepCount);        // how many degree rotate the motot
  Serial.println(volts); // print the value in volts (get data from detector)
   stepCount ++;

You say the code does not work but you have not told us what it actually does and what you want it to do that is different.

Because you have delay(1000); I suspect it only does one step per second.

I think you only need to call myStepper.setSpeed(30); once in setup(). The stepper won't move unless you call myStepper.step(). However if you only move one step at a time the speed is not used and it is up to you to manage the time by calling myStepper.step(1); at appropriate intervals.

Two or three hours spent thinking and reading documentation solves most programming problems.


Go Up