Adding "delay" into limit switch code

Hey guys, I have written the code below with some help from this thread . I am life-cycle testing an electric actuator and I am using two reed switches (one at full stroke and one at bottom stroke). I need the actuator to go up until full stroke, wait 5 seconds, then go down to fully retracted, wait 5 seconds, and repeat. The code I have works in the sense that it will run the motor up until the upper switch is closed and then will go down until the down switch is closed. I need to add the 5 second delay in, but when I try, the code no longer works. Any help or advice is greatly appreciated.

//constants
const int DirectionPin = 5;
const int PwmPin = 6;
const int UpSwitch = 4;
const int DownSwitch = 3;

//variables
boolean UpSwitch_CurrentState = HIGH;
boolean DownSwitch_CurrentState = HIGH;


void setup() {
  Serial.begin(9600);

  pinMode (DirectionPin, OUTPUT);       //Direction pin for H-Bridge
  pinMode (PwmPin, OUTPUT);             //PWM pin for H-Bridge
  pinMode (UpSwitch, INPUT_PULLUP);     //Up stroke limit switch
  pinMode (DownSwitch, INPUT_PULLUP);   //Down stroke limit switch
}

void loop()
{
  UpSwitch_CurrentState = digitalRead(UpSwitch);
  {
    if (UpSwitch_CurrentState == LOW)
      while ((DownSwitch_CurrentState = digitalRead(DownSwitch)) != LOW) {
        DOWN();
      }
    {
      DownSwitch_CurrentState = digitalRead(DownSwitch);
      {
        if (DownSwitch_CurrentState == LOW)
          while ((UpSwitch_CurrentState = digitalRead(UpSwitch)) != LOW) {
            UP();
          }
      }
    }
  }
}

void UP() {                             //Setting for motor up
  digitalWrite(PwmPin, HIGH);
  digitalWrite(DirectionPin, LOW);
}

void DOWN() {                           //Setting for motor down
  digitalWrite(PwmPin, HIGH);
  digitalWrite(DirectionPin, HIGH);
}

void STOP() {                           //Setting for motor stop/break
  digitalWrite(PwmPin, LOW);
  digitalWrite(DirectionPin, LOW);
}

P.S. My apologies for any coding errors/taboos.

Suggest you avoid using ‘while’, it can be blocking and almost always gets new people into trouble.

Instead of looking at the state of your switches, monitor them for a change in state.

Command your actuator to go up.

When the top limit switch goes from open to closed (LOW), stop the actuator and start your 5 second timer (do not use delay() ).

When the timer finishes, reverse the actuator, when the lower limit switch changes from open to closed (LOW), stop the actuator, start the 5 second timer.

Repeat.

  //*************************************
  //timer
  if (timerFlag == true && currentMillis - timerMillis >= intervalTime)
  {
    timerFlag  = false;
    motorFlag = true;
  }

Attached is code for up only, you can add the DOWN code :wink:

ReversingActuator.ino (3.29 KB)

Thank you so much @larryd

I added the DOWN code and it works great!

I have one more follow up question:

I have an electrical enclosure box where I will all of my electronics. I want to mount a switch on the face of the enclosure that will allow me to stop and start the program for maintenance without unplugging the system all together. What is the best way to implement this into my code or is it even possible?

Thank you in advance.

If you do use delay(), only use it immediately after a STOP() is issued so you know nothing is moving while the processor is unresponsive.

If you fully understand the attached code, great!

If not, you will not learn if you don't ask questions.

See attached code with 'maintenance switch' added.

Note:
This can be a learning experience for how to design a project.
It is much easier (although not always evident) to fully consider everything you want in a project prior to writing code or building the hardware.
Adding things piecemeal, one thing at a time, makes the design more difficult to produce.
Only when you have everything considered should you proceed with implementation.
ex: adding a maintenance switch after code has been written.

It is your responsibility to test all possible situations and confirm you will not get hurt by your apparatus.

ReversingActuator.ino (5.73 KB)