I've been working on my stepper motor project, but for some reason the program only runs once. So far so good, it does what it's supposed to do, but only once.
I would be grateful for some help explaining why the program only runs once?
Also, I only need to run 'homeposition' once to get an initial start position, presumably that could be moved to void setup or is there a better way to call a function once only?
Cheers,
Paul
// MultiStepper.pde
// -- mode: C++ --
//
// Shows how to multiple simultaneous steppers
// Runs one stepper forwards and backwards, accelerating and decelerating
// at the limits. Runs other steppers at the same time
//
// Copyright (C) 2009 Mike McCauley
// $Id: MultiStepper.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $
#include <AccelStepper.h>
// Define some steppers and the pins the will use
AccelStepper stepper1(1, 9, 8);
AccelStepper stepper2(1, 7, 6);
AccelStepper stepper3(1, 5, 4);
const int buttonPin = 3; // the number of the pushbutton pin
const int sense1Pin = 10; // the number of the pushbutton pin
const int sense2Pin = 11; // the number of the pushbutton pin
const int sense3Pin = 12; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int sense1State = 0;
int sense2State = 0;
int sense3State = 0;
void setup()
{
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
pinMode(sense1Pin, INPUT);
pinMode(sense2Pin, INPUT);
pinMode(sense3Pin, INPUT);
}
void startbutton()
{
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
//if (buttonState == LOW) {
// turn LED on:
//digitalWrite(ledPin, HIGH);
//}
//else {
// turn LED off:
//digitalWrite(ledPin, LOW);
}
void sequence()
{
stepper1.moveTo(-4800);
stepper1.setMaxSpeed(2500);
stepper1.setAcceleration(2500);
stepper2.moveTo(- 3200);
stepper2.setMaxSpeed(2500);
stepper2.setAcceleration(2500);
stepper3.moveTo(-1600);
stepper3.setMaxSpeed(2500);
stepper3.setAcceleration(2500);
stepper1.run();
stepper2.run();
stepper3.run();
}
void homeposition()
{
stepper1.setMaxSpeed(1000);
stepper1.setAcceleration(1500);
sense1State = digitalRead(sense1Pin);
stepper2.setMaxSpeed(1000);
stepper2.setAcceleration(1500);
sense2State = digitalRead(sense2Pin);
stepper3.setMaxSpeed(1000);
stepper3.setAcceleration(1500);
sense3State = digitalRead(sense3Pin);
if (sense1State == LOW) {
stepper1.move(-267);
stepper1.run ();
}
else {
stepper1.stop();
}
if (sense2State == LOW) {
stepper2.move(-267);
stepper2.run ();
}
else {
stepper2.stop();
}
if (sense3State == LOW) {
stepper3.move(-267);
stepper3.run ();
}
else {
stepper3.stop();
}
}
void loop()
{
homeposition();
startbutton();
while (buttonState == LOW) {
sequence();
}
homeposition();
startbutton();
while (buttonState == LOW) {
sequence();
}
}