I'm trying to get a steeper motor to go backward and forwards randomly between to set points. I'm trying to adapt the following sketch
[/int Distance = 0; // Record the number of steps we've taken
void setup() {
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
}
void loop() {
digitalWrite(9, HIGH);
delayMicroseconds(100);
digitalWrite(9, LOW);
delayMicroseconds(100);
Distance = Distance + 1; // record this step
// Check to see if we are at the end of our move
if (Distance == 3600)
{
// We are! Reverse direction (invert DIR signal)
if (digitalRead(8) == LOW)
{
digitalWrite(8, HIGH);
}
else
{
digitalWrite(8, LOW);
}
// Reset our distance back to zero since we're
// starting a new move
Distance = 0;
// Now pause for half a second
delay(500);
}
}]
Is there anyway to do what I want to achieve.
I'm useless at programming I have literally been trying for 2 weeks and I can't get anywhere
this is the code I have been using, but I cant get it to work. it just will not compile. I am not really sure what I am doing I have looked at other sketches to add pins but It just keeps giving me an error.
// Random.pde
// -*- mode: C++ -*-
//
// Make a single stepper perform random changes in speed, position and acceleration
//
// Copyright (C) 2009 Mike McCauley
// $Id: Random.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $
#include <AccelStepper.h>
// Define a stepper and the pins it will use
AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
void setup()
{
}
void loop()
{
if (stepper.distanceToGo() == 0)
{
// Random change to speed, position and acceleration
// Make sure we dont get 0 speed or accelerations
delay(1000);
stepper.moveTo(rand() % 200);
stepper.setMaxSpeed((rand() % 200) + 1);
stepper.setAcceleration((rand() % 200) + 1);
}
stepper.run();
}
OK I have sussed it out . I never had the library in correct location.
My problem now is how can I make it go randomly in both directions within a set parameter.. I'm using it on a track with screwed rod so for example it can't turn more than a 100 turns from the center and no more than a 100 turns from the centre in the opposite direction.
Plex1981:
OK I have sussed it out . I never had the library in correct location.
My problem now is how can I make it go randomly in both directions within a set parameter.. I'm using it on a track with screwed rod so for example it can't turn more than a 100 turns from the center and no more than a 100 turns from the centre in the opposite direction.
Hi,
Paul is basically asking, How does it know where it is starting when you run the code?
If it is at one end then you will be pushing the screw block off the end of the thread.
You need to find its position with some linear feedback.
OR
You need two limit switches, one at each end of the maximum travel you are allowing.
When you boot you code or push a start button, the stepper drive unit drives the stepper until it hits a limit, then you reverse the stepper the number of steps needed to get to 1/2 way on your screw-thread.
The limits also act as safety limits in case you stepper is overloaded and it skips steps.
Hey Plex1981,
I just went through something close to what you are doing and found this, You can give the stepper a home point and move from there or you can just add another limit switch in the middle.
You can zero the stepper by using the following in your setup when you turn everything on or modify it to do it where you want, just add homeVal and your limit switch as globals.
void stepperHome() {
homeVal = digitalRead(name of your position sensor);
tableSlider.setSpeed(choose a speed);
while (homeVal == HIGH) {
tableSlider.step(100); //home table to sensor
homeVal = digitalRead(name of your position sensor);
//if you want the stepper in the middle of the 2 limit switches tell it to step that distance
}
}
Add the stepperHome(); inside the setup(); and you're done.
You pick what is best for you, add a limit switch or add the code.