Tech project help

Hey Guys,
I am creating a system that will cycle test a doorknob. As seen in the picture, I have 2 stepper motors. Nema 17, and Nema 23.

Nema 17 drives the platform in a leaner direction via lead screw.
Nema 23 motor turns the door knob.

Want I want the system to do:

  1. Nema 17 drives the platform moving it 2", that movement engages the doorknob into the strike plate (Not shown in Drawing)
  2. Nema 17 motor stops.
  3. Nema 23 turn the doorknob unlocking it from the strikeplate.
  4. Nema 17 drives the platform 2" back to the original position.
  5. Nema 23 motor turns the doorknob into its original position.
    And the cycles repeats.
    I understand my drawing is not correct, but you get the concept.

My question is, the code I have has no initial delay, as soon as I power it, it turns on.
How can I add a delay?
Could I add a keyboard button so when I press it, it turns on from the beginning of the code?
How can I add the other stepper motor.
If you need more pictures of clarification let me know
Thanks in advance

Arduino uno
tb6600 drivers (2X for each stepper)
nema 17
nema 23
12 v power supply

I have a code for one working motor.

long Distance = 0;  // Record the number of steps we have taken

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

void loop()  {
  digitalWrite(9, HIGH);
  delayMicroseconds(1000);
  digitalWrite(9, LOW);
  delayMicroseconds(1000);
  Distance = Distance + 1;  //record the step
  // Check to see if we are at the end of our move
  
  if  (Distance == 475)
  {
    // We are! Reverse direction (invert DIR signal)
    if (digitalRead(8) == LOW)
    {
      digitalWrite(8, HIGH);
    }
    else
    {
      digitalWrite(8, LOW);
    }
    
    Distance = 0;
    
    delay(2000);
  }
  
}

You need to explain how the 'delay' fits within the sequence you described. To you want it to be a one-off delay when the device is powered up, or are you looking for a start/stop switch?

The code you have currently only seems to drive a couple of outputs which I guess are the step and direction inputs for a stepper driver. If the driver you're using is a shield then you will need to look at the spec for it to understand how many stepper motors it's capable of driving. If it's a separate board then I expect you could add an another couple of motors. The approach you're using in this sketch would be OK as long as you only want to move one stepper at a time and don't need the sketch to control anything else while it is doing that.

