Hi All
Ill try to make a long story short. I'm turning my basement into another living space/play area and I wanted to have a hidden door for my future work area/utility room. I have the door design all planed out but need some coding and wiring help. After searching the internet for weeks I realize in order to make the door work the way I want it to (and not spend $1000's on it) I need to make the lock my self. I remember on Adafruit's website plans for Biometric Lock Box (I made this once and still have the parts) and thought this would work perfectly if I come get it to activate a stepper motor instead of a solenoid. This is where I need help I spent the last few nights trying to figure out how to modify the code to work with Adafruit Motor/Stepper/Servo Shield for Arduino v2 Kit - v2.0 and the Stepper motor - NEMA-17 size - 200 steps/rev, 12V 350mA and could figure out what need to be modified to get it to work and I'm not sure if its even possible.
Programing/Wiring Help
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- adafruit_support_bill
- Posts: 88150
- Joined: Sat Feb 07, 2009 10:11 am
Re: Programing/Wiring Help
What have you got so far?
The first thing to do is to test the shield and motor with the example code from the library to make sure it is working.
Then you need to add the library includes and declarations to the lock-box code.
Changes to the lock-box code would be mostly in the SetLockState() function. You would need to replace the digital write calls with step calls. But first you will need to figure out how many steps in each direction it takes to lock and unlock your door. Then add calls to the 'step' function - with different step directions depending on whether you are locking or unlocking the door.
The first thing to do is to test the shield and motor with the example code from the library to make sure it is working.
Then you need to add the library includes and declarations to the lock-box code.
Changes to the lock-box code would be mostly in the SetLockState() function. You would need to replace the digital write calls with step calls. But first you will need to figure out how many steps in each direction it takes to lock and unlock your door. Then add calls to the 'step' function - with different step directions depending on whether you are locking or unlocking the door.
-
- Posts: 12
- Joined: Wed Jul 17, 2013 7:29 pm
Re: Programing/Wiring Help
Hi Billadafruit_support_bill wrote:What have you got so far?
The first thing to do is to test the shield and motor with the example code from the library to make sure it is working.
Then you need to add the library includes and declarations to the lock-box code.
Changes to the lock-box code would be mostly in the SetLockState() function. You would need to replace the digital write calls with step calls. But first you will need to figure out how many steps in each direction it takes to lock and unlock your door. Then add calls to the 'step' function - with different step directions depending on whether you are locking or unlocking the door.
Thanks for getting back to me. I got the motor work with the example code, as far as the code for the door, its code I got from the Biometric Box page. I added the libraries to the code and was going to change out the solenoid with the stepper motor code, but then realize it maybe harder then I thought because the solenoid connects to the button and not sure how to get the button to work with the stepper motor. I can see where the solenoid is in the code but not sure how to change it. As far how many step it will need, I will need to test it once its in place because don't know how many rotations the wheel need to make to move the peg in and out yet.
Code: Select all
/***************************************************
Biometric Box Sketch for the optical Fingerprint sensor
This sketch implements a two-level security scheme requiring the
user to enter a passcode via the keypad before scanning a fingerprint
for access.
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Bill Earl for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#include <Keypad.h>
#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Define the states for the lock state machine
#define LOCKED 2
#define PASSWORD_OK 1
#define UNLOCKED 0
// State Variables: Initialize to the locked state
int LockState = LOCKED;
long StartTime = 0;
int position = 0;
// Define your password key sequence here
char* secretCode = "1423";
// Keypad key matrix:
const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] =
{
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
// Keypad pin definitions
byte rowPins[rows] = {2, 3, 4, 5};
byte colPins[cols] = {6, 7, 8};
// Instantiate the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
// More pin definitions:
int LedPin = 10;
int SolenoidPin = 11;
// Define a Fingerprint sensor on pins 12 & 13
int getFingerprintIDez();
SoftwareSerial mySerial(13, 12);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
void setup()
{
pinMode(LedPin, OUTPUT);
pinMode(SolenoidPin, OUTPUT);
// Flash hello
for (int i = 0; i < 3; i++)
{
digitalWrite(LedPin, HIGH);
delay(100);
digitalWrite(LedPin, LOW);
delay(100);
}
// Initialize state and communicatins
setLockState(LOCKED);
Serial.begin(9600);
finger.begin(57600);
// Connect to the sensor
if (finger.verifyPassword())
{
Serial.println("Found fingerprint sensor!");
}
else
{
Serial.println("Did not find fingerprint sensor :(");
while (1);
}
}
void loop()
{
// Run the state machine:
// Locked State - Monitor keypad for valid Password code entry
if (LockState == LOCKED)
{
char key = keypad.getKey();
if (key == '*' || key == '#')
{
position = 0;
setLockState(LOCKED);
}
if (key != 0)
{
if (key == secretCode[position]) // Valid key in Password sequence
{
Serial.print("Matched ");
Serial.print(key);
Serial.print("-at-");
Serial.println(position);
position ++;
}
else // Invalid key - start all over again
{
Serial.println("Invalid Code!");
position = 0;
}
}
// Let the LED 'breathe' while we wait
analogWrite(LedPin, sin((millis() % 3142) / 1000.0) * 255);
if (position == 4) // Password successfully entered - advance state
{
setLockState(PASSWORD_OK);
position = 0;
}
delay(100);
}
// PASSWORD_OK state - Now wait for a valid fingerprint reading
else if (LockState == PASSWORD_OK)
{
if (getFingerprintIDez() != -1)
{
setLockState(UNLOCKED); // Valid fingerprint - advance state to UNLOCKED
}
if (millis () - StartTime > 5000)
{
setLockState (LOCKED); // Time-out - go back to the LOCKED state
}
}
// UNLOCKED state - hold the solenoid open for a limited time
else if (LockState == UNLOCKED)
{
for (int i = 0; i < 30; i++)
{
// Flash the led to indicate the lock is open
digitalWrite(LedPin, LOW);
delay(50);
digitalWrite(LedPin, HIGH);
delay(50);
}
setLockState (LOCKED); // Time-out - go back to the locked state.
}
}
// returns -1 if failed, otherwise returns ID #
int getFingerprintIDez()
{
uint8_t p = finger.getImage();
if (p != FINGERPRINT_OK) return -1;
p = finger.image2Tz();
if (p != FINGERPRINT_OK) return -1;
p = finger.fingerFastSearch();
if (p != FINGERPRINT_OK) return -1;
// found a match!
Serial.print("Found ID #"); Serial.print(finger.fingerID);
Serial.print(" with confidence of "); Serial.println(finger.confidence);
return finger.fingerID;
}
// Set the state and the time of the state change
void setLockState(int state)
{
LockState = state;
StartTime = millis ();
if (state == LOCKED)
{
Serial.println("Locked!");
digitalWrite(LedPin, HIGH);
digitalWrite(SolenoidPin, LOW);
}
else if (state == PASSWORD_OK)
{
Serial.println("PASSWORD_OK!");
digitalWrite(LedPin, LOW);
}
else if (state == UNLOCKED)
{
Serial.println("Unlocked!");
digitalWrite(LedPin, LOW);
digitalWrite(SolenoidPin, HIGH);
}
}
- adafruit_support_bill
- Posts: 88150
- Joined: Sat Feb 07, 2009 10:11 am
Re: Programing/Wiring Help
There is no connection between the button and the solenoid. The solenoid is controlled by pin 11 and all the code for the solenoid is contained in the setLockState function.
If you replace the solenoid code with stepper code, the program will call it to unlock the door once the pass-code and fingerprint are verified.
If you replace the solenoid code with stepper code, the program will call it to unlock the door once the pass-code and fingerprint are verified.
Code: Select all
// Set the state and the time of the state change
void setLockState(int state)
{
LockState = state;
StartTime = millis ();
if (state == LOCKED)
{
Serial.println("Locked!");
digitalWrite(LedPin, HIGH);
digitalWrite(SolenoidPin, LOW);
}
else if (state == PASSWORD_OK)
{
Serial.println("PASSWORD_OK!");
digitalWrite(LedPin, LOW);
}
else if (state == UNLOCKED)
{
Serial.println("Unlocked!");
digitalWrite(LedPin, LOW);
digitalWrite(SolenoidPin, HIGH);
}
}
-
- Posts: 12
- Joined: Wed Jul 17, 2013 7:29 pm
Re: Programing/Wiring Help
Not sure if this is right? Also I wasn't sure were the line of code to tell it what speed to use, would it go right before it? should i move the step line down one and put the speed above it?
Also I saw that the solenoid was in this part of the code. I'm not sure what this line here is for? :
Code: Select all
/***************************************************
Biometric Box Sketch for the optical Fingerprint sensor
This sketch implements a two-level security scheme requiring the
user to enter a passcode via the keypad before scanning a fingerprint
for access.
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Bill Earl for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#include <Keypad.h>
#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
// Define the states for the lock state machine
#define LOCKED 2
#define PASSWORD_OK 1
#define UNLOCKED 0
// State Variables: Initialize to the locked state
int LockState = LOCKED;
long StartTime = 0;
int position = 0;
// Define your password key sequence here
char* secretCode = "1423";
// Keypad key matrix:
const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] =
{
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
// Keypad pin definitions
byte rowPins[rows] = {2, 3, 4, 5};
byte colPins[cols] = {6, 7, 8};
// Instantiate the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
// More pin definitions:
int LedPin = 10;
int SolenoidPin = 11;
// Define a Fingerprint sensor on pins 12 & 13
int getFingerprintIDez();
SoftwareSerial mySerial(13, 12);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
void setup()
{
pinMode(LedPin, OUTPUT);
pinMode(SolenoidPin, OUTPUT);
// Flash hello
for (int i = 0; i < 3; i++)
{
digitalWrite(LedPin, HIGH);
delay(100);
digitalWrite(LedPin, LOW);
delay(100);
}
// Initialize state and communicatins
setLockState(LOCKED);
Serial.begin(9600);
finger.begin(57600);
// Connect to the sensor
if (finger.verifyPassword())
{
Serial.println("Found fingerprint sensor!");
}
else
{
Serial.println("Did not find fingerprint sensor :(");
while (1);
}
}
void loop()
{
// Run the state machine:
// Locked State - Monitor keypad for valid Password code entry
if (LockState == LOCKED)
{
char key = keypad.getKey();
if (key == '*' || key == '#')
{
position = 0;
setLockState(LOCKED);
}
if (key != 0)
{
if (key == secretCode[position]) // Valid key in Password sequence
{
Serial.print("Matched ");
Serial.print(key);
Serial.print("-at-");
Serial.println(position);
position ++;
}
else // Invalid key - start all over again
{
Serial.println("Invalid Code!");
position = 0;
}
}
// Let the LED 'breathe' while we wait
analogWrite(LedPin, sin((millis() % 3142) / 1000.0) * 255);
if (position == 4) // Password successfully entered - advance state
{
setLockState(PASSWORD_OK);
position = 0;
}
delay(100);
}
// PASSWORD_OK state - Now wait for a valid fingerprint reading
else if (LockState == PASSWORD_OK)
{
if (getFingerprintIDez() != -1)
{
setLockState(UNLOCKED); // Valid fingerprint - advance state to UNLOCKED
}
if (millis () - StartTime > 5000)
{
setLockState (LOCKED); // Time-out - go back to the LOCKED state
}
}
// UNLOCKED state - hold the solenoid open for a limited time
else if (LockState == UNLOCKED)
{
for (int i = 0; i < 30; i++)
{
// Flash the led to indicate the lock is open
digitalWrite(LedPin, LOW);
delay(50);
digitalWrite(LedPin, HIGH);
delay(50);
}
setLockState (LOCKED); // Time-out - go back to the locked state.
}
}
// returns -1 if failed, otherwise returns ID #
int getFingerprintIDez()
{
uint8_t p = finger.getImage();
if (p != FINGERPRINT_OK) return -1;
p = finger.image2Tz();
if (p != FINGERPRINT_OK) return -1;
p = finger.fingerFastSearch();
if (p != FINGERPRINT_OK) return -1;
// found a match!
Serial.print("Found ID #"); Serial.print(finger.fingerID);
Serial.print(" with confidence of "); Serial.println(finger.confidence);
return finger.fingerID;
}
// Set the state and the time of the state change
void setLockState(int state)
{
LockState = state;
StartTime = millis ();
if (state == LOCKED)
{
Serial.println("Locked!");
digitalWrite(LedPin, HIGH);
myMotor->step(150, FORWARD, INTERLEAVE);
}
else if (state == PASSWORD_OK)
{
Serial.println("PASSWORD_OK!");
digitalWrite(LedPin, LOW);
}
else if (state == UNLOCKED)
{
Serial.println("Unlocked!");
digitalWrite(LedPin, LOW);
myMotor->step(150, BACKWARD, INTERLEAVE);
}
}
Code: Select all
// Define a Fingerprint sensor on pins 12 & 13
int getFingerprintIDez();
SoftwareSerial mySerial(13, 12);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
void setup()
{
pinMode(LedPin, OUTPUT);
pinMode(SolenoidPin, OUTPUT);
// Flash hello
for (int i = 0; i < 3; i++)
{
digitalWrite(LedPin, HIGH);
delay(100);
digitalWrite(LedPin, LOW);
delay(100);
}
// Initialize state and communicatins
setLockState(LOCKED);
Serial.begin(9600);
finger.begin(57600);
- adafruit_support_bill
- Posts: 88150
- Joined: Sat Feb 07, 2009 10:11 am
Re: Programing/Wiring Help
You'll have to experiment with the speed to see what works for your lock. Higher speeds mean less available torque. You can put the speed setting just before the call to step.
That is just setting the pin to output mode.Also I saw that the solenoid was in this part of the code. I'm not sure what this line here is for? :
Please be positive and constructive with your questions and comments.