Skip to main content

I have a project for raising and lowering a "steel beam" using a big crane. I have the arduino sketch written so it will raise and lower a part by the crane. What I need now is a way of having the arduino run one cycle when a button is pushed then stop untill the nwxt button press. Anyone have any ideas: Is it even possible? What would be the code to make it work?

Thanks for any ideas.

Jay

Original Post

Replies sorted oldest to newest

To detect a button push, you create a switch with pull up resistor. Resistor to VCC of Arduino, other end of resistor to push button and an Arduino input, then from other push button pin to an Arduino GND. So when push button is not pushed, input to Arduino is a 1 (VCC), and when the push button is pushed, the input to Arduino will be 0 (GND). You just need a simple if statement:

void loop() {

if (digitalread(PushButton) == 0)

{

your code for the crane

}

delay(250);  // optional delay, here I set it 250 milliseconds

}

So when you push the push button, Arduino reads a 0 on the input pin you assigned, the if condition is made, and your crane code executes. After your crane code executes, or the if condition is not made, you drop to the delay. After the delay, you go back to the if, to see if the button is pushed.

This simple if results in the crane operation repeating as long as the button is pushed, i.e. there is no provision to force the crane to do only one cycle waiting for the button to be no longer pushed.

Here is the push button circuit I referred to:

PushButtonExample

Attachments

Images (1)
  • PushButtonExample

This is where I'm at:

#include <Stepper.h>

const int stepsPerRevolution = 200;

Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
// set the speed at 50 rpm:
myStepper.setSpeed(50);
}

void loop() {

myStepper.step(stepsPerRevolution);
delay(5000);
myStepper.step(-stepsPerRevolution);
delay(5000);
}

This works but I need it to stop and wait for a button push.

If you are keeping the 10-second delay in your crane routine, then you don't need to worry about debouncing the pushbutton, since everything will have settled by the time one crane cycle executes.

So the simple way to do it would be just something like this

At the beginning of your program, define a button where x is the pin you have connected to a pushbutton which connects to ground when pushed:

#define craneButton x

in setup():

pinMode(craneButton, INPUT_PULLUP);

in your loop() routine:

if (digitalRead(craneButton) == LOW)) {

myStepper.step(stepsPerRevolution);
delay(5000);
myStepper.step(-stepsPerRevolution);
delay(5000);

}

This should execute your crane cycle when the button is pressed.  If the button is still being pressed after the crane cycle is complete, then it will execute it again.

Last edited by Professor Chaos

This is the code I have so far. Motor runs nonstop. So far, no luck having it run one cycle and stop. Here's my code so far.

#include <Stepper.h>
const int buttonPin =3;
const int stepsPerRevolution = 200;

Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
pinMode(buttonPin, INPUT_PULLUP);

myStepper.setSpeed(20);
}
void loop() {
if (digitalRead(buttonPin)==LOW);

myStepper.step(stepsPerRevolution);
delay(1000);
myStepper.step(-stepsPerRevolution);
delay(1000);

}

What do you think?

Thanks for all the ideas so far.

Jay

You didn't use the if statement the way you needed to - no semicolon at end of it statement, and missing open/close braces.

void loop() {
if (digitalRead(buttonPin)==LOW)    // removed semicolon

{          // added open brace

myStepper.step(stepsPerRevolution);
delay(1000);
myStepper.step(-stepsPerRevolution);
delay(1000);

}          // added closing brace
}

A couple things that may me helpful to you. I am a novice Adruino programmer, so there may be other helpful stuff others can teach us.

At the top of the Adruino sketch program (the program you enter your code into, process, and download with) click on Tools, then Auto Format. This will apply indentation to your code to help you make sure you have open/close braces, and see better what code is going to be executed within the open/close braces. For example, your code I modified above, after Auto Format, would appear like this:

void loop() {
if (digitalRead(buttonPin)==LOW)    // removed semicolon

  {          // added open brace

     myStepper.step(stepsPerRevolution);
     delay(1000);
     myStepper.step(-stepsPerRevolution);
     delay(1000);

  }          // added closing brace
}

As you can see, the Auto Format makes you program a bit more readable and can assist in finding problems.

The second thing that you may already know, is that you can click on Help, then click Reference. This will open a browser with a page of all the Arduino programming language statements. You than click on the statement you want more info on, and you get a page describing what and how to use for that statement. I usually right click on the statement I am interested in, then click Open link in new tab. I do this because I usually need two or three statements that I can quickly go back and forth between.

I know this code is very short, but I was always taught by "professional" programmers that you can't have enough comments. They help you remember what you programmed, and maybe why you programmed it. Comments can help others understand what/why you of your program, and help them help you figure out what me be going awry. // can definitely be your friend.

Hope these may be of some help!!!!

Last edited by MED

Add Reply

Post

OGR Publishing, Inc., 1310 Eastside Centre Ct, Ste 6, Mountain Home, AR 72653
800-980-OGRR (6477)
www.ogaugerr.com

×
×
×
×
Link copied to your clipboard.
×
×