As you are already using the delay() (that I don't like, but anyway...), to answer this question:

hiimvik:
(...)
How can I add a delay?
(...)

you can do something like:

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

and you will have 10s between the power on and the begin of the cycle.

hiimvik:
(...)
Could I add a keyboard button so when I press it, it turns on from the beginning of the code?
(...)

Yes, and this will be better that the delay.

hiimvik:
(...)
How can I add the other stepper motor.
(...)

First, adding another driver board, and then coding the new features. (if you don't give more details, I think is the only answer)

Do you have enough pins to add this devices? What is the keyboard that you are thinking to use?

If you are using a proper stepper motor driver board that takes step and direction signals you would probably be better changing your step code to this

digitalWrite(9, HIGH);
  digitalWrite(9, LOW);
  delayMicroseconds(2000);

In general the driver boards just need a very short pulse to cause a step and putting all the time into one place makes it easier to manage the time between steps.

You should study the technique in the Blink Without Delay example sketch to see a better way of managing time. The delay() function effectively freezes the Arduino until it completes and it is impossible to do other things such as check switches during the delay(). There is an extended demo of the technique in the example several things at a time.

That extended demo may also give you some ideas about how to organize code for your two motors and key buttons. Once your code gets beyond anything trivial it will be much easier to understand if it is divided into separate functions each with a well defined purpose.

...R

I have attached the diagram.

I am basically trying to control 2 steppers

  • both will rotate C.W and C.C.W
  • Both have time delay function so i can play around with the start and stop of motor

Is their not a way I can add the Nema 23 motor, using pin 10,11 as shown on diagram (help with code)
-Complete noob at this, i Just need it to work for my project for a quick video

long Distance = 0;  // Record the number of steps we have taken

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

void loop()  {
  digitalWrite(9, HIGH);
  delayMicroseconds(45);
  digitalWrite(9, LOW);
  delayMicroseconds(45);
  Distance = Distance + 1;  //record the step
  // Check to see if we are at the end of our move
  
  if  (Distance == 64000)
  {
    // We are! Reverse direction (invert DIR signal)
    if (digitalRead(8) == LOW)
    {
      digitalWrite(8, HIGH);
    }
    else
    {
      digitalWrite(8, LOW);
    }
    
    Distance = 0;
    
    delay(2000);
  }
  
}

electrical%20diagram.pdf (110 KB)

If you prefer a push button rather than delay(), CrossRoads suggested a means of "starting" a sketch in a recent thread. That particular requirement was to hold the sketch at the end of setup() before starting loop() so the following code was the very last lines in setup(). It could, I guess, be put at the top or middle of setup() if that was the requirement.

Have a pushbutton on a pin with the other side of the pin to ground. Use the internal pullup, so the pin is high unless pushed. With the code below, it only escapes to continue once the button is pressed and the pin goes low.

//Attribution: CrossRoads 
void setup(){
pinMode (pinX, INPUT_PULLUP); // button with internal pullup resistor, connect to Gnd when pressed
//other setup stuff, like Serial.begin, SPI.begin, etc.
:
:
// last thing:
while (digitalRead(pinX) == HIGH){
// hang out waiting for a LOW
}
// LOW received, proceed
}

I rather have a delay. I can just play around with the seconds until I have both motors sync the way I want them.

I just need to know how I could add the second motor to the exciting sketch I posted earlier today.

The second motor will use pin 10,11 on the arduino and will be connected to another driver. See the wiring diagram provided earlier.

So basically combining these two sketches

Nema 17

long Distance = 0;  // Record the number of steps we have taken

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

void loop()  {
  digitalWrite(9, HIGH);
  delayMicroseconds(45);
  digitalWrite(9, LOW);
  delayMicroseconds(45);
  Distance = Distance + 1;  
  
  
  if  (Distance == 64000)
  {
    
    if (digitalRead(8) == LOW)
    {
      digitalWrite(8, HIGH);
    }
    else
    {
      digitalWrite(8, LOW);
    }
    
    Distance = 0;
    
    delay(2000);
  }
  
}

Nema 23

long Distance = 0;  // Record the number of steps we have taken

void setup() {
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  delay (3000);
}

void loop()  {
  digitalWrite(11, HIGH);
  delayMicroseconds(500);
  digitalWrite(11, LOW);
  delayMicroseconds(500);
  Distance = Distance + 1;  
  
  
  if  (Distance == 64000)
  {
    
    if (digitalRead(10) == LOW)
    {
      digitalWrite(10, HIGH);
    }
    else
    {
      digitalWrite(10, LOW);
    }
    
    Distance = 0;
    
    delay(2000);
  }
  
}

Create a new sketch that contains the two sketches in this way

// all the stuff before setup from both programs

void setup() {
    // all the contents of setup from bothe programs
}

void loop() {
    motorA();
    motorB();
}

void motorA() {
   // all the stuff that is inside loop() for motorA
}

void motorB() {
   // all the stuff that is inside loop() for motorB
}

There may be some duplication or conflicting variable names that you will have to modify but this should be pretty close to a working solution.

...R

thanks for the reply, Both motors run however they turn the same amount of times and the speed is the same even though I change the numbers around.

In my previous sketch that I posted, I was available to change rpm by changing the "delaymicroseconds"
and the number of turns by changing (distance ==3200)

However in this sketch no matter the number I put, nothing changes.
Can you see the problem.

The sketch is below

long Distance = 0;  // Record the number of steps we have taken

void setup() {
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  delay (3000);

}
  void loop() {
    motorA();
    motorB();
}    

void motorA() {

  digitalWrite(9, HIGH);
  delayMicroseconds(30);
  digitalWrite(9, LOW);
  delayMicroseconds(30);
  Distance = Distance + 1;  
  
  
  if  (Distance == 3200)
  {
    
    if (digitalRead(8) == LOW)
    {
      digitalWrite(8, HIGH);
    }
    else
    {
      digitalWrite(8, LOW);
    }
    
    Distance = 0;
    
    delay(2000);
  }
  
}

void motorB() {
  
  digitalWrite(11, HIGH);
  delayMicroseconds(500);
  digitalWrite(11, LOW);
  delayMicroseconds(500);
  Distance = Distance + 1;  
  
  
  if  (Distance == 3200)
  {
    
    if (digitalRead(10) == LOW)
    {
      digitalWrite(10, HIGH);
    }
    else
    {
      digitalWrite(10, LOW);
    }
    
    Distance = 0;
    
    delay(2000);
  }
  
}

hiimvik:
However in this sketch no matter the number I put, nothing changes.

I can't see any obvious problem apart from the likelihood that 30x2 microseconds is probably too short. It would amount to about 16,000 steps per second. Try increasing the minimum to 500x2. And note what I said in an earlier post about only needing one delay within the pulse sequence - i.e. 1 x 1000 rather than 2 x 500.

Your comment above talks of changing "the number" but you don't say which number(s) on what line(s) of your code that you change.

One nice thing about the way your code is now organized is that you can modify the code in loop() like the following to stop one of the motors working while you concentrate on the other.

void loop() {
  //  motorA();
    motorB();
}

If I get a chance later I will try your code with my steppers.

You have a lot of long delays in your code which probably should be replaced with the technique from the Blink Without Delay example sketch.

You could also (if you need to) modify your code so that both motors run simultaneously rather than one after the other. But leave that until they both work properly in sequence.

...R

My friend was able to figure it out and here is the sketch controlling two stepper for my project

#include <Stepper.h>

long Distance = 0;  // Record the number of steps we have taken

void setup() {
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT); // CHANGE x TO WHATEVER YOU PIN IS FOR THE SECOND MOTOR
  pinMode(11, OUTPUT); // CHANGE Y TO WHATEVER YOU PIN IS FOR THE SECOND MOTOR
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW); // CHANGE x TO WHATEVER YOU PIN IS FOR THE SECOND MOTOR
  digitalWrite(11, LOW); // CHANGE Y TO WHATEVER YOU PIN IS FOR THE SECOND MOTOR

}

