Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- ******************************************************************************
- * @file door.ino
- * @authors Dragonsshout
- * @version V1.1.0
- * @date 22-January-2014
- * @brief Spark Door
- ******************************************************************************/
- /* Defines -------------------------------------------------------------------*/
- #define NBTAG 2
- #define TAGNUMBER 14
- #define LED D7
- #define RELAY1 D0
- /* Function prototypes -------------------------------------------------------*/
- int openDoor(String command);
- bool comparetag(int tag1[TAGNUMBER], int tag2[TAGNUMBER]);
- int checkmytags(int newtag[TAGNUMBER]);
- void readTags();
- /* Variables -----------------------------------------------------------------*/
- int tag[NBTAG][TAGNUMBER] = {
- {1,2,3,4,5,6,7,8,9,10,11,12,13,14},
- {14,13,12,11,10,9,8,7,6,5,4,3,2,1}
- };
- /* This function is called once at start up ----------------------------------*/
- void setup() {
- pinMode(LED, OUTPUT);
- pinMode(RELAY1, OUTPUT);
- //Serial.begin(9600);
- Serial1.begin(9600);
- Spark.function("openDoor", openDoor);
- //digitalWrite(LED, HIGH);
- }
- /* This function loops forever --------------------------------------------*/
- void loop() {
- readTags();
- delay(200); // needed to allow time for the data to come in from the serial buffer.
- }
- /*******************************************************************************
- * Function Name : readTags
- * Description : Read tags over serial 1
- * Input : None.
- * Output : None.
- * Return : None.
- *******************************************************************************/
- void readTags()
- {
- int data = 0;
- int newtag[TAGNUMBER] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // used for read comparisons
- 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
- if (Serial1.available() > 0)
- {
- data = 0;
- delay(100); // needed to allow time for the data to come in from the serial buffer.
- //Serial.println("Data: ");
- for (int i = 0 ; i < TAGNUMBER ; i++) // read tag
- {
- data = Serial1.read();
- //Serial.print(data, DEC);
- //Serial.print(",");
- newtag[i] = data;
- }
- // do the tags match up?
- ok = checkmytags(newtag);
- }
- // now do something based on tag type
- if (ok > 0) // if we had a match
- {
- //Serial.println(" -> Accepted");
- RGB.control(true);
- RGB.color(0, 50, 0);
- openDoor("");
- RGB.color(0, 0, 0);
- RGB.control(false);
- while (Serial1.available() > 0) Serial1.read(); // stops multiple reads
- }
- else if (ok == 0) // if we didn't have a match
- {
- //Serial.println(" -> Rejected");
- RGB.control(true);
- RGB.color(50, 0, 0);
- delay(1000);
- RGB.color(0, 0, 0);
- RGB.control(false);
- while (Serial1.available() > 0) Serial1.read(); // stops multiple reads
- }
- }
- /*******************************************************************************
- * Function Name : openDoor
- * Description : Open door with relay
- * Input : int command
- * Output : None.
- * Return : int
- *******************************************************************************/
- int openDoor(String command)
- {
- digitalWrite(RELAY1, HIGH);
- delay(2500);
- digitalWrite(RELAY1, LOW);
- return 200;
- }
- /*******************************************************************************
- * Function Name : checkmytags
- * Description : Compares each tag against the tag just read
- * Input : int newtag[14]
- * Output : None.
- * Return : int
- *******************************************************************************/
- int checkmytags(int newtag[TAGNUMBER])
- {
- int ok = 0;
- for (int i = 0 ; i < NBTAG ; i++)
- {
- if (comparetag(newtag, tag[i]) == true)
- {
- ok++;
- }
- }
- return ok;
- }
- /*******************************************************************************
- * Function Name : comparetag
- * Description : Compare 2 tags
- * Input : int tag1[14], int tag2[14]
- * Output : None.
- * Return : bool
- *******************************************************************************/
- bool comparetag(int tag1[TAGNUMBER], int tag2[TAGNUMBER])
- {
- int count = 0;
- for (int i = 0 ; i < TAGNUMBER ; i++)
- {
- if (tag1[i] == tag2[i])
- {
- count++;
- }
- }
- if (count == TAGNUMBER)
- {
- return true;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement