Running Board logic

Hi everyone. I need some advice. I’m working on a project for my truck, I have a set of running boards that I’m trying to install. They didn’t come with a module so I’m working with an arduino to control the logic. So far what I have is the two position sensors wired in on pin A0 and pin A1 to report if the boards are deployed or stowed. Pins D2, D3, D4, and D5 are my running board control relays. Pins D6, D7, D8, and D9 are my door ajar switches. So far the logic is working great. If one of my ajar switches goes low it powers the relay until the position switch reports the proper location. Now what I want to add to the program is some form or jammed protection. For example. If the door opens it will power the relay for the running board but I want it to stop trying if it doesnt reach its position in a certain time, incase their is something blocking the running board. Here is the code I have so far with no jam protection. Keep in mind I’m very new to this and am trying my best with what I can google. Thanks everyone for any help you can lend…

int LeftUp = 2;
int LeftDown = 3;
int RightUp = 4;
int RightDown = 5;
int LFAjar = 6;
int LRAjar = 7;
int RFAjar = 8;
int RRAjar = 9;
int LeftAjarState = 1;
int RightAjarState = 1;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(LeftUp, OUTPUT);
  pinMode(LeftDown, OUTPUT);
  pinMode(RightUp, OUTPUT);
  pinMode(RightDown, OUTPUT);
  pinMode(LFAjar, INPUT);
  pinMode(LRAjar, INPUT);
  pinMode(RFAjar, INPUT);
  pinMode(RRAjar, INPUT);
}

void loop() {
  int LeftPosition = analogRead(A0);
  int RightPosition = analogRead(A1);
  Serial.println(LeftPosition);
  Serial.println(RightPosition);
  Serial.println(LeftAjarState);
  Serial.println(RightAjarState);
  int LFState = digitalRead(LFAjar);
  int LRState = digitalRead(LRAjar);
  int RFState = digitalRead(RFAjar);
  int RRState = digitalRead(RRAjar);
{ if ((LFState == 1) || (LRState == 1)) {
      LeftAjarState = 0;
    }
    else
    {
      LeftAjarState = 1;
    }
  }
{ if ((RFState == 1) || (RRState == 1)) {
      RightAjarState = 0;
    }
    else
    {
      RightAjarState = 1;
    }
  }
{ if ((LeftPosition > 50) && (LeftAjarState == 0)) {
      digitalWrite(LeftUp, HIGH);
    }
    else
    {
      digitalWrite(LeftUp, LOW);
    }
  }
{ if ((LeftPosition < 973) && (LeftAjarState == 1)) {
      digitalWrite(LeftDown, HIGH);
    }
    else
    {
      digitalWrite(LeftDown, LOW);
    }
  }
{ if ((RightPosition > 50) && (RightAjarState == 0)) {
      digitalWrite(RightUp, HIGH);
    }
    else
    {
      digitalWrite(RightUp, LOW);
    }
  }
{ if ((RightPosition < 973) && (RightAjarState == 1)) {
      digitalWrite(RightDown, HIGH);
    }
    else
    {
      digitalWrite(RightDown, LOW);
    }
  }
}

What do You mean by "running board?

Side steps. The are electric running boards that deploy when a door in the vehicle is opened

That is a circle evidence. Running board is a running board...... Board that "deploys".... Not known terminolgies here.

To simplify it. Im looking for something I can add to the code so that the "then" only runs for a specified amount of time. Right now as it stand the "then" will run when a state changes and it reaches its desired analog input. I want to add something so that if the state changes and it starts running the "then" but doesnt reach the desired analog input within a certain amount of time it will give up.

Time stamp the events and compare to millis(). Then drop an "if" if time has passed the limit You want.

Save the value of millis() as the start time and set a boolean to true when the trigger action occurs. In loop(), if the boolean is true then check whether the required period has elapsed by subtracting the start time from the current time and if so take whatever action is needed

I figured it out. Took a lot of trial and error lol. I used the time stamps as suggested, my issue was stopping it from time stamping over and over again. But I added an If statement within the action and that solved my problem. Ill paste the code finished code here for anyone who might be interested. I’m sure the experts will fine some coding faux paw, if you do don’t be afraid to point it out. I’m learning and don’t mind the criticism to improve myself.

int LeftUp = 2;
int LeftDown = 3;
int RightUp = 4;
int RightDown = 5;
int LFAjar = 6;
int LRAjar = 7;
int RFAjar = 8;
int RRAjar = 9;
int LeftAjarState = false;
int RightAjarState = false;
bool LeftDeploying = false;
bool LeftStowing = false;
unsigned long LeftTimer;
bool LeftTiming = true;
bool LeftDeployDisable = false;
bool LeftStowDisable = false;
bool RightDeploying = false;
bool RightStowing = false;
unsigned long RightTimer;
bool RightTiming = true;
bool RightDeployDisable = false;
bool RightStowDisable = false;
unsigned long LeftStowDelayTime;
bool LeftStowDelayTimer = true;
unsigned long RightStowDelayTime;
bool RightStowDelayTimer = true;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(LeftUp, OUTPUT);
  pinMode(LeftDown, OUTPUT);
  pinMode(RightUp, OUTPUT);
  pinMode(RightDown, OUTPUT);
  pinMode(LFAjar, INPUT);
  pinMode(LRAjar, INPUT);
  pinMode(RFAjar, INPUT);
  pinMode(RRAjar, INPUT);
}

void loop() {
  int LeftPosition = analogRead(A0);
  int RightPosition = analogRead(A1);
  Serial.println(LeftPosition);
  Serial.println(RightPosition);
  int LFState = digitalRead(LFAjar);
  int LRState = digitalRead(LRAjar);
  int RFState = digitalRead(RFAjar);
  int RRState = digitalRead(RRAjar);
{ if ((LFState == HIGH) || (LRState == HIGH)) {
      LeftAjarState = true;
      LeftStowDisable = false;
      LeftStowDelayTimer = true;

    }
    else
    {
      LeftAjarState = false;
      LeftDeployDisable = false;
      if (LeftStowDelayTimer == true) {
        LeftStowDelayTime = millis();
        LeftStowDelayTimer = false;
      }
    }
  }
{ if ((RFState == HIGH) || (RRState == HIGH)) {
      RightAjarState = true;
      RightStowDisable = false;
      RightStowDelayTimer = true;
    }
    else
    {
      RightAjarState = false;
      RightDeployDisable = false;
      if (RightStowDelayTimer == true) {
        RightStowDelayTime = millis();
        RightStowDelayTimer = false;
      }
    }
  }
{ if (LeftDeploying == false) {
      if ((LeftPosition > 50) && (LeftAjarState == false) && (LeftStowDisable == false) && ((millis() - LeftStowDelayTime) > 5000)) {
        digitalWrite(LeftUp, LOW);
        LeftStowing = true;
        if (LeftTiming == true) {
          LeftTimer = millis();
          LeftTiming = false;
        }
        if ((millis() - LeftTimer) > 5000) {
          LeftStowDisable = true;
        }
      }
      else
      {
        digitalWrite(LeftUp, HIGH);
        LeftStowing = false;
        LeftTiming = true;
      }
    }
  }
{ if (LeftStowing == false) {
      if ((LeftPosition < 973) && (LeftAjarState == true) && (LeftDeployDisable == false)) {
        digitalWrite(LeftDown, LOW);
        LeftDeploying = true;
        if (LeftTiming == true) {
          LeftTimer = millis();
          LeftTiming = false;
        }
        if ((millis() - LeftTimer) > 5000) {
          LeftDeployDisable = true;
        }
      }
      else
      {
        digitalWrite(LeftDown, HIGH);
        LeftDeploying = false;
        LeftTiming = true;
      }
    }
  }
{ if (RightDeploying == false) {
    { if ((RightPosition > 50) && (RightAjarState == false) && (RightStowDisable == false) && ((millis() - RightStowDelayTime) > 5000)) {
          digitalWrite(RightUp, LOW);
          RightStowing = true;
          if (RightTiming == true) {
            RightTimer = millis();
            RightTiming = false;
          }
          if ((millis() - RightTimer) > 5000) {
            RightStowDisable = true;
          }
        }
        else
        {
          digitalWrite(RightUp, HIGH);
          RightStowing = false;
          RightTiming = true;
        }
      }
    }
  }
{ if (RightStowing == false) {
    { if ((RightPosition < 973) && (RightAjarState == true) && (RightDeployDisable == false)) {
          digitalWrite(RightDown, LOW);
          RightDeploying = true;
          if (RightTiming == true) {
            RightTimer = millis();
            RightTiming = false;
          }
          if ((millis() - RightTimer) > 5000) {
            RightDeployDisable = true;
          }
        }
        else
        {
          digitalWrite(RightDown, HIGH);
          RightDeploying = false;
          RightTiming = true;
        }
      }
    }
  }
}

When interlocking parts , it’s often better to do some of the logic hard wired , rather than just using inputs/outputs so that stuff doesn’t get tangled when your processor or code crashes .

( not exactly sure of what you are doing , but something like limit switches only powering a relay directly for the next function, or system only powered from a contact made when the ignition is off rather than have a processor input to say the ignition is off )