LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

(UPDATED) Running a motor for 5 seconds and switch directions with a button.

Dear friends,

 

I need to be able to click a button and make a motor run for 5 seconds clockwise then stop, then when I click the button again, it runs for 5 seconds again counter clockwise. It needs to interchange between the two directions with every click. Currently, when I click the button it runs for five seconds clockwise and then after another five seconds it runs again without me clicking the button, it also runs clockwise again. I have linked my work below.

 

I’m new to LabView so I apologize if my code is sloppy or if I am using the wrong setup or function. Thank you for your help! Have a good day.

 

Best regards.

Nate

 

0 Kudos
Message 1 of 5
(931 Views)

Please save for Labview 2018 or earlier. (File -> Save for Previous Version...).

I cannot see your code, but I suppose that you send an angle value to your motor? Or a direction command. You can alternate the direction between each iteration. Either incrementing a counter and doing mod 2 or simply inverting a boolean each time you click the button. Then send + or - angle depending on the value of the boolean (or if iteration mod 2 is 0 or 1).

0 Kudos
Message 2 of 5
(914 Views)

This should be a file for LabVIEW 2018. Thank you for your help.

0 Kudos
Message 3 of 5
(854 Views)

Your code isn't clear, maybe add some comments?

You are just sending a boolean. How are you telling to the motor which direction to go? True = clockwise, false = counterclockwise?

Also you have an external switch that you are reading with a DAQ? You should really be more specific in your question if you want to get some help.

Is this what you mean?

Switch Demo.png

0 Kudos
Message 4 of 5
(841 Views)

What do you want to do?

  • Run a motor for 5 seconds and then make it stop.

How can you do this?

  • Sequentially run the following three commands:
    1. Run the motor.
    2. Wait 5 seconds.
    3. Stop the motor.

Did you learn about LabVIEW being a "Data Flow" programming language?  Did anyone talk to you about the importance of the Error Line, which using goes into and out of LabVIEW functions and sub-VIs on the lower corners?

 

Suppose you have a Function (or VI) called "Run Motor" that, when called, "runs the motor".  Let's make it simple, have one Boolean input that if True makes the motor run Clockwise and if False makes it run Counter-clockwise.  Let's also assume we have another Function called "Stop Motor".

 

Have you ever looked at the Timing functions in LabVIEW?  Look at the function called "Stall".

 

So, now, with Run Motor, Stop Motor, and Stall (the first two having Error In and Error Out in their lower left corners and Run having the Boolean input "CW" in its upper left input), using pencil and paper, draw the Block Diagram for "Run the Motor CW for 5 seconds, then Stop".

 

What change do you have to make to "Run the Motor CCW for 5 seconds, then Stop"?  [Hint -- it's the same code as before, but the input to one VI changes).

 

In LabVIEW, how do you make something repeat, over and over, until you tell it to stop?  [Hint -- look at the Structure Palette on the Block Diagram].

 

Have you learned about the Shift Register, something that you put on the edge of a While or For loop to "feed-back" an Output (a wire that goes to the right-hand edge of the Structure) to a corresponding Input (a wire coming in from the left-hand edge of the Structure)?  In your example, what changes every other loop?  If you had that element in the Shift Register, how would you change it from one value to another?

 

At this point, you should see that you now have the motor going CW for 5 seconds, stopping, then going CCW for 5 seconds, stopping, then going CW again ...

 

Hmm.  Maybe we want to wait 5 seconds, stop the motor, then just sit there until somebody pushes a button to say "Do it Again", or a different button that says "Stop".  Actually, you never said how the motor starts, so instead of "Do it Again", we should call the button "Move".

 

Have you learned about the Event structure?  This is something that "reacts to an Event", the most common of which is some interacts with a Control on the Front Panel, often a rectangular Boolean Control (like OK, Cancel, and Stop).  Do you know why these "act differently" than the other Boolean Controls on the Palette?

 

Suppose, now, you have an Event Structure inside a While Loop, and you added a "Move" and "Stop" button to your Front Panel.  What do you want to happen when you push the Move button?  (You've already worked this one out, I hope).  What do you want to happen when you push the Stop Button?

 

Problem (mostly) solved.  I have no idea what the Express VIs in the code you attached are doing, but I don't really think it matters (and am reasonably sure it is "wrong").  

 

Review:

  • Use Error Line to make functions execute sequentially.
  • Every LabVIEW sub-VI you create should have Error In and Error Out on lower corners, should always be wired.
  • Use Timer functions wisely.  "Stall" sits very nicely on the Error Line, can put a delay between two functions.
  • I didn't say this earlier, but "almost never" use the Frame Sequence.  [You will find a use for it when you have been using LabVIEW for 3-5 years, or in my case, about 10-12 years].
  • Do the Simple Things first, then figure out how to combine simple steps to do the Complete Task.  Eventually Simple Steps will become sub-VIs, which if you create a distinctive Icon for them and write a Description to remind you of what they do and their Inputs and Outputs, will help you and others to use them.

Bob Schor

 

 

0 Kudos
Message 5 of 5
(832 Views)