Go Down

Topic: Touchscreen code to unlock electric strike. (Read 711 times) previous topic - next topic

egeeked

This is really a two part question. I have some modified code that I have nearly working. I am reaching two problems. First let me explain the purpose of the code. I have a touchscreen attached to to a Mega2560 and I have the numbers 0-9 in the forum of buttons. I want to be able to type in a "code" and it releases an electric strike. I have the board wired up and everything works except, when the code is entered it is not registering. Meaning it doesn't check to see if the "secret code" is correct. There is an if statement in place and to my eye it appears to be correct. The second is, in the if statement I have code to send power to the lock,delay,then power down the lock. This part of the code doesn't run. (I have tested it on both sides of the if else)

Any advice on what is wrong that could put me in the right direction would be greatly appreciated. Thank you very much

Code: [Select]

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

// Declare which fonts we will be using
extern uint8_t BigFont[];

// Uncomment the next two lines for the Arduino 2009/UNO
//UTFT        myGLCD(ITDB24D,19,18,17,16);   // Remember to change the model parameter to suit your display module!
//UTouch      myTouch(15,10,14,9,8);

// Uncomment the next two lines for the Arduino Mega
UTFT        myGLCD(ITDB32S, 38,39,40,41);   // Remember to change the model parameter to suit your display module!
UTouch      myTouch(6,5,4,3,2);

int x, y;
int lock = 13;
const int lockOpenTime = 3000;
char secretCode[]="000000";
char stCurrent[6]="";
int stCurrentLen;
char stLast[20]="";

/*************************
**   Custom functions   **
*************************/

void drawButtons()
{
// Draw the upper row of buttons
  for (x=0; x<5; x++)
  {
    myGLCD.setColor(0, 0, 255);
    myGLCD.fillRoundRect (10+(x*60), 40, 60+(x*60), 90);
    myGLCD.setColor(255, 255, 255);
    myGLCD.drawRoundRect (10+(x*60), 40, 60+(x*60), 90);
    myGLCD.printNumI(x+1, 27+(x*60), 57);
  }
// Draw the center row of buttons
  for (x=0; x<5; x++)
  {
    myGLCD.setColor(0, 0, 255);
    myGLCD.fillRoundRect (10+(x*60), 100, 60+(x*60), 150);
    myGLCD.setColor(255, 255, 255);
    myGLCD.drawRoundRect (10+(x*60), 100, 60+(x*60), 150);
    if (x<4)
      myGLCD.printNumI(x+6, 27+(x*60), 117);
  }
  myGLCD.print("0", 267, 117);
// Draw the lower row of buttons
  myGLCD.setColor(0, 0, 0);
  myGLCD.setColor(255, 255, 255);
  myGLCD.print("Enter Access Code", 20, 10);
  myGLCD.setColor(0, 0, 255);
  myGLCD.fillRoundRect (10, 160, 150, 210);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (10, 160, 150, 210);
  myGLCD.print("Clear", 40, 177);
  myGLCD.setColor(0, 0, 255);
  myGLCD.fillRoundRect (160, 160, 300, 210);
  myGLCD.setColor(255, 255, 255);
  myGLCD.drawRoundRect (160, 160, 300, 210);
  myGLCD.print("Enter", 190, 177);
  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);
  }
}

// Draw a red frame while a button is touched
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);
}

/*************************
**  Required functions  **
*************************/

