Program only runs once?

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();

}

}

Please edit your post and add code tags for the code so its readable, thanks...

while (buttonState == LOW) {

sequence();


}

How does that break?

And code tags. Ooo yeah, code tags.

And maybe a bit of "control T" ?

I'm afraid I'm too lazy to read through all your code and try to understand it. If it works once then most of the code is correct so the trick is to isolate where the error is.

In a situation like that I write a simplified version of my code with a pencil on a piece of paper. I use a pencil so I can erase mistakes and I write it by hand so I keep it as short as possible. Then I follow through the logic of what I have written as though I was a computer following the code. The error usually appears fairly quickly.

...R

Sorry, I've now added tags.

The program does what it's supposed to do, but only once. I don't understand why it doesn't loop?

And is it possible to only call a function once?

#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 start button
int sense1State = 0; // variable for reading the IR sensor1
int sense2State = 0; // variable for reading the IR sensor2
int sense3State = 0; //// variable for reading the IR sensor3

void setup()
{
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the IR sensors and pushbutton pin as an input:
pinMode(buttonPin, INPUT);
pinMode(sense1Pin, INPUT);
pinMode(sense2Pin, INPUT);
pinMode(sense3Pin, INPUT);

}

void startbutton()

{
buttonState = digitalRead(buttonPin); //read start button pin

if (buttonState == HIGH) {

digitalWrite(ledPin, HIGH); // turn start button LED on:
}
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); //set motor1 position to -4800
stepper1.setMaxSpeed(2500); //set motor1 to speed 2500
stepper1.setAcceleration(2500); //set motor1 acceleration to 2500
stepper2.moveTo(- 3200); //set motor2 position to -3200
stepper2.setMaxSpeed(2500); //set motor2 to speed 2500
stepper2.setAcceleration(2500); //set motor2 acceleration to 2500
stepper3.moveTo(-1600); //set motor3 position to -1600
stepper3.setMaxSpeed(2500); //set motor3 to speed 2500
stepper3.setAcceleration(2500); //set motor3 acceleration to 2500

stepper1.run(); //run motor 1
stepper2.run(); //run motor 2
stepper3.run(); //run motor 3

}

void homeposition()
{
stepper1.setMaxSpeed(1000); //set motor1 speed to 1000
stepper1.setAcceleration(1500); //set motor1 acceleration to 1500

sense1State = digitalRead(sense1Pin); //read IR sensor 1

stepper2.setMaxSpeed(1000); //set motor2 speed to 1000
stepper2.setAcceleration(1500); //set motor2 acceleration to 1500

sense2State = digitalRead(sense2Pin); //read IR sensor2

stepper3.setMaxSpeed(1000); //set motor3 speed to 1000
stepper3.setAcceleration(1500); //set motor3 acceleration to 1500

sense3State = digitalRead(sense3Pin); //read IR sensor3

if (sense1State == LOW) { //if IR sensor1 is LOW step motor 1

stepper1.move(-267); //set step increment for motor 1
stepper1.run (); //run motor 1
}
else {

stepper1.stop(); //if IR sensor1 is HIGH stop stepper motor 1
}

if (sense2State == LOW) { //if IR sensor2 is LOW step motor 2

stepper2.move(-267); //set step increment for motor 2
stepper2.run (); //run motor 2
}
else {

stepper2.stop(); //if IR sensor2 is HIGH stop stepper motor 2
}

if (sense3State == LOW) { //if IR sensor3 is LOW step motor 3

stepper3.move(-267); //set step increment for motor 3
stepper3.run (); //run motor 3
}
else {

stepper3.stop(); //if IR sensor3 is HIGH stop stepper motor 3
}

}

void loop()
{

homeposition(); //call function home position (all reels step to home position)

startbutton(); //call function startbutton (after reels step to home position, start LED lights)

while (buttonState == LOW) { //program waits on LOW from startbutton (button must be pressed to continue)

sequence(); //all 3 motors run and stop in sequence 3, 2, 1

}

}

Your original code had several bogus lines of code right at the end. What was that supposed to do ?

Sorry, I've now added tags

Where?

while (buttonState == LOW) {     //program waits on LOW from startbutton (button must be pressed to continue)

See reply #2

In the original code I just called the functions again to see if the program would run a second time.

I thought tags were // explanation of the code, if not how do I add code tags?

Cheers,

Paul

OK, put simply, where and how does buttonState change value?

Tags are explained in the posting guidelines.

No the // are called comments.

Code tags are to make the code....

.... appear like this which
{is much (easier to read);
}

Select the code in your post, and hit the # icon just above the :wink: and :sweat_smile:

Also if in the IDE you hit control-T, it will autoformat the code and makes the indents snazzy.

The switch pulls pin 3 LOW on the ardunio, this part works fine. When the switch is goes LOW the function sequence is run.

#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 start button
int sense1State = 0;        // variable for reading the IR sensor1
int sense2State = 0;        // variable for reading the IR sensor2
int sense3State = 0;        //// variable for reading the IR sensor3

void setup()
{  
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the IR sensors and pushbutton pin as an input:
  pinMode(buttonPin, INPUT); 
    pinMode(sense1Pin, INPUT); 
      pinMode(sense2Pin, INPUT); 
        pinMode(sense3Pin, INPUT); 

}

void startbutton()

{
  buttonState = digitalRead(buttonPin); //read start button pin
  
    if (buttonState == HIGH) {     
        
    digitalWrite(ledPin, HIGH);  // turn start button LED on: 
  } 
  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);          //set motor1 position to -4800
        stepper1.setMaxSpeed(2500);      //set motor1 to speed 2500
	stepper1.setAcceleration(2500);  //set motor1 acceleration to 2500
	stepper2.moveTo(- 3200);         //set motor2 position to -3200
        stepper2.setMaxSpeed(2500);      //set motor2 to speed 2500
	stepper2.setAcceleration(2500);  //set motor2 acceleration to 2500
	stepper3.moveTo(-1600);          //set motor3 position to -1600
        stepper3.setMaxSpeed(2500);      //set motor3 to speed 2500
	stepper3.setAcceleration(2500);  //set motor3 acceleration to 2500
    
    stepper1.run(); //run motor 1
    stepper2.run(); //run motor 2
    stepper3.run(); //run motor 3
    
    }
    
    void homeposition()
    {
    stepper1.setMaxSpeed(1000);           //set motor1 speed to 1000
    stepper1.setAcceleration(1500);       //set motor1 acceleration to 1500
    
   sense1State = digitalRead(sense1Pin);  //read IR sensor 1
   
    stepper2.setMaxSpeed(1000);           //set motor2 speed to 1000
    stepper2.setAcceleration(1500);       //set motor2 acceleration to 1500
    
   sense2State = digitalRead(sense2Pin);  //read IR sensor2
   
    stepper3.setMaxSpeed(1000);            //set motor3 speed to 1000
    stepper3.setAcceleration(1500);        //set motor3 acceleration to 1500
    
   sense3State = digitalRead(sense3Pin);  //read IR sensor3
   
  if (sense1State == LOW) {     //if IR sensor1 is LOW step motor 1
        
    stepper1.move(-267);        //set step increment for motor 1
    stepper1.run ();            //run motor 1
  } 
  else {
    
    stepper1.stop();             //if IR sensor1 is HIGH stop stepper motor 1
  }
  
    if (sense2State == LOW) {     //if IR sensor2 is LOW step motor 2
        
    stepper2.move(-267);          //set step increment for motor 2
    stepper2.run ();              //run motor 2
  } 
  else {
   
    stepper2.stop();              //if IR sensor2 is HIGH stop stepper motor 2
  }
  
    if (sense3State == LOW) {     //if IR sensor3 is LOW step motor 3
       
    stepper3.move(-267);          //set step increment for motor 3
    stepper3.run ();              //run motor 3
  } 
  else {
    
    stepper3.stop();              //if IR sensor3 is HIGH stop stepper motor 3
  }
  
}
    
void loop()
{
       
    homeposition();              //call function home position (all reels step to home position)
    
      startbutton();             //call function startbutton (after reels step to home position, start LED lights)

while (buttonState == LOW) {     //program waits on LOW from startbutton (button must be pressed to continue)

sequence();                      //all 3 motors run and stop in sequence 3, 2, 1


}

}

...and what stops "sequence()" from running?

The program waits for the button to be pressed before sequence runs.

So when I power on, all 3 motors move to the home position.

The LED lights. I press the start button, the LED goes out and the motors spin and stop 3, 2, 1.

All good.

But that's where I'm stumped. If the program is in a loop, surely it should run again?

So I would expect the motors to step to the home position and then the process to repeat?

Paul

How about making a copy of your program and deleting all the code that obviously does work properly so you are left with a short simple sketch that illustrates the problem?

...R

There's nothing to delete.

It does work properly, it does what it's supposed to do, but only once.

Each function is called, the button is press and the sequence completes.

All I'm asking is why doesn't it loop?

Paul

Awol has hinted very strongly why not, several times.

To help you see it, I suggest you rewire your circuit so that you're not using pin one and put some serial.print statements in your code. Don't forget serial.begin in setup. One print each at the top of loop, sequence, startbutton and homeposition should do it.