Go Down

Topic: Help with Stepper motor+Button code (Read 747 times) previous topic - next topic

Im using stepper motors in a rat sculpture. Im trining the rats to use the feeding systems and activate the doors.
Only thing I need help with the code for the stepper motor as I am a newb and totally useless atm.
The 5v stepper motor is wired to a L293d and that is wired upto my D arduino
The rats will press the button activating the stepper motor which will turn xx amount of steps then it will pause for X seconds and reverse x steps to original
This is what I was doing last

#include <Stepper.h>

// change this to the number of steps on your motor
#define STEPS 100

// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to
Stepper stepper(STEPS, 8, 9, 10, 11);

// the previous reading from the analog input
int previous = 0;

void setup()
{
  // set the speed of the motor to 30 RPMs
  stepper.setSpeed(30);
}

void loop()
{
  // get the sensor value
  int val = analogRead(0);

  // move a number of steps equal to the change in the
  // sensor reading
  stepper.step(val - previous);

  // remember the previous value of the sensor
  previous = val;
}

Heres a photo of the Piece


ANY HELP IS SERIOUSLY APPRECIATED

johnwasser

So you want to take out the analogRead() and use a button instead?  You just need to de-bounce the button
Code: [Select]

void loop() {
    static unsigned long lastButtonTime = 0;
    static boolean lastButtonState = false;

    boolean newButtonState = digitaRead(buttonPin);
    unsigned long currentTime = millis();

    // Make sure it has been at least 100 milliseconds since the last button change to filter out bounces
    if (newButtonState != lastButtonState && currentTime-lastButtonTime > 100) [
        lastButtonTime = currentTime;
        lastButtonState = newButtonState;
        if (newButtonState) {  // Fresh button press
             stepper.step(XX);
             delay(X*1000); // Wait X seconds
             stepper.step(-XX);
        }
    }
}
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

This is what I have right now, it tells me that Digital read was not declared in this scope?? Thanks a mill for the reply  :)

#include <Stepper.h>

// change this to the number of steps on your motor
#define STEPS 100

// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to
Stepper stepper(STEPS, 8, 9, 10, 11);

// the previous reading from the analog input
int previous = 0;
const int buttonPin = 2;     // the number of the pushbutton pin
// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup()
{
  // set the speed of the motor to 30 RPMs
  stepper.setSpeed(30);
}

  void loop() {
    static unsigned long lastButtonTime = 0;
    static boolean lastButtonState = false;

    boolean newButtonState = digitaRead(buttonPin);
    unsigned long currentTime = millis();

    // Make sure it has been at least 100 milliseconds since the last button change to filter out bounces
    if (newButtonState != lastButtonState && currentTime-lastButtonTime > 100) [
        lastButtonTime = currentTime;
        lastButtonState = newButtonState;
        if (newButtonState) {  // Fresh button press
             stepper.step(XX);
             delay(X*1000); // Wait X seconds
             stepper.step(-XX);
        }
    }
}
}

johnwasser

That's because I mis-spelled it as 'digitaRead'.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Ha easily corrected, right its done compiling so im going to wire up this button.
Thanks for that John
One small step for man...

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy