Write My Code For Me!

:wink:

Need a little help with some pure basic code (I bloody hate code)

Trying to set a stepper motor up to be controlled by a couple of buttons, one to start the motor to step continuous (with a delay), and another button to turn the motor just once.

Iv written the code for the motor:

#include <Stepper.h>

#define motorSteps 200     // change this depending on the number of steps
                           // per revolution of your motor
#define motorPin1 8
#define motorPin2 9

// initialize of the Stepper library:
Stepper myStepper(motorSteps, motorPin1,motorPin2); 

void setup() {
  // set the motor speed at 60 RPMS:
  myStepper.setSpeed(60);

}

void loop() {
  // Step forward 100 steps:
  myStepper.step(49);
  delay(500);
  }

But How do i go about setting it up so it will wait for a input from a button?

I guess I have to set the buttons with

const int buttonPin1 = 1;
const int buttonPin2 = 2;

????

And how would I denote two "buttonStates" for setting if (buttonState == HIGH/ == LOW)

I know this is really basic stuff, but even the basics when it comes to code escapes me!

There are a number of button tutorials on the Arduino site that might help you get started. You can start on the Tutorial page, and look in the Digital I/O section.

You read the button state with an digitalRead(pin); where pin is the pin number you have connected the button to.

You have to decide if you need the motor to turn while you are pressing the button or if one press puts it in constant motion.

Each time round the loop you look to see if any button is pressed and take the appropriate action with an if( ) { }

http://www.arduino.cc/playground/Code/Button

[UNTESTED CODE]

#include <Stepper.h>
#include <Button.h>

const byte motorSteps = 200; // change this depending on the number of steps
                                    // per revolution of your motor
const byte STEP_SPEED = 5;
const byte motorPin1 = 8;
const byte motorPin2 = 9;

// initialize of the Stepper library:
Stepper myStepper(motorSteps, motorPin1,motorPin2);

Button button1 = Button(11,PULLUP);
Button button2 = Button(12,PULLUP);

void setup() {
  // set the motor speed at 60 RPMS:
  myStepper.setSpeed(60);
}

void loop() {
 
  if (button1.isPressed()){
    myStepper.step(STEP_SPEED);
    delay(500);
  }

  if (button2.isPressed()){
    // Step forward 1 step
    myStepper.step(1);
  }  

}

Thanks AlphaBeta will go test out the coe tonight......but I make sense now you have laid it out in the right format, and Grumpy_Mike comments......thanks guys :slight_smile: