Mateus 0 Posted June 26, 2014 Author Share Posted June 26, 2014 Sometimes it works right and sometimes the motor only works if I change the lock/unlock sensor. And now the code is like this: I made a function (trancad) to read just the button *, to lock the door from outside if it is unlocked, and I add more conditions to read the keypad, now the keypad only can be read if the door is locked, except the button * as I said. "if (digitalRead(C2) == LOW && digitalRead(status) == LOW || digitalRead(C0) == LOW && digitalRead(status) == LOW || digitalRead(C1) == LOW && digitalRead(status) == LOW)" /*Controle Porta do Quarto 3.0*//* teclado: S Quote Link to post Share on other sites
abecedarian 330 Posted June 26, 2014 Share Posted June 26, 2014 Just noticed something.... Your first question was regarding using serial. You should switch the jumpers on header to take advantage of hardware serial on the 2553. This does create a conflict with how you are wiring things: const int C0 = 5; // 2 a 4 pinos referentes as colunas do teclado em ordem de signific Mateus 1 Quote Link to post Share on other sites
Mateus 0 Posted June 26, 2014 Author Share Posted June 26, 2014 (edited) Just noticed something.... Your first question was regarding using serial. You should switch the jumpers on header to take advantage of hardware serial on the 2553. This does create a conflict with how you are wiring things: const int C0 = 5; // 2 a 4 pinos referentes as colunas do teclado em ordem de signific Edited June 26, 2014 by Mateus Quote Link to post Share on other sites
abecedarian 330 Posted June 26, 2014 Share Posted June 26, 2014 Shouldn't I change the J3 jumpers only to use UART communication? Because my code is working fine now, and the problems I had are not about this, by the way I changed the names of the columns and rows pins to match the significance sequence.If it's working with UART / Serial, then I'm not sure what to say, but I don't see any "Serial" code in what you've posted so far. Pin 3 is connected to the emulation side of J3; pin 4 is connected to the MSP430 side of J3. Mateus 1 Quote Link to post Share on other sites
Mateus 0 Posted June 26, 2014 Author Share Posted June 26, 2014 If it's working with UART / Serial, then I'm not sure what to say, but I don't see any "Serial" code in what you've posted so far. Pin 3 is connected to the emulation side of J3; pin 4 is connected to the MSP430 side of J3. I think you know much more than me about this, but my code is working and the msp is receiving the correct password. So I think I'll let this the way it is. If I have more problems, I'll read more about this, because at energia's web page there are no details. Thank you a lot Quote Link to post Share on other sites
Mateus 0 Posted June 28, 2014 Author Share Posted June 28, 2014 It is almost done, the only problem is that some times when I turn on the project, everything works, but the motor including the led wich turns on when any button is read. Then if I change the position of the switch in the pin switch that is a sensor of locked/unlocked door, the motor starts working when requested. Does anybody has an idea that what can be happening? Quote Link to post Share on other sites
abecedarian 330 Posted June 30, 2014 Share Posted June 30, 2014 First thing I would suggest is to initialize your variable: char senha[3] = {'x', 'x', 'x'}; Mateus 1 Quote Link to post Share on other sites
abecedarian 330 Posted July 7, 2014 Share Posted July 7, 2014 @@Mateus - Here's a (fairly) simple door lock. I didn't include many comments because I think the code is fairly self-explanatory. The basics of how it works: - set up pins and the keyboard matrix key characters - cycle the door lock mechanism - check the door status (open / closed) and leaves the lock unlocked/locked as necessary - poll the keypad waiting for a key press and checks the door status if the door is locked - respond to 4 keys: 1 = unlock door; 2 = check door and lock status; 3 = lock door; and * = door bell - if door is closed and locked, opening the door triggers an 'alarm' - uses hardware serial to display messages on a terminal window in a connected computer Password handling is not implemented however key presses are handled as a 'char', representing the key character of the key pressed, which should make it easy to implement passwords. Serial use, as mentioned earlier, requires pins 3 and 4 so this means no separate interior door unlock button. Wiring two keypads in parallel, one interior / one exterior, and requiring the password to unlock the door from either side is an option. Another option could be using an I2C I/O expander. Also, I've defined the functions to take an optional parameter, and a constant called "silent". If you call the functions without a parameter, they display messages over serial, but if you include "silent" as a parameter, this suppresses such messages. This only affects the functions though, not what's sent over serial in setup. For example, calling "getDoorStatus(silent)" calls the function and sets global variables accordingly but does not send anything over serial. Good luck! // Assign keyboard GPIO pins: const int keypadColumn [] = {6,5,2}; const int keypadRow [] = {10,9,8,7}; // Configure matrix keypad character map: this is 3x4 matrix. const char keypadChar[][4] = { {'1','2','3'}, {'4','5','6'}, {'7','8','9'}, {'*','0','#'} }; // Assign GPIO pins to L293D: const int motorEnable = 19; // L293D pin 1 const int motorPinOne = 13; // L293D pin 3 const int motorPinTwo = 15; // L293D pin 7 const int ledUnlocked = 11; // L293D pin 14 const int ledLocked = 12; // L293D pin 11 // Assign pins for door and internal exit switch status and buzzer: const int doorSwitch = 18; // pin switch grounds when door is closed const int buzzer = 14; // drive led or buzzer like doorbell // Variables: int doorIsClosed = 0; // 0 = open, 1 = closed int doorIsLocked = 0; // 0 = unlocked, 1 = locked // door / lock status message 'end of sentences': const char doorStatus [][8] = { "open.", "closed."}; const char lockStatus [][10] = { "unlocked." , "locked."}; // Functions: const int silent = 0; // may used to suppress messages from some functions. For instance, // getDoorStatus(silent) will not show messages to serial terminal. void getDoorStatus(int verbose = 1) { // read door switch: high = closed, low = open doorIsClosed = digitalRead(doorSwitch); if (verbose) { Serial.println("\nChecking door status...."); Serial.print("Door is "); Serial.println(doorStatus[doorIsClosed]); Serial.print("Door is "); Serial.println(lockStatus[doorIsLocked]); } } void lockDoor(int verbose = 1) { digitalWrite(motorEnable, LOW); // ensure motor is disabled digitalWrite(motorPinOne, HIGH); // set motor direction to lock digitalWrite(motorEnable, HIGH); // enable motor delay(500); // wait 1/2 second digitalWrite(motorEnable, LOW); // disable motor digitalWrite(motorPinOne, LOW); // pull motor winding to ground doorIsLocked = 1; digitalWrite(ledLocked, doorIsLocked); digitalWrite(ledUnlocked, !doorIsLocked); if (verbose) Serial.println("Door is locked."); } void unlockDoor(int verbose = 1) { digitalWrite(motorEnable, LOW); // ensure motor is disabled digitalWrite(motorPinTwo, HIGH); // set motor direction to unlock digitalWrite(motorEnable, HIGH); // enable motor delay(500); // wait 1/2 second digitalWrite(motorEnable, LOW); // disable motor digitalWrite(motorPinTwo, LOW); // pull motor winding to ground doorIsLocked = 0; digitalWrite(ledLocked, doorIsLocked); digitalWrite(ledUnlocked, !doorIsLocked); if (verbose) Serial.println("Door is unlocked."); } char getKeypress(int verbose = 1) { int stat = 0; // variable to store door state for alarm purposes if (verbose) Serial.print("\nWaiting for key press: "); char keypress = 0; while (!keypress) { for (int c = 0; c < 3; c ++){ digitalWrite(keypadColumn[c], HIGH); for (int r = 0; r < 4; r ++) { if (digitalRead(keypadRow[r])) { keypress = keypadChar[r][c]; } } digitalWrite(keypadColumn[c], LOW); stat = doorIsClosed; getDoorStatus(silent); if ((doorIsClosed) && (stat != doorIsClosed)) lockDoor(); if ((!doorIsClosed) && (doorIsLocked)) soundAlarm(); } } Serial.println(keypress); return keypress; } void requestEntry(int verbose = 1) { if (verbose) Serial.println("\nUser requesting entry."); // toggle buzzer / led three times for (int i = 0; i < 3; i++) { digitalWrite(buzzer, HIGH); delay(500); digitalWrite(buzzer, LOW); delay(500); } } void soundAlarm() { Serial.println("\n*** ALARM ***"); digitalWrite(buzzer, HIGH); delay(50); digitalWrite(buzzer, LOW); delay(100); } void setup() { /* Setup serial for debug / status messages: */ Serial.begin(9600); delay(1000); Serial.println("\n\n***************\nStarting Up....\n***************"); /* Configure door and related devices: */ pinMode(motorEnable, OUTPUT); digitalWrite(motorEnable, LOW); pinMode(motorPinOne, OUTPUT); pinMode(motorPinTwo, OUTPUT); digitalWrite(motorPinOne, LOW); digitalWrite(motorPinTwo, LOW); pinMode(doorSwitch, INPUT_PULLUP); pinMode(ledLocked, OUTPUT); pinMode(ledUnlocked, OUTPUT); /* Configure keypad column and row pins for I/O: */ for (int c=0; c<3; c++) { // cycle through 3 columns pinMode(keypadColumn[c], OUTPUT); // set to output digitalWrite(keypadColumn[c], LOW); // set output "low" }; for (int r=0; r<4 ; r++) { // cycle through 3 rows pinMode(keypadRow[r], INPUT_PULLDOWN); }; /* Configure other pins: */ pinMode(buzzer, OUTPUT); digitalWrite(buzzer, LOW); // verify buzzer is low to prevent annoying issues /* The next parts are for power on or reset. */ // Cycle door lock: Serial.println("Cycling door lock."); lockDoor(); unlockDoor(); // Check door. If closed, it will be locked. getDoorStatus(); if (digitalRead(doorSwitch)) lockDoor(); // verify lock LED's are set correctly: digitalWrite(ledLocked, doorIsLocked); digitalWrite(ledUnlocked, !doorIsLocked); Serial.println("\n\n**********************"); Serial.println("Startup complete."); Serial.println("Entering main program."); Serial.println("**********************\n\n"); } void loop() { int key = 0; // variable to hold individual key presses while (1){ // delay(750); // delay to prevent rapid, repeating key presses key=getKeypress(); // get a key press if (key == '*') requestEntry(); // rings doorbell if (key == '1') unlockDoor(); // unlock the door if (key == '2') getDoorStatus(); // check door status if (key == '3') lockDoor(); // lock the door }; } Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.