Go Down

Topic: Sketch for Stepper motor?? (Read 2 times) previous topic - next topic

dewyoder18

Thanks PaulS when PIR1 is HIGH and PIR2 is HIGH is when i get confused on my programming... I will try your sketch!! thanks!

dewyoder18

Ok I have tried that sketch but I can't figure it out... I need help trying to figure out how to keep track of how many steps the stepper takes from when it is powered up and if I can get that then I should be able to get the rest... any ideas on how to do this? Here is my code....


Code: [Select]
#include <Stepper.h>

Stepper myStepper = Stepper(200,8,9,10,11);

int ledpin = 13;
int pirpin = 4;
int pirstate = 0; // variable to store current pir state
int lastpirstate = 0; // variable to store last pir state
int pirpin2 = 7;
int pirstate2 = 0;
int lastpirstate2 = 0;
int bothpirstate = 0;
int lastbothpirstate = 0;


void setup() {
 
  myStepper.setSpeed(60); // sets speed of stepper
 
  pinMode(ledpin, OUTPUT);
  pinMode(pirpin, INPUT);
  pinMode(pirpin2, INPUT);
}

void loop() {
 
  int pirpinstate = digitalRead(pirpin);
  int pirpin2state = digitalRead(pirpin2);
  const int pos1 = 50;
  const int pos2 = 100;
 
 
pirstate = digitalRead(pirpin);
 
  if(pirstate != lastpirstate) {
    if(pirstate == HIGH) {
    digitalWrite(ledpin, HIGH);
    myStepper.step(pos1);
  } else {
    digitalWrite(ledpin, LOW);
    myStepper.step(-pos1);
  }
}
lastpirstate = pirstate;

  pirstate2 = digitalRead(pirpin2);
 
  if(pirstate2 != lastpirstate2) {
    if(pirstate2 == HIGH) {
      digitalWrite(ledpin, HIGH);
      myStepper.step(pos2);
    } else {
      digitalWrite(ledpin, LOW);
      myStepper.step(-pos2);
    }
  }
lastpirstate2 = pirstate2;
}



UKHeliBob


I need help trying to figure out how to keep track of how many steps the stepper takes from when it is powered up and if I can get that then I should be able to get the rest... any ideas on how to do this?

Your program is what is telling the stepper to move using the myStepper.step method.  The method takes an argument, so if after each move you add the number of the argument to a variable that is initialised to zero you will know how many steps it has taken won't you ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up