Wireless Receiver and Tramister - Nema17 stepper motor

Hi Guys,

A little help here, i have a stepper motor nema17 and a stepper controler the Easy Driver.

Want a simple thing, that when i press the key fob one time only the stepper motor starts spinning and dont stop.

I manage to get the motor rotating but i have to be pressing the key fob so the motor continues to rotate, if i release the button the motors stops.

int Distancia = 0;  

void setup() {                
  pinMode(2, OUTPUT);     
  pinMode(3, OUTPUT);
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);

  pinMode(5, INPUT); // Input for the Wireless Receiver
}

void loop() {

if (digitalRead(5)){
  
  digitalWrite(2, HIGH);
  delay(1);          
  digitalWrite(2, LOW); 
  delay(1);  
  }
}

Check out the state change detection example in the IDE under File, Examples, Digital. Use that to run the motor when the state of pin5 CHANGES from low to high only (not WHILE pin 5 IS high).

when i press the key fob one time only the stepper motor starts spinning and dont stop.

Then, why, pray tell, are you using a stepper motor?

PaulS,

Because i need Torque and sensitive rotation, that i can manage to have on stepper motors and not on servos.

You may find some useful stuff in stepper motor basics and in this simple stepper code

...R

Still no luck

Already tried waht you guys told me and no luck, still got the motor just running with the key fob pressed always.

 // Set digital pin numbers:
  const int buttonPin = 5;  // The number of the Pushbutton pin

  int buttonState = 0;  // Variable for reading the pushbutton status
  int directionState = 0;  // Variable for reading direction of the servo

  int Distancia = 0; 

void setup() {
    pinMode(2, OUTPUT);     
    pinMode(3, OUTPUT);
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    
   pinMode(buttonPin, INPUT);  // initialize the pushbutton pin as an input
 }

void loop(){
   // read the state of the pushbutton value:
   buttonState = digitalRead(buttonPin);

   if (directionState == 0){
     //The button is pushed
     if (buttonState == HIGH) {
            
         digitalWrite(2, HIGH);
         delayMicroseconds(100);          
         digitalWrite(2, LOW); 
         delayMicroseconds(100);
              Distancia = Distancia + 1;   
  
  
     if (Distancia == 1600)
   {
      if (digitalRead(3) == LOW)
    {
          digitalWrite(3, HIGH);
    }
        else
    {
          digitalWrite(3, LOW);
    }
    
          Distancia = 0;
    
   
          delay(1000);
  }
}

     }
}

You need a variable that denotes whether the motor should run or not. Perhaps motorRun

Then part of your code will be
if (buttonState == HIGH) {
if (motorRun == true) {

digitalWrite(2, HIGH);
delayMicroseconds(100);
digitalWrite(2, LOW);
delayMicroseconds(100);

Then, separately you will need some code that changes the value of motorRun every time you press the button. I will leave that for your homework.

You may be interested in planning and implementing a program - particularly how it organizes the code into small functions.

...R

Thanks Robin2,

Let's test it and pull up some hairs :smiley:

nberga:
pull up some hairs

I think that should be "hares" (animals similar to rabbits)

...R

Ok I've done it.

But still, something about details only, that i like to put on.

Will it be possible to output the delay in there like a countdown? I have already the delay outputting 3000 but would like it to like 3...2...1 or even 1..2..3.

#include <Stepper.h>
long MyTimer;
const int revs = 3600 ; 
boolean wasTriggered = false; 
Stepper stepper1(revs, 2,3);


void setup() {
  Serial.begin(9600);
  stepper1.setSpeed(200); // larger number = faster

  pinMode(5, INPUT); // Input for the Wireless Receiver
}

void loop() {

  if (digitalRead(5)){
      wasTriggered = true;
  }

  if(wasTriggered){
    long myTimer = millis(); //using long type because the value can be very large
    Serial.print("Start");
    delay(3000);
    long timePast = millis() - myTimer; //number of milliseconds since you set myTimer 
    Serial.print(timePast);
    stepper1.step(revs);
    delay(1500);
    stepper1.step(-revs);
    delay(1000);
    Serial.print("Stop");
    wasTriggered = false;
    
  }
  
}

It makes no sense to use both millis() and delay(). The Arduino can do nothing during a delay() - that's why you should use millis() for all your timing. Then you can print any countdown you like.

The use of millis() is illustrated in planning and implementing a program which I linked to earlier and in several things at a time.

...R