Go Down

Topic: How do I make the stepper motor perform a complete revolution? (Read 153 times) previous topic - next topic

jpaulo69

This code works continuously with intervals, I want it to one full turn and stop.
Obs(the relays no control the motor )
Code: [Select]
#include <AccelStepper.h>
// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::DRIVER, 5, 2);
int pos = 50;
int teste;
//Porta ligada ao pino IN1 do modulo
int porta_rele1 = 7;
void setup()
{
  Serial.begin(9600);
  stepper.setEnablePin(8);
  stepper.setMaxSpeed(4000.0);//0
  stepper.setAcceleration(4000.0);//0
  stepper.setCurrentPosition(0); //zero
  pinMode(porta_rele1, OUTPUT);
}
void loop()
{
while (Serial.available()>0)
  teste = Serial.read () ; '-0';
  if (teste == 116)
    { 
            digitalWrite(porta_rele1, HIGH);  //Liga rele 1
       Serial.println(teste);
    digitalWrite(porta_rele1, LOW); //Desliga rele 1
if ((stepper.distanceToGo() != 0)) {
    stepper.run();
}
    if (stepper.distanceToGo() == 0)
      {
        delay(500);
        pos = -pos;
        stepper.moveTo(pos);
      }
    stepper.run();
   }
  }

MorganS

How many steps per revolution? Do you think it would be a good idea to move that many steps instead of just 50 steps?
"The problem is in the code you didn't post."

Robin2

The usual way to make a stepper motor move one revolution is to tell it to move 200 steps (or however many there are in a revolution).

Please use the AutoFormat tool to indent your code consistently and make it easier to read.

...R
Stepper Motor Basics
Simple Stepper Code
Two or three hours spent thinking and reading documentation solves most programming problems.

MarkT

You need something more like:
Code: [Select]

setup()
{
  stepper.setEnablePin(8);
  stepper.setMaxSpeed(4000.0);//0
  stepper.setAcceleration(4000.0);//0
}

loop()
{
  stepper.run();  // must call this all the time, so every time through loop.  Don't use delay()

  if (Serial.available()>0)   // use if, not while...
  {
    char teste = Serial.read () ;
    if (teste == 116)
      stepper.move (200) ;  // initiate relative move - AccelStepper library does the rest.
  }
}

[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up