void setup()
{
// Initial setup
  myGLCD.InitLCD();
  myGLCD.clrScr();

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

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

void loop()
{
  while (true)
  {
    if (myTouch.dataAvailable())
    {
      myTouch.read();
      x=myTouch.getX();
      y=myTouch.getY();
     
      if ((y>=40) && (y<=90))  // Upper row
      {
        if ((x>=10) && (x<=60))  // Button: 1
        {
          waitForIt(10, 40, 60, 90);
          updateStr('1');
        }
        if ((x>=70) && (x<=120))  // Button: 2
        {
          waitForIt(70, 40, 120, 90);
          updateStr('2');
        }
        if ((x>=130) && (x<=180))  // Button: 3
        {
          waitForIt(130, 40, 180, 90);
          updateStr('3');
        }
        if ((x>=190) && (x<=240))  // Button: 4
        {
          waitForIt(190, 40, 240, 90);
          updateStr('4');
        }
        if ((x>=250) && (x<=300))  // Button: 5
        {
          waitForIt(250, 40, 300, 90);
          updateStr('5');
        }
      }

      if ((y>=100) && (y<=150))  // Center row
      {
        if ((x>=10) && (x<=60))  // Button: 6
        {
          waitForIt(10, 100, 60, 150);
          updateStr('6');
        }
        if ((x>=70) && (x<=120))  // Button: 7
        {
          waitForIt(70, 100, 120, 150);
          updateStr('7');
        }
        if ((x>=130) && (x<=180))  // Button: 8
        {
          waitForIt(130, 100, 180, 150);
          updateStr('8');
        }
        if ((x>=190) && (x<=240))  // Button: 9
        {
          waitForIt(190, 100, 240, 150);
          updateStr('9');
        }
        if ((x>=250) && (x<=300))  // Button: 0
        {
          waitForIt(250, 100, 300, 150);
          updateStr('0');
        }
      }

      if ((y>=160) && (y<=210))  // Upper row
      {
        if ((x>=10) && (x<=150))  // Button: Clear
        {
          waitForIt(10, 160, 150, 210);
          stCurrent[0]='\0';
          stCurrentLen=0;
          myGLCD.setColor(0, 0, 0);
          myGLCD.fillRect(0, 224, 319, 239);
        }
        if ((x>=160) && (x<=300))  // Button: Enter
        {
          waitForIt(160, 160, 300, 210);
          if (stCurrent==secretCode)
          {
            myGLCD.setColor(0, 0, 0);
            myGLCD.fillRect(0, 208, 319, 239);
            myGLCD.setColor(0, 255, 0);
            myGLCD.clrScr();
            myGLCD.print("ACCESS GRANTED!", CENTER, 120);
            delay(500);
            myGLCD.print("               ", CENTER, 120);
            delay(500);
            myGLCD.print("ACCESS GRANTED!", CENTER, 120);
            delay(500);
            myGLCD.print("               ", CENTER, 120);
            delay(500);
            myGLCD.print("Welcome Back Jordan!", CENTER, 120);
            delay(1500);
            digitalWrite(lock, HIGH);
            delay (lockOpenTime);
            digitalWrite(lock, LOW);
           
            myGLCD.InitLCD();
            myGLCD.clrScr();

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

            myGLCD.setFont(BigFont);
            myGLCD.setBackColor(0, 0, 255);
            drawButtons(); 
            stCurrentLen=0;
          }
          else
          {
            myGLCD.clrScr();
            myGLCD.setColor(255, 0, 0);
            myGLCD.print("ACCESS DENIED!", CENTER, 120);
            delay(500);
            myGLCD.print("              ", CENTER, 120);
            delay(500);
            myGLCD.print("ACCESS DENIED!", CENTER, 120);
            delay(500);
            myGLCD.print("              ", CENTER, 120);
            delay(500);
            myGLCD.print("ACCESS DENIED!", CENTER, 120);
            delay(1500);
            myGLCD.print("              ", CENTER, 120);
            myGLCD.setColor(0, 255, 0);
                       
            myGLCD.InitLCD();
            myGLCD.clrScr();

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

            myGLCD.setFont(BigFont);
            myGLCD.setBackColor(0, 0, 255);
            drawButtons(); 
            stCurrentLen=0;
           
          }
        }
      }
    }
  }
}

HazardsMind

#1
Jul 05, 2013, 06:56 am Last Edit: Jul 05, 2013, 09:22 pm by HazardsMind Reason: 1
This is my old password sketch, its not perfect but it should give you some idea how to do it. Its not the same library that your using but the library functions are similar.
Now for your problem, "if (stCurrent==secretCode)" this is not the proper way to compare Strings. You need to use strcmp() or my old method. (My new sketch uses strcmp)


Code: [Select]
#include <Wire.h>
#include <ITDB02_Touch.h>
#include <ITDB02_Graph16.h>

// Declare which fonts we will be using
extern uint8_t SmallFont[];
extern uint8_t BigFont[];
//myGLCD(RS,WR,CS,RST,ALE,mode);
ITDB02 myGLCD(A1,A2,A0,A3,A5,ITDB32S);   //
ITDB02_Touch  myTouch(13,10,11,12,A4);
char * text[4][3] = {
  {
    "1","2","3"}
  ,
  {
    "4","5","6"}
  ,
  {
    "7","8","9"}
  ,
  {
    "*","0","#"}
};
char *Pass[4];
char master[5] = "1234";
int Pcount=0;
int PassCounter=0;

void setup()
{
  myGLCD.InitLCD(LANDSCAPE);
  myGLCD.setFont(SmallFont);
  myTouch.InitTouch(LANDSCAPE);
  myTouch.setPrecision(PREC_EXTREME);
  for(int line = 0; line <= 239; line++) //Paints the screen my custom background colors
  {
    myGLCD.setColor(0, 0, line);//text color White 
    myGLCD.drawLine(0, line, 319, line);
  }
  PassWord_makeButton();
  PassWord_buttonText();
  RUN();
}

void loop()
{ }

void RUN()
{
  while(1)
  {
    myTouch.read();
    int tx = myTouch.TP_X;
    int ty = myTouch.TP_Y;
    //tx=round(tx);
    //ty=round(ty);
    //debug(tx,ty);
    int XC= constrain (map(tx, 205, 3900,0,239),0,239); // map to fit my screen range
    int YC= constrain (map(ty, 300, 3850,0,319),0,319); //-------------------------------
    //data(XC,YC);
    GetNum(XC,YC);
  }
}

void PassWord_makeButton()
{
  for(int y = 0; y < 300; y+=100)
  {
    for(int x = 0; x < 200; x+=50)
    {
      drawButton(20+x, 90+y, 50+x, 30+y);
    }
  }
  return;
}

void drawButton(int x1, int y1, int x2, int y2)
{
  myGLCD.setColor(170,170,170);
  myGLCD.fillRoundRect(y1-3, x1+3, y2-3, x2+3);//Rect Shading  myGLCD.fillRoundRect(x1+3, y1+3, x2+3, y2+3); PORTRAIT MODE

  myGLCD.setColor(255, 255, 255);
  myGLCD.fillRoundRect(y1, x1, y2, x2);//Rect

  myGLCD.setColor(200,0,0);
  myGLCD.fillRoundRect(y1-5, x1+5, y2+5, x2-5);//inner rect
  return;
}

void PassWord_buttonText()
{
  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(200, 0, 0);
  myGLCD.setColor(255,255,255);

  for(int tr = 0; tr < 300; tr+=100)
   {
    for(int tc = 0; tc < 200; tc+=50)
     {
      char *myText = text[tc/50][tr/100];
      myGLCD.print(myText,52+tr,27+tc,0);
    }
  }
}

void GetNum(int tx, int ty)
{
  char * lastText;
  for(int tr = 0; tr < 300; tr+=100)
   {
    for(int tc = 0; tc < 200; tc+=50)
     {
      char *myText = text[tc/50][tr/100];
      if(tx>= (20+tc) && ty <= (90+tr) && tx <= (50+tc) && ty >= (30+tr))
       {
        myGLCD.print(myText,200,1,0);
        if(myText != lastText)
        {
          check(myText);
        }
        lastText = myText;
      }
    }
  }
}

void check(char* pass)
{
  myGLCD.setFont(BigFont);
  Pass[Pcount] = pass;
  if(*Pass[Pcount] == master[Pcount])
  {
    PassCounter++;
  }
  //myGLCD.print(*Pass,150,220,0);
  myGLCD.printNumI(Pcount,200,0);
  if(Pcount == 3 && PassCounter == 4)
  {
    myGLCD.print("GOOD",130,0,0);
    delay(1000);
    myGLCD.print("    ",130,0,0);
  }

  if(Pcount == 3 && PassCounter != 4)
  {
    myGLCD.print("FAIL",130,0,0);
    delay(1000);
    myGLCD.print("    ",130,0,0);
  }
  delay(500);
  Pcount++;
  if(Pcount > 3)
  {
    Pcount = 0;
    PassCounter = 0;
  }
}
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

PaulS

I got this far:
Code: [Select]
void loop()
{
  while (true)
  {

When you can explain why you are running an infinite loop inside an infinite loop, I'll read further.

PaulS

Quote
Look again Paul, im not using the loop function.

Look again, yourself. I copied that snippet from an earlier reply, and it most certainly was using an infinite loop in the loop() function.

egeeked

He is talking about my code. It is sample code I pulled from web: http://www.henningkarlsen.com/electronics

If anything is wrong it is because I am new to C++ most of my experience is in web languages (php and jquery).

So if you can offer any other advice on how I could get this working properly I would truly appreciate your help.

Thank you.

egeeked

HazardsMind: Thank you I got the strcmp working properly. Any idea why the

Code: [Select]

            digitalWrite(lock, HIGH);
            delay (lockOpenTime);
            digitalWrite(lock, LOW);


doesn't work?

HazardsMind

Quote
He is talking about my code.

If that's true, then Paul, I apologize. I thought you were talking about my code.

@egeeked
Can you please post your code, or make it an attachment as either a txt or ino file.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

egeeked

I found that it only works when I have both the 12v plug and USB and Serial Monitor On. If I do it with just the plug the passcode detection works but not the unlocking of the lock.

As per your request, I have attached the txt.

HazardsMind

The reason your lock doesn't work is because your missing a very important line of code in your setup function.

pinMode(lock, OUTPUT);

Add this in your setup() and it will work.

Nice layout too. I was able to change your library to mine and got it to work on my screen.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

egeeked

Your amazing. Thank you.

The layout is essentially the default except I moved it down. Added the words on top. and made the granted or denied messages appear on their own screens.

Go Up