Need Help with actuator/relay/reed switch code

I am currently working on a project where I need two actuators to move back and forth at the same time. They are controlled by a 4 channel relay. They will move forward for 5.5 seconds then stop for 2 then go backward for 5.5 seconds then stop for 2. During this movement though I have a magnetic reed switch and if the proximity is not close the actuators need to stop. If the proximity is close or goes back to being close it should run like normal. I am new to Arduino and help with this would be appreciated! Thanks.


//code to run actuator and reed

long startMillis1 = 0;        

long startMillis2 = 0;        

long startMillis3 = 0;        

long startMillis4 = 0;        

unsigned long currentMillis;

unsigned long ReedMillis1 = 0;

unsigned long ReedMillis2 = 0;

unsigned long ReedstartMillis1;

unsigned long ReedstartMillis2;

unsigned long period0 = 0;

unsigned long period1 = 5500;

unsigned long period2 = 7500;

unsigned long period3 = 13000;

unsigned long period4 = 15000;

const int forwards1 = 7;

const int backwards1 = 6;

const int forwards2 = 5;

const int backwards2 = 4;

const int REED_PIN = 3;

int y = 0;

void setup() {

 pinMode(forwards1, OUTPUT);

 pinMode(backwards1, OUTPUT);

 pinMode(forwards2, OUTPUT);

 pinMode(backwards2, OUTPUT);

 pinMode(REED_PIN, INPUT_PULLUP);



}

void loop() {

 currentMillis = millis() - ReedMillis1 - ReedMillis2; 

  if ((currentMillis - startMillis1 >= period0) && (currentMillis - startMillis1 < period1))

    {

      startMillis1 = currentMillis;

      if (digitalRead(REED_PIN) == LOW)

      {

        digitalWrite(forwards1, LOW);

        digitalWrite(backwards1, HIGH);

        digitalWrite(forwards2, LOW);

        digitalWrite(backwards2, LOW);
        
      }

      else if (digitalRead(REED_PIN) == HIGH) ReedstartMillis1 = currentMillis;

      {

        digitalWrite(backwards1, LOW);

        digitalWrite(backwards2, HIGH);

        ReedMillis1 = millis() - ReedstartMillis1;

      }

    }

    if ((currentMillis - startMillis2 >= period1) && (currentMillis - startMillis2 < period2)) 

    {

      startMillis2 = currentMillis;

      if (digitalRead(REED_PIN) == LOW)

      {

        digitalWrite(forwards1, LOW);

        digitalWrite(backwards1, LOW);

        digitalWrite(forwards2, LOW);       

      }

    }

    if ((currentMillis - startMillis3 >= period2) && (currentMillis - startMillis3 < period3)) 

    {

      startMillis3 = currentMillis;

      if (digitalRead(REED_PIN) == LOW)

      {

        digitalWrite(forwards1, HIGH);

        digitalWrite(backwards1, LOW);

        digitalWrite(forwards2, HIGH);

        digitalWrite(backwards2, HIGH);

      }

      else if (digitalRead(REED_PIN) == HIGH) ReedstartMillis2 = currentMillis;

      {

        digitalWrite(backwards1, LOW);

        digitalWrite(backwards2, HIGH);

        ReedMillis2 = millis() - ReedstartMillis2;

      }

    }

    if ((currentMillis - startMillis4 >= period3) && (currentMillis - startMillis4 < period4)) 

    {

      startMillis4 = currentMillis;

      if (digitalRead(REED_PIN) == LOW)

      {

        digitalWrite(forwards1, HIGH);

        digitalWrite(backwards1, HIGH);

        digitalWrite(forwards2, LOW);

        for (int x = 0; x >= 0; x = x + 1)

        {

          y = 15000 * x;

          period0 = 0 + y;

          period1 = 5500 + y;

          period2 = 7500 + y;

          period3 = 13000 + y;

          period4 = 15000 + y;

        }

      }

    }

}

Suggest you convert this into a proper State Machine sketch.

Writing a sketch without good comments is far from wise.


Start using much better names for your variables.


Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.

That code doesn't do what you think it is doing. Only 1 statement is executed when the if() statement is true. Everything within the parenthesis are always executed.

Use a few well placed Serial.print(" on step # nn"); to assist with diagnosis.

Hello
Publish a timing diagram showing all relationships between timers and relays.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.