SHARE
TWEET

Spark Door for Spark Web IDE

Dragonsshout Jan 23rd, 2014 1,545 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  ******************************************************************************
  3.  * @file    door.ino
  4.  * @authors  Dragonsshout
  5.  * @version V1.1.0
  6.  * @date    22-January-2014
  7.  * @brief   Spark Door
  8.  ******************************************************************************/
  9.  
  10. /* Defines -------------------------------------------------------------------*/
  11. #define NBTAG 2
  12. #define TAGNUMBER 14
  13. #define LED D7
  14. #define RELAY1 D0
  15.  
  16. /* Function prototypes -------------------------------------------------------*/
  17. int openDoor(String command);
  18. bool comparetag(int tag1[TAGNUMBER], int tag2[TAGNUMBER]);
  19. int checkmytags(int newtag[TAGNUMBER]);
  20. void readTags();
  21.  
  22. /* Variables -----------------------------------------------------------------*/
  23. int tag[NBTAG][TAGNUMBER] = {
  24.   {1,2,3,4,5,6,7,8,9,10,11,12,13,14},
  25.   {14,13,12,11,10,9,8,7,6,5,4,3,2,1}
  26. };
  27.  
  28. /* This function is called once at start up ----------------------------------*/
  29. void setup() {
  30.     pinMode(LED, OUTPUT);
  31.     pinMode(RELAY1, OUTPUT);
  32.    
  33.     //Serial.begin(9600);
  34.     Serial1.begin(9600);  
  35.    
  36.     Spark.function("openDoor", openDoor);
  37.  
  38.     //digitalWrite(LED, HIGH);
  39. }
  40.  
  41. /* This function loops forever --------------------------------------------*/
  42. void loop() {
  43.     readTags();
  44.     delay(200); // needed to allow time for the data to come in from the serial buffer.
  45. }
  46.  
  47. /*******************************************************************************
  48.  * Function Name  : readTags
  49.  * Description    : Read tags over serial 1
  50.  * Input          : None.
  51.  * Output         : None.
  52.  * Return         : None.
  53.  *******************************************************************************/
  54. void readTags()
  55. {
  56.   int data = 0;
  57.   int newtag[TAGNUMBER] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // used for read comparisons
  58.   int ok = -1; // this variable helps decision-making, if it is 1 we have a match, zero is a read but no match, -1 is no read attempt made
  59.  
  60.   if (Serial1.available() > 0)
  61.   {
  62.     data = 0;
  63.     delay(100); // needed to allow time for the data to come in from the serial buffer.
  64.    
  65.     //Serial.println("Data: ");
  66.     for (int i = 0 ; i < TAGNUMBER ; i++) // read tag
  67.     {
  68.       data = Serial1.read();
  69.       //Serial.print(data, DEC);
  70.       //Serial.print(",");
  71.       newtag[i] = data;
  72.     }
  73.    
  74.     // do the tags match up?
  75.     ok = checkmytags(newtag);
  76.   }
  77.  
  78.   // now do something based on tag type
  79.   if (ok > 0) // if we had a match
  80.   {
  81.         //Serial.println(" -> Accepted");
  82.         RGB.control(true);
  83.     RGB.color(0, 50, 0);
  84.     openDoor("");
  85.     RGB.color(0, 0, 0);
  86.     RGB.control(false);
  87.    
  88.     while (Serial1.available() > 0) Serial1.read(); // stops multiple reads
  89.   }
  90.   else if (ok == 0) // if we didn't have a match
  91.   {
  92.         //Serial.println(" -> Rejected");
  93.         RGB.control(true);
  94.     RGB.color(50, 0, 0);
  95.     delay(1000);
  96.     RGB.color(0, 0, 0);
  97.     RGB.control(false);
  98.    
  99.     while (Serial1.available() > 0) Serial1.read(); // stops multiple reads
  100.   }
  101. }
  102.  
  103. /*******************************************************************************
  104.  * Function Name  : openDoor
  105.  * Description    : Open door with relay
  106.  * Input          : int command
  107.  * Output         : None.
  108.  * Return         : int
  109.  *******************************************************************************/
  110. int openDoor(String command)
  111. {
  112.   digitalWrite(RELAY1, HIGH);
  113.   delay(2500);
  114.   digitalWrite(RELAY1, LOW);
  115.  
  116.   return 200;
  117. }
  118.  
  119. /*******************************************************************************
  120.  * Function Name  : checkmytags
  121.  * Description    : Compares each tag against the tag just read
  122.  * Input          : int newtag[14]
  123.  * Output         : None.
  124.  * Return         : int
  125.  *******************************************************************************/
  126. int checkmytags(int newtag[TAGNUMBER])
  127. {
  128.   int ok = 0;
  129.  
  130.   for (int i = 0 ; i < NBTAG ; i++)
  131.   {
  132.     if (comparetag(newtag, tag[i]) == true)
  133.     {
  134.       ok++;
  135.     }
  136.   }
  137.   return ok;
  138. }
  139.  
  140. /*******************************************************************************
  141.  * Function Name  : comparetag
  142.  * Description    : Compare 2 tags
  143.  * Input          : int tag1[14], int tag2[14]
  144.  * Output         : None.
  145.  * Return         : bool
  146.  *******************************************************************************/
  147. bool comparetag(int tag1[TAGNUMBER], int tag2[TAGNUMBER])
  148. {
  149.   int count = 0;
  150.   for (int i = 0 ; i < TAGNUMBER ; i++)
  151.   {
  152.     if (tag1[i] == tag2[i])
  153.     {
  154.       count++;
  155.     }
  156.   }
  157.   if (count == TAGNUMBER)
  158.   {
  159.     return true;
  160.   }
  161.   return false;
  162. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top