void loop()  {
delay(1000);//time to control time both motors

// this is where your code will get tricky, using the delay, you need to fire your second motor. You might need to play with your code and build 
//steps using these code sections

// Turn your door knob Open


digitalWrite(10,HIGH);

for (int motor2Distance= 0; motor2Distance < 500; motor2Distance++) { 

		digitalWrite(11, HIGH); 
  		delayMicroseconds(500);//control the nema 23 motor rpm speed C.W
	
		digitalWrite(11, LOW); 
  		delayMicroseconds(500);//control the nema 23 motor rpm speed C.W
}
	
delay(500);

//Open the door

digitalWrite(8, HIGH);
// 1) Reseting the stepper to 0, 2) how many steps you want, 3) counts steps
for (long motor1Distance= 0; motor1Distance < 50000; motor1Distance++) { 

  	digitalWrite(9, HIGH);
 	 delayMicroseconds(50);//speed of motor nema 17 C.W.W

	
 	 digitalWrite(9, LOW); 
  	delayMicroseconds(50);//speed of the motor 17 C.W.W
}

// Turn your door knob back
digitalWrite(10, LOW);

for (int motor2Distance = 0; motor2Distance < 500; motor2Distance++) { 

		digitalWrite(11, HIGH); 
  		delayMicroseconds(500); //rpm of motot nema 23 C.C.W
	
		digitalWrite(11, LOW); 
  		delayMicroseconds(500); //rpm of motot nema 23 C.C.W
}
	
//Close the door
delay(500);
digitalWrite(8, LOW);

for (long motor1Distance= 0; motor1Distance< 50000; motor1Distance++) { 

  	digitalWrite(9, HIGH);
 	 delayMicroseconds(50);

	
 	 digitalWrite(9, LOW); 
  	delayMicroseconds(50);
}


delay(500);



  
}