Go Down

Topic: Relay Controlled Door with Limit switches. (Read 3164 times) previous topic - next topic

tuffyt

Hi all;

I wrote the code  the door opened and closed no problem.  Limit switches are working properly.
I made several attempts with this code but  main lock switch (lockPin) did not work properly.
If the main lock is off can not perform any function.  This must be the first priority.  As I told,
code works perfectly in this state but main lock  (lockPin)  I can't control switch.

Code: [Select]

#include <tinyFAT.h>
#include <UTFT.h>
#include <UTFT_tinyFAT.h>
#include <UTouch.h>

extern uint8_t BigFont[];

char doorstate; // setup a variable for the open/closed status of door, using 0 for closed and 1 for open.
char lockstate; // door security lock.


UTouch      myTouch(6,5,4,3,2);

UTFT         myGLCD(CTE70, 38, 39, 40, 41);   //
UTFT_tinyFAT myFiles(&myGLCD);

const int closePin = 8;
const int openPin = 9;
const int lockPin = 10;
const int closeRelay = 11;
const int openRelay = 12;




int x, y;
char stCurrent[20]="";
int stCurrentLen=0;
char stLast[20]="";

void drawButtons()
{
  // Draw the upper row of buttons
  for (x=0; x<5; x++)
  {
    myGLCD.setColor(0, 0, 255);
    myGLCD.fillRoundRect (10+(x*60), 10, 60+(x*60), 60);
    myGLCD.setColor(255, 255, 255);
    myGLCD.drawRoundRect (10+(x*60), 10, 60+(x*60), 60);
    myGLCD.printNumI(x+1, 27+(x*60), 27);
  }
  // Draw the center row of buttons
  for (x=0; x<5; x++)
  {
    myGLCD.setColor(0, 0, 255);
    myGLCD.fillRoundRect (10+(x*60), 70, 60+(x*60), 120);
    myGLCD.setColor(255, 255, 255);
    myGLCD.drawRoundRect (10+(x*60), 70, 60+(x*60), 120);
    if (x<4)
      myGLCD.printNumI(x+6, 27+(x*60), 87);
  }
  myGLCD.print("0", 267, 87);
  // Draw the lower row of buttons
  myGLCD.setColor(0, 0, 255);
  myGLCD.fillRoundRect (10, 130, 150, 180);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (10, 130, 150, 180);
  myGLCD.print("Clear", 40, 147);
  myGLCD.setColor(0, 0, 255);
  myGLCD.fillRoundRect (160, 130, 300, 180);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (160, 130, 300, 180);
  myGLCD.print("Enter", 190, 147);
  myGLCD.setBackColor (0, 0, 0);
}

void updateStr(int val)
{
  if (stCurrentLen<20)
  {
    stCurrent[stCurrentLen]=val;
    stCurrent[stCurrentLen+1]='\0';
    stCurrentLen++;
    myGLCD.setColor(0, 255, 0);
    myGLCD.print(stCurrent, LEFT, 224);
  }
  else
  {
    myGLCD.setColor(255, 0, 0);
    myGLCD.print("BUFFER FULL!", CENTER, 192);
    delay(500);
    myGLCD.print("            ", CENTER, 192);
    delay(500);
    myGLCD.print("BUFFER FULL!", CENTER, 192);
    delay(500);
    myGLCD.print("            ", CENTER, 192);
    myGLCD.setColor(0, 255, 0);
  }
}

void waitForIt(int x1, int y1, int x2, int y2)
{
  myGLCD.setColor(255, 0, 0);
  myGLCD.drawRoundRect (x1, y1, x2, y2);
  while (myTouch.dataAvailable())
    myTouch.read();
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (x1, y1, x2, y2);
}

void setup()
{
  myGLCD.InitLCD();
  myGLCD.clrScr();
  file.initFAT();
  myGLCD.setColor(255,255,255);
  myGLCD.setBackColor(VGA_WHITE);
  //myGLCD.fillScr(VGA_WHITE);

  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);

  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(0, 0, 255);

  //pin modu ayarlari

  pinMode(closePin, INPUT);
  pinMode(openPin, INPUT);
  pinMode(lockPin, INPUT);
  pinMode(openRelay, OUTPUT);
  pinMode(closeRelay, OUTPUT);




}

void loop()


{


  setdoorstate();

  myFiles.loadBitmap(150, 120, 256, 256, "stop.raw");
  myFiles.loadBitmap(540, -10, 256, 256, "ust.raw");
  myFiles.loadBitmap(540, 245, 256, 256, "alt.raw");
  myFiles.loadBitmap(115, 400, 305, 61, "logo.raw");



  while (true)
  {
    if (myTouch.dataAvailable())
    {
      myTouch.read();
      x=myTouch.getX();
      y=myTouch.getY();

      if ((y>=0) && (y<=240))  // Upper row
      {
        if ((x>=540) && (x<=795))  // Button: 1
        {
          waitForIt(540, 0, 795, 240);
         

          setdoorstate();
          opencase();

        }
      }
    }
    if ((y>=245) && (y<=480))
    {
      if ((x>=540) && (x<=795)) // Button: 2
      {
        waitForIt(540, 245, 795, 480);
        //updateStr('2');
        //  if (digitalRead(lockPin) == 0) {
        //  while (digitalRead(lockPin) != 1)
        //  myGLCD.print("DOOR LOCKED                ", 25, 25); }
        //  else{
        //  lockcheck();
        setdoorstate();
        closecase();

      }
    }
  }
}



//void lockcheck()
// {
//  if (digitalRead(lockPin) == 1)
// {
//  myGLCD.print ("DOOR LOCKED!           ", 25, 25);

//    while (digitalRead(lockPin) != 1) {
//   myGLCD.print ("DOOR LOCKED!           ", 25, 25); } }
//  if (digitalRead(lockPin) == 0)
//  {
//   lockstate = 0; }
//   if (lockstate == 1) {
//   while (digitalRead(lockPin) != 1) {
//    myGLCD.print ("DOOR LOCKED!          ", 25, 25); }
// }
// }


void setdoorstate() {
  // if (digitalRead(lockPin) == 1) {
  //   lockstate = 1;
  //  } // check limit switches
  if (digitalRead(openPin) == 1) {
    doorstate = 1;
  } // kasa acik
  if (digitalRead(closePin) == 1) {
    doorstate = 0;
  } // kasa kapali

}   


void opencase()

{

  // if(doorstate == 2)
  // {
  //  myGLCD.print ("DOOR LOCKED", 25, 25);
  // }


  if(doorstate == 1) {
    myGLCD.print ("DOOR IS ALREADY OPEN!               ", 25, 25);
  }

  else{
    myGLCD.print ("DOOR OPENING                ", 25, 25);
    while (digitalRead(openPin) != 1)
    {
      digitalWrite(openRelay, HIGH);
      if (myTouch.dataAvailable()) // Emergency Stop Button
      {
        myTouch.read();
        x=myTouch.getX();
        y=myTouch.getY();

        if ((y>=120) && (y<=370))  // Upper row
        {
          if ((x>=150) && (x<=405))  // Button: 1
          {
            waitForIt(150, 120, 405, 370);
           
            break;

          }
        }
        // delay(50);
      }
      // delay(50);
    }
    digitalWrite(openRelay, LOW);
    myGLCD.print ("DOOR OPEN                   ", 25, 25);
   
  }
}


void closecase()

{




  if(doorstate == 0) {
    myGLCD.print ("the door is already open!              ", 25, 25);
  }
  else{
    myGLCD.print ("DOOR CLOSING                  ", 25, 25);
    while (digitalRead(closePin) != 1)
    {
      digitalWrite(closeRelay, HIGH);
      if (myTouch.dataAvailable())  //Emergency Stop Button
      {
        myTouch.read();
        x=myTouch.getX();
        y=myTouch.getY();

        if ((y>=120) && (y<=370))  // Upper row
        {
          if ((x>=150) && (x<=405))  // Button: 1
          {
            waitForIt(150, 120, 405, 370);
            //updateStr('1');

            // myGLCD.print("DOOR OPENED", 25, 25);
            break;

          }
        }
        // delay(50);
      }
    }
    digitalWrite(closeRelay, LOW);
    myGLCD.print ("DOOR CLOSED                   ", 25, 25);
   
  }
}





Thank you for help.

PaulS

Code: [Select]
  setdoorstate();
Set it to what? You aren't passing the function a value to set anything to.

Code: [Select]
  while (true)
Forget it. I'm not helping you run an infinite loop inside an infinite loop.
The art of getting good answers lies in asking good questions.

tuffyt


Code: [Select]
  setdoorstate();
Set it to what? You aren't passing the function a value to set anything to.

Code: [Select]
  while (true)
Forget it. I'm not helping you run an infinite loop inside an infinite loop.


Hi,
I'm using "Touchscreen" and i don't use "while (true)"  touchscreen does not work properly.
If you have any other suggestions I can try.

Thank you.

PaulS

Quote
I'm using "Touchscreen" and i don't use "while (true)"  touchscreen does not work properly.

Would that be because you keep loading the bitmaps on every pass through loop(), instead of doing that in setup()?
The art of getting good answers lies in asking good questions.

tuffyt

You're right about that but this code will consist of two parts.
Part 1 : Login screen with password protect
Part 2: If password correct, run this code.

so i have not used in setup() for load bitmaps.
I'm not like you have information on this subject, i'm just trying to learn.

Thanks for suggestions.

lar3ry


You're right about that but this code will consist of two parts.
Part 1 : Login screen with password protect
Part 2: If password correct, run this code.

so i have not used in setup() for load bitmaps.


OK... move the following code to the end of setup()

Code: [Select]

  setdoorstate();
  myFiles.loadBitmap(150, 120, 256, 256, "stop.raw");
  myFiles.loadBitmap(540, -10, 256, 256, "ust.raw");
  myFiles.loadBitmap(540, 245, 256, 256, "alt.raw");
  myFiles.loadBitmap(115, 400, 305, 61, "logo.raw");


Get rid of the while (true) and  its terminating right curly brace.
When you write your login stuff, place it in a separate function and call if from just before the code you just moved.. After the return from your login function, decide whether to proceed with the rest of the code, allow a retry for the login, or just use a while(true) {} to stop processing any of your other code until a reset happens.

Alternatively, move the above code into a function that you call after the login at the end of setup(), if the login was successful.

tuffyt

Thanks for the help but I did not get the answer to the first question is still.

Thanks.

PaulS

Quote
Thanks for the help but I did not get the answer to the first question is still.

You have fundamental issues in the structure of your code. You really need to address them first.

What you are doing is a bit like going to the doctor complaining about a hangnail. The doctor discovers that your arm was nearly removed by a chainsaw accident and is barely hanging on. You tell the doctor to just mind his own business and deal with the hangnail. I don't think you'd get any help from him until you stopped bleeding all over the place and it was known whether you'd even keep the arm.

The same situation applies here. You must organize your code properly, and quit trying to circumvent how an Arduino is supposed to work. Then, if the original issue still persists, we can help you resolve it.
The art of getting good answers lies in asking good questions.

lar3ry


I wrote the code  the door opened and closed no problem.  Limit switches are working properly.
I made several attempts with this code but  main lock switch (lockPin) did not work properly.
If the main lock is off can not perform any function.  This must be the first priority.  As I told,
code works perfectly in this state but main lock  (lockPin)  I can't control switch.


Well, I can't figure out from this paragraph, exactly what your problem is.

What does the relay do? What causes lockPin to change state? What makes the lock actually move?
What do you mean by  "If the main lock is off" and  "can not perform any function" and "I can't control switch"?

To clarify, what is "off" in terms of "main lock"? what is "main lock"? Do you mean that if it's off, you can not perform and function, or that if it's off, you don't want it to perform any function?

You really need to describe your problem more clearly. Tell us what you expect the code to do, and what it's actually doing. The terms you use to tell us these things need to be in the same terms you use in the code.

You can start by telling us what activates the input pins, and if they are switches exactly how you have them wired. For example, if lockPin is connected to a switch, what is the other end of the switch connected to? Are there any resistors in that switch circuit (if you say no, you'd better change something).

tuffyt

Quote


Well, I can't figure out from this paragraph, exactly what your problem is.

What does the relay do? What causes lockPin to change state? What makes the lock actually move?
What do you mean by  "If the main lock is off" and  "can not perform any function" and "I can't control switch"?

To clarify, what is "off" in terms of "main lock"? what is "main lock"? Do you mean that if it's off, you can not perform and function, or that if it's off, you don't want it to perform any function?

You really need to describe your problem more clearly. Tell us what you expect the code to do, and what it's actually doing. The terms you use to tell us these things need to be in the same terms you use in the code.

You can start by telling us what activates the input pins, and if they are switches exactly how you have them wired. For example, if lockPin is connected to a switch, what is the other end of the switch connected to? Are there any resistors in that switch circuit (if you say no, you'd better change something).



Hi, thank you for help;
1: openPin = Connected door open position switch.
2: closePin = Connected door close position switch.
3: openRelay = Connected relay to door  open motor. If openPin active dont start motor and say "Door already open".
4:closeRelay = Connected relay to door close motor. If closePin active dont start motor and say "Door already closed".

Actually, there are errors on the code i need to fix, but the system is working properly so far.

but then the problem starts here;

5: lockPin = lockPin connected manually key controlled lock system.  if system manually locked "lockPin" is activated. And if lockPin is active, system say "System Locked!" and system not operate Open or Close door,  only say "System Locked".

I hope I've explained clear.

Thank you..


lar3ry


1: openPin = Connected door open position switch.
2: closePin = Connected door close position switch.
3: openRelay = Connected relay to door  open motor. If openPin active dont start motor and say "Door already open".
4:closeRelay = Connected relay to door close motor. If closePin active dont start motor and say "Door already closed".

Actually, there are errors on the code i need to fix, but the system is working properly so far.

but then the problem starts here;
5: lockPin = lockPin connected manually key controlled lock system.  if system manually locked "lockPin" is activated. And if lockPin is active, system say "System Locked!" and system not operate Open or Close door,  only say "System Locked".


So does it actually say "System Locked!" and does it disable the relay when the lockpin switch is active?
And does say "System Locked" and  allow the the door to move when  the lockpin switch is inactive?

More importantly, you still haven't told me HOW the inputs are conncted. Do the switches have resistors in their circuits or not? If not, you need to make some changes. I can't help you further until you've answered these questions.

tuffyt

Hi  lar3ry,

Quote

So does it actually say "System Locked!" and does it disable the relay when the lockpin switch is active?

Yes,

Quote

And does say "System Locked" and  allow the the door to move when  the lockpin switch is inactive?

Yes but action required (Press Open Door or Close Door button)

Quote

More importantly, you still haven't told me HOW the inputs are conncted. Do the switches have resistors in their circuits or not? If not, you need to make some changes. I can't help you further until you've answered these questions.

No resistors connected in circuit. Arduino Ground (5 volt(-)) connected all switches).

if door is open, openPin(INPUT)=0 volt & closePin(INPUT)=5 volt (until the door is opened openPin & closePin input +5 volt active);

if door is close, closePin(INPUT)=0 volt & openPin(INPUT)=5 volt (until the door is closed openPin & closePin input +5 volt active)

if door currently opening or closing press the "emergency stop button" (closePin & openPin both +5 active)

and if lockPin= +5 v "System Locked".

I hope I explained clearly,

Thank you for your interest and thank you for help :)

lar3ry


Quote

So does it actually say "System Locked!" and does it disable the relay when the lockpin switch is active?

Yes,

Quote

And does say "System Locked" and  allow the the door to move when  the lockpin switch is inactive?

Yes but action required (Press Open Door or Close Door button)


So is that the behaviour you expect? If so, what is the behaviour you don't want to happen?

Quote

No resistors connected in circuit. Arduino Ground (5 volt(-)) connected all switches).
if door is open, openPin(INPUT)=0 volt & closePin(INPUT)=5 volt (until the door is opened openPin & closePin input +5 volt active);
if door is close, closePin(INPUT)=0 volt & openPin(INPUT)=5 volt (until the door is closed openPin & closePin input +5 volt active)
if door currently opening or closing press the "emergency stop button" (closePin & openPin both +5 active)
and if lockPin= +5 v "System Locked".

OK, the first thing you need to do is to do one of these things:

1. add a pulldown resistor from each input pin to GND. A resistor of 10 to 50K should do it.

or

2. Wire the switches from their pins to GND, and set the pinMode to INPUT_PULLUP. Then reverse the sense of the value returned from the digitalRead(). That is, if you now check for HIGH, change it to LOW, and if you now check for LOW, change that to HIGH.

Leaving an input open, that is, not being actively pulled up or down by a resistor will cause the input to be highly susceptible to noise, and if you get noise on the pin when the switch is open, you are likely to have your code act as if the switch was closed, and at randome times.

Go Up