Making a sliding mechanism

Pushing the switch connected to pin 2 controls the relay connected to pin 8.

4 controls pin 7.

Oops, just noticed, pin 3 was referenced further up in the thread, should be 4 ???

Pin 2,4 inputs, 7,8 outputs.

void setup()
{
  pinMode(2, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);

  digitalWrite(8, LOW);     //relay de-energized at power up
  pinMode(8, OUTPUT);

  digitalWrite(7, LOW);     //relay de-energized at power up
  pinMode(7, OUTPUT);

}

void loop()
{
  digitalWrite(8, digitalRead(2));
  
  digitalWrite(7, digitalRead(4));

}

Yep i noticed! I made progress and explained in my previous post

Are your switches turning the LEDs ON AND OFF?
Are you wanting 1 switch or two switches in you project?

larryd:
Are your switches turning the LEDs ON AND OFF?

Yes!

larryd:
Are you wanting 1 switch or two switches in you project?

2 buttons that will activate the motor in either direction!

How long 'do you guess' it will take the motor to fully 'open' the door?

How long 'do you guess' it will take the motor to fully 'close' the door?

No more than 5 seconds for both. It's the same path

I'll post a small sketch you can start with in a few minutes.

Wonderful. Just a heads up that my wiring now is exactly like your first:

I do not have your circuit set up so you will have to test this sketch:

//VERSION 1

//*****************************
//switch stuff
#define PUSHED   LOW
#define RELESED  HIGH

//*****************************
//relay stuff
#define powerOFF   LOW
#define powerON    HIGH

#define openDoor   LOW
#define closeDoor  HIGH

//*****************************
//I/O pin variables
const byte powerRelay     = 8;    //controls the power ON/OFF relay
const byte openCloseRelay = 7;    //controls the open/close relay
const byte openSwitch     = 2;    //switch opens the door
const byte closeSwitch    = 4;    //switch that closes the door
const byte heartBeatLED   = 13;   //this LED will toggle if the code is non blocking

//*****************************
//FSM stuff
byte mState           = 0;
const byte STOPPED    = 0;
const byte OPENING    = 1;
const byte CLOSING    = 2;

//*****************************
byte lastStateOpenSwitch;
byte lastStateCloseSwitch;

//*****************************
//Timing variables
const unsigned long doorTravelTime = 5 * 1000; //5 seconds to move door

unsigned long doorOpenMillis;
unsigned long doorClosedMillis;
unsigned long heartBeatMillis;
unsigned long switchMillis;


//****************************************************************************

void setup()
{
  pinMode(openSwitch,  INPUT_PULLUP);
  pinMode(closeSwitch, INPUT_PULLUP);

  digitalWrite(powerRelay, powerOFF);      //at power up, door power is OFF
  pinMode(powerRelay, OUTPUT);

  digitalWrite(openCloseRelay, openDoor);  //at power up door defaults to open
  pinMode(openCloseRelay, OUTPUT);

  pinMode(heartBeatLED,  OUTPUT);          //heart beat LED

} //END of setup()

//****************************************************************************
void loop()
{
  //*****************************
  //time to toggle heart beat LED?
  if (millis() - heartBeatMillis >= 500)
  {
    //restart timer
    heartBeatMillis = millis();

    //toggle LED
    digitalWrite(heartBeatLED, !digitalRead(heartBeatLED));
  }

  //*****************************
  //if there door is not moving, is it time to check the switches?
  if (mState == STOPPED && millis() - switchMillis >= 50)
  {
    //restart timer
    switchMillis = millis();

    //time to check the switches
    checkswitches();
  }

  //*****************************
  //let's check the FSM
  checkMachine();


} //END of loop()

//****************************************************************************
void checkswitches()
{
  byte currentState;

  //*****************************
  currentState = digitalRead(openSwitch);

  //has the switch changed state?
  if (lastStateOpenSwitch != currentState)
  {
    //update to the new state
    lastStateOpenSwitch = currentState;

    //is the switch currently pushed?
    if (currentState == PUSHED)
    {
      //enter the opening state
      mState = OPENING;

      //the time the door started moving open
      doorOpenMillis = millis();

      //set the direction relay
      digitalWrite(openCloseRelay, openDoor);

      //wait 5ms
      delay(5);
      
      //turn on the power to the door
      digitalWrite(powerRelay, powerON);
      
    } //END of  if (currentState == PUSHED)

  } //END of if (lastStateOpenSwitch != currentState)


  //*****************************
  currentState = digitalRead(closeSwitch);

  //has the switch changed state?
  if (lastStateCloseSwitch != currentState)
  {
    //update to the new state
    lastStateCloseSwitch = currentState;

    //is the switch currently pushed?
    if (currentState == PUSHED)
    {
      //enter the closing state
      mState = CLOSING;

      //the time the door started moving closed
      doorClosedMillis = millis();

      //set the direction relay
      digitalWrite(openCloseRelay, closeDoor);

      //wait 5ms
      delay(5);
      
      //turn on the power to the door
      digitalWrite(powerRelay, powerON);
      
    } //END of  if (currentState == PUSHED)

  } //END of if (lastStateCloseSwitch != currentState)

} //END of  checkswitches()


//****************************************************************************
void checkMachine()
{
  //*****************************
  //machine state
  switch (mState)
  {
    //*************
    case STOPPED:
      {
        //do nothing
      }
      break;

    //*************
    case OPENING:
      {
        if (millis() - doorOpenMillis >= doorTravelTime)
        {
          //enter stopped condition
          mState = STOPPED;
          
          //power to door OFF
          digitalWrite(powerRelay, powerOFF);
        }

      }
      break;

    //*************
    case CLOSING:
      {
        if (millis() - doorClosedMillis >= doorTravelTime)
        {
          //enter stopped condition
          mState = STOPPED;
          
          //power to door OFF
          digitalWrite(powerRelay, powerOFF);
        }

      }
      break;

  }
  
}  //END of   checkMachine()

//****************************************************************************

EDIT
See attached 'printable' schematic PDF file.

LabDoorSchematic.pdf (16.2 KB)

Amazing work 8)
I see that you changed the diode +. i did that and the code works as intended!
The only issue is that is not exactly what i intended :slight_smile:

The 2 buttons should do the same job: open/close the door. Because one will be on one side of the door and the other in the other side. I think that we have to make a slight change and let arduino know the state of limit switch and, according to that, open or close the door!

Let me know if i have been clear enough

some pics:

The schematic is essentially/electrically the same as before.

Just moved things around and relabeled things a bit.

For the switches, how about:

  1. 1 push open, 2 pushes closed?
    OR
  2. short push open (< 1 second), long push close (1 > second)?

The limit switches should be fine, they should remove 12v power when actuated, Arduino need not know their position.

The limit switches should open just before the 5 seconds has expired, example at 4 seconds.

The five seconds can easily be adjusted to a different time by changes in the sketch, example 6 seconds.

Having the springs on the door pull wires is a good thing!

larryd:
The schematic is essentially/electrically the same as before.

Just moved things around and relabeled things a bit.

For the switches, how about:

  1. 1 push open, 2 pushes closed?
    OR
  2. short push open (< 1 second), long push close (1 > second)?

i was thinking more that when either button 1 or 2 is pressed it open the door and the closure should be automatic after X seconds if it's not too complicated. Just for the sake of lazyness :stuck_out_tongue:

larryd:
The limit switches should be fine, they should remove 12v power when actuated, Arduino need not know their position.

The limit switches should open just before the 5 seconds has expired, example at 4 seconds.

The five seconds can easily be adjusted to a different time by changes in the sketch, example 6 seconds.

Exactly!

larryd:
Having the springs on the door pull wires is a good thing!

Yep! the steel cable didn't have enough grip on the plastic "shaft" so i added springs for more tension

In the meantime i wired up the motor and tried the current code. it seems to run smooth and no overheatings

“i was thinking more that when either button 1 or 2 is pressed the closure should be automatic after X seconds if it's not too complicated. Just for the sake of

“should be automatic after X seconds”
Please explain.

OR
A push on either switch closes the door, a second push opens the door, a third push closes again, etc.

Looking at your physical switch placements, it might be wise the add some hardware filtering on the switch itself; an updated schematic to follow.

larryd:
“i was thinking more that when either button 1 or 2 is pressed the closure should be automatic after X seconds if it's not too complicated. Just for the sake of lazyness”

Okay, a push on either switch (for more than 500ms) closes the door, a second push opens the door, a third push closes again, etc.

Looking at your physical switch placements, it might be wise the add some hardware filtering on the switch itself; an updated schematic to follow.

I think i still havent cleared my intentions! Think af the doors when you take an elevator.
When you push the button from one side it opens the door, you walk in and, without touching anything the door closes themselves. Like any other automatic door

sugar0:
I think i still havent cleared my intentions! Think af the doors when you take an elevator.
When you push the button from one side it opens the door, you walk in and, without touching anything the door closes themselves. Like any other automatic door

Okay.

Lazy :wink:

See updated schematic PDF.

LabDoorSchematic.pdf (16.9 KB)

or "really advanced tecnology" :smiley:

Version 2:

//VERSION 2

//No effort has been made to minimize sketch size

//*****************************
//Switch stuff
#define PUSHED     LOW

//*****************************
//Power relay
#define powerOFF   LOW
#define powerON    HIGH

//Open/Close relay
#define openDoor   LOW
#define closeDoor  HIGH

//*****************************
//I/O pin variables

const byte insideSwitch   = 2;    //a switch that opens/closes the door
const byte outsideSwitch  = 4;    //a switch that opens/closes the door
const byte openCloseRelay = 7;    //drives the OPEN/Close relay
const byte powerRelay     = 8;    //drives the power ON/OFF relay
const byte heartBeatLED   = 13;   //a LED will toggle if the code is non blocking

//*****************************
//last read switch states
byte  lastStateInsideSwitch;
byte  lastStateOutsideSwitch;

//*****************************
//FSM stuff
const byte STOPPED    = 0;
const byte OPENING    = 1;
const byte WAIT       = 2;
const byte CLOSING    = 3;
byte mState           = STOPPED;  //power up in the stopped state

//*****************************
//Timing variables
const unsigned long doorTravelTime = 5 * 1000; //5 seconds to move door
const unsigned long waitTime       = 5 * 1000; //5 seconds before closing door

unsigned long doorOpenMillis;
unsigned long doorClosedMillis;
unsigned long heartBeatMillis;
unsigned long switchMillis;
unsigned long doorWaitMillis;


//****************************************************************************
void setup()
{
  pinMode(insideSwitch,  INPUT_PULLUP);
  lastStateInsideSwitch = digitalRead(insideSwitch);

  pinMode(outsideSwitch, INPUT_PULLUP);
  lastStateOutsideSwitch = digitalRead(outsideSwitch);

  digitalWrite(powerRelay, powerOFF);      //at power up, door power is OFF
  pinMode(powerRelay, OUTPUT);

  digitalWrite(openCloseRelay, openDoor);  //at power up door defaults to open
  pinMode(openCloseRelay, OUTPUT);

  pinMode(heartBeatLED, OUTPUT);           //heart beat LED

} //END of setup()


//****************************************************************************
void loop()
{
  //*****************************
  //is it time to toggle heart beat LED?
  if (millis() - heartBeatMillis >= 500)
  {
    //restart timer
    heartBeatMillis = millis();

    //toggle LED
    digitalWrite(heartBeatLED, !digitalRead(heartBeatLED));
  }

  //*****************************
  //is it time to check the switches?
  if (millis() - switchMillis >= 50)
  {
    //restart timer
    switchMillis = millis();

    //check the switches
    checkSwitches();
  }

  //*****************************
  //check the FSM
  checkMachine();

  //*****************************
  //other non-blocking code goes here
  //*****************************

} //END of loop()


//****************************************************************************
void checkSwitches()
{
  byte currentState;

  //*****************************                I N S I D E   S W I T C H
  currentState = digitalRead(insideSwitch);

  //if we are in the STOPPED state, has the inside switch changed state?
  if (mState == STOPPED &&  lastStateInsideSwitch != currentState)
  {
    //update to the new state
    lastStateInsideSwitch = currentState;

    //is the switch currently pushed?
    if (currentState == PUSHED)
    {
      //FSM to the OPENING state
      mState = OPENING;

      //the time the door started moving open
      doorOpenMillis = millis();

      //set up the direction relay
      digitalWrite(openCloseRelay, openDoor);

      //wait 10ms for relay to transfer
      delay(10);

      //turn on the power to the door
      digitalWrite(powerRelay, powerON);

    } //END of  if (currentState == PUSHED)

  } //END of if (mState == STOPPED &&  lastStateInsideSwitch != currentState)

  //*****************************                O U T S I D E   S W I T C H
  currentState = digitalRead(outsideSwitch);

  //if we are in the STOPPED state, has the outside switch changed state?
  if (mState == STOPPED &&  lastStateOutsideSwitch != currentState)
  {
    //update to the new state
    lastStateOutsideSwitch = currentState;

    //is the switch currently pushed?
    if (currentState == PUSHED)
    {
      //FSM to the OPENING state
      mState = OPENING;

      //the time the door started moving open
      doorOpenMillis = millis();

      //set up the direction relay
      digitalWrite(openCloseRelay, openDoor);

      //wait 10ms for relay to transfer
      delay(10);

      //turn on the power to the door
      digitalWrite(powerRelay, powerON);

    } //END of  if (currentState == PUSHED)

  } //END of if (mState == STOPPED &&  lastStateOutsideSwitch != currentState)


  //*****************************
  //Future switch area
  //*****************************

} //END of  checkSwitches()


//****************************************************************************
void checkMachine()
{
  //*****************************
  //FSM
  switch (mState)
  {
    //*************
    case STOPPED:
      //do nothing
      break;

    //*************
    case OPENING:
      if (millis() - doorOpenMillis >= doorTravelTime)
      {
        //FSM to the WAIT state
        mState = WAIT;

        //the time we entered the WAIT state
        doorWaitMillis = millis();

        //power to door OFF
        digitalWrite(powerRelay, powerOFF);
      }
      break;

    //*************
    case WAIT:
      if (millis() - doorWaitMillis >= waitTime)
      {
        //FSM to the CLOSING state
        mState = CLOSING;

        //the time the door started moving closed
        doorClosedMillis = millis();
        
        //set up the direction relay
        digitalWrite(openCloseRelay, closeDoor);

        //wait 10ms for relay to transfer
        delay(10);

        //power to door ON
        digitalWrite(powerRelay, powerON);

      }
      break;

    //*************
    case CLOSING:
      if (millis() - doorClosedMillis >= doorTravelTime)
      {
        //FSM to the STOPPED state
        mState = STOPPED;

        //power to door OFF
        digitalWrite(powerRelay, powerOFF);

        //wait 10ms for relay to transfer
        delay(10);

        //set up the direction relay
        digitalWrite(openCloseRelay, openDoor);
      }
      break;

  } //END of  switch/case

}  //END of   checkMachine()

//****************************************************************************

In a similar project, I used large gauge springs:

2019-06-23_11-31-24.jpg

2019-06-23_11-31-24.jpg

larryd:
Version 2:

//VERSION 2

//No effort has been made to minimize sketch size

//*****************************
//Switch stuff
#define PUSHED    LOW

//*****************************
//Power relay
#define powerOFF  LOW
#define powerON    HIGH

//Open/Close relay
#define openDoor  LOW
#define closeDoor  HIGH

//*****************************
//I/O pin variables

const byte insideSwitch  = 2;    //a switch that opens/closes the door
const byte outsideSwitch  = 4;    //a switch that opens/closes the door
const byte openCloseRelay = 7;    //drives the OPEN/Close relay
const byte powerRelay    = 8;    //drives the power ON/OFF relay
const byte heartBeatLED  = 13;  //a LED will toggle if the code is non blocking

//*****************************
//last read switch states
byte  lastStateInsideSwitch;
byte  lastStateOutsideSwitch;

//*****************************
//FSM stuff
const byte STOPPED    = 0;
const byte OPENING    = 1;
const byte WAIT      = 2;
const byte CLOSING    = 3;
byte mState          = STOPPED;  //power up in the stopped state

//*****************************
//Timing variables
const unsigned long doorTravelTime = 5 * 1000; //5 seconds to move door
const unsigned long waitTime      = 5 * 1000; //5 seconds before closing door

unsigned long doorOpenMillis;
unsigned long doorClosedMillis;
unsigned long heartBeatMillis;
unsigned long switchMillis;
unsigned long doorWaitMillis;

//****************************************************************************
void setup()
{
  pinMode(insideSwitch,  INPUT_PULLUP);
  lastStateInsideSwitch = digitalRead(insideSwitch);

pinMode(outsideSwitch, INPUT_PULLUP);
  lastStateOutsideSwitch = digitalRead(outsideSwitch);

digitalWrite(powerRelay, powerOFF);      //at power up, door power is OFF
  pinMode(powerRelay, OUTPUT);

digitalWrite(openCloseRelay, openDoor);  //at power up door defaults to open
  pinMode(openCloseRelay, OUTPUT);

pinMode(heartBeatLED, OUTPUT);          //heart beat LED

} //END of setup()

//****************************************************************************
void loop()
{
  //*****************************
  //is it time to toggle heart beat LED?
  if (millis() - heartBeatMillis >= 500)
  {
    //restart timer
    heartBeatMillis = millis();

//toggle LED
    digitalWrite(heartBeatLED, !digitalRead(heartBeatLED));
  }

//*****************************
  //is it time to check the switches?
  if (millis() - switchMillis >= 50)
  {
    //restart timer
    switchMillis = millis();

//check the switches
    checkSwitches();
  }

//*****************************
  //check the FSM
  checkMachine();

//*****************************
  //other non-blocking code goes here
  //*****************************

} //END of loop()

//****************************************************************************
void checkSwitches()
{
  byte currentState;

//*****************************                I N S I D E  S W I T C H
  currentState = digitalRead(insideSwitch);

//if we are in the STOPPED state, has the inside switch changed state?
  if (mState == STOPPED &&  lastStateInsideSwitch != currentState)
  {
    //update to the new state
    lastStateInsideSwitch = currentState;

//is the switch currently pushed?
    if (currentState == PUSHED)
    {
      //FSM to the OPENING state
      mState = OPENING;

//the time the door started moving open
      doorOpenMillis = millis();

//set up the direction relay
      digitalWrite(openCloseRelay, openDoor);

//wait 10ms for relay to transfer
      delay(10);

//turn on the power to the door
      digitalWrite(powerRelay, powerON);

} //END of  if (currentState == PUSHED)

} //END of if (mState == STOPPED &&  lastStateInsideSwitch != currentState)

//*****************************                O U T S I D E  S W I T C H
  currentState = digitalRead(outsideSwitch);

//if we are in the STOPPED state, has the outside switch changed state?
  if (mState == STOPPED &&  lastStateOutsideSwitch != currentState)
  {
    //update to the new state
    lastStateOutsideSwitch = currentState;

//is the switch currently pushed?
    if (currentState == PUSHED)
    {
      //FSM to the OPENING state
      mState = OPENING;

//the time the door started moving open
      doorOpenMillis = millis();

//set up the direction relay
      digitalWrite(openCloseRelay, openDoor);

//wait 10ms for relay to transfer
      delay(10);

//turn on the power to the door
      digitalWrite(powerRelay, powerON);

} //END of  if (currentState == PUSHED)

} //END of if (mState == STOPPED &&  lastStateOutsideSwitch != currentState)

//*****************************
  //Future switch area
  //*****************************

} //END of  checkSwitches()

//****************************************************************************
void checkMachine()
{
  //*****************************
  //FSM
  switch (mState)
  {
    //*************
    case STOPPED:
      //do nothing
      break;

//*************
    case OPENING:
      if (millis() - doorOpenMillis >= doorTravelTime)
      {
        //FSM to the WAIT state
        mState = WAIT;

//the time we entered the WAIT state
        doorWaitMillis = millis();

//power to door OFF
        digitalWrite(powerRelay, powerOFF);
      }
      break;

//*************
    case WAIT:
      if (millis() - doorWaitMillis >= waitTime)
      {
        //FSM to the CLOSING state
        mState = CLOSING;

//the time the door started moving closed
        doorClosedMillis = millis();
       
        //set up the direction relay
        digitalWrite(openCloseRelay, closeDoor);

//wait 10ms for relay to transfer
        delay(10);

//power to door ON
        digitalWrite(powerRelay, powerON);

}
      break;

//*************
    case CLOSING:
      if (millis() - doorClosedMillis >= doorTravelTime)
      {
        //FSM to the STOPPED state
        mState = STOPPED;

//power to door OFF
        digitalWrite(powerRelay, powerOFF);

//wait 10ms for relay to transfer
        delay(10);

//set up the direction relay
        digitalWrite(openCloseRelay, openDoor);
      }
      break;

} //END of  switch/case

}  //END of  checkMachine()

//****************************************************************************






In a similar project, I used large gauge springs:

![2019-06-23_11-31-24.jpg|383x384](upload://kOIDZG4Bn39uyQwtb7kF1EyAPrN.jpeg)

Yeah Larry you did it! Pressing either button it spin in one direction and after X second in the other.
That massive code must took you much time .. i'm really gratefull. The least i can do is try to understand it as much as i can before completing the work.

Do you think we can add that last segment where i want to leave the door open until i press again the button?

Did you print the new schematic posted in post #74?

larryd:
Did you print the new schematic posted in post #74?

Oops i missed the edit! Are those edit for protection?