Pages: [1]   Go Down
Author Topic: Help with Stepper motor+Button code  (Read 675 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8967
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So you want to take out the analogRead() and use a button instead?  You just need to de-bounce the button
Code:
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);
        }
    }
}
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley

#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);
        }
    }
}
}
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8967
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's because I mis-spelled it as 'digitaRead'.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: