Running stepper and calling functions in loop

Hello All Gurus of Arduino.

I read several posts here and with help of all of you, I am putting together a code, Please help me find , why does pressing the push button doesn’t start the process.

Here’s the situation:
BladePos and BottlePos are sensors; the associated pins needs to be high to start a process. Once they are high, a green light needs to turn on. The code works until here.

Now, when the GreenPushButton is pressed nothing happens.I want it to start moving a stepper motor, servo motor and again stepper motor at interval of 2.5 seconds.

There is another input called ‘SelSwitch’ , if this was high, the process will also include turning on BlueLed.

I also want the process to stop immediately, if BladePos or BottlePos goes Low at any time.

Note: code includes calling the functions as mentioned in post, ‘planning and Implementing an Arduino program’. It also uses millis to time the functions

Stepper motor is being controlled using L293D IC and doesn’t use any library. The stepper code works on its own

#include <Servo.h>

  //LEDs and inputs declaration.
const byte GreenledPin = 0;
const byte RedledPin = 1; 
const byte A = 2;
const byte A_bar = 3;
const byte B = 4;
const byte B_bar = 5;
const int x = 5000;
const byte stepsPerRevolution = 200;

const byte GreenPushButtonPin = 6;
const byte BladePosPin = 7; 
const byte BottlePosPin = 8; 
const byte SelSwitchPin = 9;
const byte BlueledPin = 11;
byte GreenPushButtonState;
byte BladePosState; 
byte BottlePosState; 
byte SelSwitchState;
boolean Ready = false;
boolean Start = false;
unsigned long buttonPushedMillis; // when button was pushed.
unsigned long Delay1 = 2500; // wait
unsigned long Delay2 = 5000; // wait
unsigned long Delay3 = 7500; // wait

unsigned long currentMillis; 

Servo BladeServo;
const byte bladeservoPin = 10;
const byte servoMin = 0;
const byte servoMax = 180;
byte servoPosition = servoMin;

void setup() {
  // LEDs and Inputs Setup
pinMode(GreenledPin, OUTPUT);
pinMode(RedledPin, OUTPUT);
pinMode(BlueledPin, OUTPUT);
pinMode(GreenPushButtonPin, INPUT_PULLUP);
pinMode(BladePosPin, INPUT);
pinMode(BottlePosPin, INPUT);
pinMode(SelSwitchPin, INPUT);
  
  // servos setup
 BladeServo.attach(bladeservoPin);
 BladeServo.write(servoPosition);

  //stepper setup
pinMode(A, OUTPUT);
pinMode(A_bar, OUTPUT);
pinMode(B, OUTPUT);
pinMode(B_bar, OUTPUT);

delay(2000) ; //for servo to take position

}

 

void loop() {
   unsigned long currentMillis = millis(); 
   
   checkInputs();
   Start_Process();
  
   
   if (Start == true){
   TurnBlade90Stepper();
     if ((unsigned long)(currentMillis - buttonPushedMillis) >= Delay1) {
   BladeExtractServo();
   }
    if ((unsigned long)(currentMillis - buttonPushedMillis) >= Delay2) {
   TurnBlade90Stepper();
    }
       if(SelSwitchState){
    if ((unsigned long)(currentMillis - buttonPushedMillis) >= Delay3) {
   digitalWrite(BlueledPin, HIGH);
    }
   }
  }
}


void checkInputs() {
   
  GreenPushButtonState = digitalRead(GreenPushButtonPin);
  BladePosState = digitalRead(BladePosPin);
  BottlePosState = digitalRead(BottlePosPin);
  SelSwitchState = digitalRead(SelSwitchPin);
}

void   Start_Process(){
  
  if (  BladePosState == HIGH && BottlePosState == HIGH )
  {boolean Ready = true; digitalWrite(GreenledPin, HIGH);digitalWrite(RedledPin, LOW);
  }
  else {digitalWrite(RedledPin, HIGH);digitalWrite(GreenledPin, LOW);
    }
 
  if(Ready == true && GreenPushButtonState == LOW){
    buttonPushedMillis = currentMillis;
    boolean Start = true;
    }
}


