Go Down

Topic: Stepper motor button control  (Read 108 times) previous topic - next topic

Abelrod

looking for some help with controlling a stepper motor.

currently i am running the code below and it works fine. when i push a button the motor goes 6400 steps (1 full rev using the Big Easy Driver)

so when i push the button it executes "program"

i need the "program" to rotate 1 full rev and delay for an amount of time, repeat x# times at a button push


when i push the button
rotate 1 rev
stop 1 sec
rotate  1 rev
stop 1 sec
.
.
.




Code: [Select]
#define DISTANCE 6400

int StepCounter = 0;
int Stepping = false;

void setup() {               
  pinMode(8, OUTPUT);     
  pinMode(9, OUTPUT);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);

  pinMode(3,INPUT);
}

void loop() {
  if (digitalRead(3) == LOW && Stepping == false)
  {
    Stepping = true;
  }

  if (Stepping == true)
  {
    digitalWrite(9, HIGH);
    delayMicroseconds(100);         
    digitalWrite(9, LOW);
    delayMicroseconds(100);

    StepCounter = StepCounter + 1;

    if (StepCounter == DISTANCE)
    {
      StepCounter = 0;
      Stepping = false;
     
     }
  }
}

Robin2

#1
Jun 19, 2018, 06:15 pm Last Edit: Jun 19, 2018, 06:16 pm by Robin2
This is not very elegant but it may meet your needs and it has required very little change to your code
Code: [Select]

void loop() {
  if (digitalRead(3) == LOW && Stepping == false)
  {
    runSequence = true;
  }
  
  if (runSequence == true) {
    for (n = 0; n < numMoves; n++) {
        if (Stepping == false) {
            stepping = true;
            moveMotor();
        }
    }
    runSequence = false;
  }
  
}

void moveMotor() {
  if (Stepping == true)
  {
    digitalWrite(9, HIGH);
    delayMicroseconds(100);        
    digitalWrite(9, LOW);
    delayMicroseconds(100);

    StepCounter = StepCounter + 1;

    if (StepCounter == DISTANCE)
    {
      StepCounter = 0;
      Stepping = false;
      delay(delayPeriod);
    
     }
  }
}


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

Abelrod

thanks a lot!

a couple of things

runSequence
moveMotor
n

are not defined?



Robin2

moveMotor() should be. Check that all the {} are properly paired.

You have to create a variable called runSequence.

And I made a mistake - this is how the line should be
Code: [Select]
for (byte n = 0; n < numMoves; n++) {


If that does not solve the problem post the actual code that you are trying to compile.

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

Go Up