void TurnBlade90Stepper(){

    for (int i = 0; i < (stepsPerRevolution/4) ; i++) {
    digitalWrite(A, HIGH);
    digitalWrite(A_bar, LOW);
    digitalWrite(B, HIGH);
    digitalWrite(B_bar, LOW);
    delayMicroseconds (x);

    digitalWrite(A, LOW);
    digitalWrite(A_bar, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(B_bar, LOW);
    delayMicroseconds (x);

    digitalWrite(A, LOW);
    digitalWrite(A_bar, HIGH);
    digitalWrite(B, LOW);
    digitalWrite(B_bar, HIGH);
    delayMicroseconds (x);

    digitalWrite(A, HIGH);
    digitalWrite(A_bar, LOW);
    digitalWrite(B, LOW);
    digitalWrite(B_bar, HIGH);
    delayMicroseconds (x);
  }
}


void   BladeExtractServo(){
  BladeServo.write(servoMax);
  BladeServo.write(120);
  BladeServo.write(servoMax);
  BladeServo.write(120);
  BladeServo.write(servoMax);
  BladeServo.write(120);
  BladeServo.write(servoMax);
  BladeServo.write(servoMin);
  }

In this code

void   Start_Process(){
  
  if (  BladePosState == HIGH && BottlePosState == HIGH )
  {boolean Ready = true; digitalWrite(GreenledPin, HIGH);digitalWrite(RedledPin, LOW);
  }
  else {digitalWrite(RedledPin, HIGH);digitalWrite(GreenledPin, LOW);
    }
 
  if(Ready == true && GreenPushButtonState == LOW){
    buttonPushedMillis = currentMillis;
    boolean Start = true;
    }
}

your use of the word "boolean" creates new local variables that are different from those with the same name defined at the top of the program. The local variables are lost when the function ends. Remove the word "boolean" from this function.

Separately, it is not a good idea to write code like this

boolean Ready = true; digitalWrite(GreenledPin, HIGH);digitalWrite(RedledPin, LOW);

the code will be much easier to read and debug if each statement is on a separate line like this

boolean Ready = true; 
digitalWrite(GreenledPin, HIGH);
digitalWrite(RedledPin, LOW);

...R

Hi,

 //LEDs and inputs declaration.
const byte GreenledPin = 0;
const byte RedledPin = 1;

Pin 0 and Pin 1 are programming pins and using them for I/O should be avoided.

What model Arduino are you using?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

TomGeorge:
What model Arduino are you using?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

Hello Tom, I am using Uno and I did changed the pins to 12 and 13, instead of 0 and 1.
Also a pic is attached using Fritzing.


Robin2:
your use of the word “boolean” creates new local variables that are different from those with the same name defined at the top of the program. The local variables are lost when the function ends. Remove the word “boolean” from this function.
…R

#include <Servo.h>

//LEDs and inputs declaration.
const byte GreenledPin = 13;
const byte RedledPin = 12;
const byte A = 2;
const byte A_bar = 3;
const byte B = 4;
const byte B_bar = 5;
const int x = 5000;
const byte stepsPerRevolution = 200;

const byte GreenPushButtonPin = 6;
const byte BladePosPin = 7;
const byte BottlePosPin = 8;
const byte SelSwitchPin = 9;
const byte BlueledPin = 11;
byte GreenPushButtonState;
byte BladePosState;
byte BottlePosState;
byte SelSwitchState;
boolean Ready = false;
boolean Start = false;
unsigned long buttonPushedMillis; // when button was pushed.
unsigned long Delay1 = 6000; // wait
unsigned long Delay2 = 5000; // wait
unsigned long Delay3 = 7500; // wait

unsigned long currentMillis;
unsigned long time_a; // Timings for stepper steps.(using without delay)
unsigned long time_b; // Timings for stepper steps.(using without delay)

Servo BladeServo;
const byte bladeservoPin = 10;
const byte servoMin = 20;
const byte servoMax = 160;
byte servoPosition = servoMin;

void setup() {
  Serial.begin(9600);
  // LEDs and Inputs Setup
  pinMode(GreenledPin, OUTPUT);
  pinMode(RedledPin, OUTPUT);
  pinMode(BlueledPin, OUTPUT);
  pinMode(GreenPushButtonPin, INPUT_PULLUP);
  pinMode(BladePosPin, INPUT);
  pinMode(BottlePosPin, INPUT);
  pinMode(SelSwitchPin, INPUT);

  // servos setup
  BladeServo.attach(bladeservoPin);
  BladeServo.write(servoPosition);

  //stepper setup
  pinMode(A, OUTPUT);
  pinMode(A_bar, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(B_bar, OUTPUT);

  delay(2000) ; //for servo to take position

}



void loop() {
  unsigned long currentMillis = millis();

  Serial.println(Start);

  checkInputs();
  Start_Process();

  if (Start == true) {
    TurnBlade90Stepper();
    if ((unsigned long)(currentMillis - buttonPushedMillis) >= Delay1) {
      BladeExtractServo();
    }
  }

  //if ((unsigned long)(currentMillis - buttonPushedMillis) >= Delay2) {
  // TurnBlade90Stepper();
  //}
  if (SelSwitchState) {
    if ((unsigned long)(currentMillis - buttonPushedMillis) >= Delay3) {
      digitalWrite(BlueledPin, HIGH);
    }
  }
}

void checkInputs() {
  GreenPushButtonState = digitalRead(GreenPushButtonPin);
  BladePosState = digitalRead(BladePosPin);
  BottlePosState = digitalRead(BottlePosPin);
  SelSwitchState = digitalRead(SelSwitchPin);
}

void   Start_Process() {
  if (  BladePosState == HIGH && BottlePosState == HIGH )
  { Ready = true;
    digitalWrite(GreenledPin, HIGH);
    digitalWrite(RedledPin, LOW);
  }
  else {
    Ready = false;
    digitalWrite(RedledPin, HIGH);
    digitalWrite(GreenledPin, LOW);
  }

  if (Ready == true && GreenPushButtonState == LOW) {
    buttonPushedMillis = currentMillis;
    Start = true;
  }
  else {
    Start = false;
  }
}



void TurnBlade90Stepper() {

  for (int i = 0; i < (stepsPerRevolution / 4) ; i++) {
    digitalWrite(A, HIGH);
    digitalWrite(A_bar, LOW);
    digitalWrite(B, HIGH);
    digitalWrite(B_bar, LOW);
    delayMicroseconds (x);

    digitalWrite(A, LOW);
    digitalWrite(A_bar, HIGH);
    digitalWrite(B, HIGH);
    digitalWrite(B_bar, LOW);
    delayMicroseconds (x);

    digitalWrite(A, LOW);
    digitalWrite(A_bar, HIGH);
    digitalWrite(B, LOW);
    digitalWrite(B_bar, HIGH);
    delayMicroseconds (x);

    digitalWrite(A, HIGH);
    digitalWrite(A_bar, LOW);
    digitalWrite(B, LOW);
    digitalWrite(B_bar, HIGH);
    delayMicroseconds (x);
  }
}


void   BladeExtractServo() {
  BladeServo.write(servoMax);
  delay(2000);
  BladeServo.write(120);
  delay(2000);
  BladeServo.write(servoMax);
  delay(2000);
  BladeServo.write(120);
  delay(2000);
  BladeServo.write(servoMax);
  delay(2000);
  BladeServo.write(120);
  delay(2000);
  BladeServo.write(servoMax);
  delay(2000);
  BladeServo.write(servoMin);
  delay(2000);
}

Hello Robin2

Based upon your suggestion, I was able to fix that issue. Thanks a lot. Your previous posts have been also very useful.

I have attached my current code, with few other fixes.
Now I am able to move stepper with press of button, but not sure why the servo function doesn’t seem to delay 6 secs, it starts almost instantaneously,

*Once i get the millis working here, I am planning to remove ‘delay’ from stepper and servo functions as well.

This is just a test setup, Actual program will include more steppers and servos. I think I will have to create many Flag boolean variables,

Can you guide me a bit toward finite state machine. Do u think that’ll be better approach?

Mani5678:
Now I am able to move stepper with press of button, but not sure why the servo function doesn't seem to delay 6 secs, it starts almost instantaneously,

There are a lot of delay()s in your program - which one are you referring to?

Can you guide me a bit toward finite state machine. Do u think that'll be better approach?

It makes sequential actions (or actions that should wait for something to happen) much easier to program. I've been trying to write a short tutorial about the concept and this may be the incentive to finish it and publish it. Maybe tomorrow or Tuesday.

...R

Thanks Robin2, A tutorial on State machine would be Great.
Since, i wasn't able to find a good one. And you write a pretty clear and easy to follow guide.

I was referring to delay1 =6000 , rest are still unused.

Mani5678:
I was referring to delay1 =6000 , rest are still unused.

The thing I need to know is which line of the code is supposed to be using that, but is not.

When trying to debug a problem it is a good idea to take a copy of the program and remove all the unused stuff - to let the dog see the rabbit :slight_smile:

...R

Hi,
Ops picture;


Tom... :slight_smile: