Hallo,
Ik ben bezig met de deur van mijn slaapkamer te voorzien van een elektronisch deurslot via een servomotor en 3D-geprinte onderdelen voor het slot.
Ik heb een arduino nano met daaraan gekoppeld een HC-05 bluetooth module en een MFRC522 RFID lezer.
Ik heb beide apart getest en ik kan het slot openen en sluiten.
Maar als ik beide codes bij elkaar voeg, lukt het niet.
Kan er iemand naar mijn code kijken wat er fout is en verbeteren? Ik heb al een topic gemaakt in het Engelstalig gedeelte, maar blijkbaar heeft niemand zin daar om er naar te kijken.
Dit is mijn code:
#include <SPI.h>
#include <SoftwareSerial.h>
#include <MFRC522.h>
#include <Servo.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
String read_rfid;
String ok_rfid_1="909987ab"; //add as many as you need.
String ok_rfid_2="85aaba5";
String ok_rfid_3="13d4b228";
String ok_rfid_4="9ab7b97f";
String ok_rfid_5="33578aa7";
String ok_rfid_6="b2bc71d";
Servo myservo; // create servo object to control a servo
int posClosed = 0; // variable to store the servo position for locked
int posOpen = 360; //same for open...
int bluetoothTx = 0;
int bluetoothRx = 1;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
/*
* Initialize.
*/
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
bluetooth.begin(9600);
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
myservo.attach(3); // attaches the servo on pin 3 to the servo object
}
/*
* Helper routine to dump a byte array as hex values to Serial.
*/
void dump_byte_array(byte *buffer, byte bufferSize) {
read_rfid="";
for (byte i = 0; i < bufferSize; i++) {
read_rfid=read_rfid + String(buffer[i], HEX);
}
}
void open_lock() {
//Use this routine when working with Servos.
myservo.write(90);
delay(2000);
myservo.write(0);
}
void loop() {
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()){
//Read from bluetooth and write to usb serial
if(bluetooth.available()> 0 ) // receive number from bluetooth
{
int servopos = bluetooth.read(); // save the received number to servopos
Serial.println(servopos); // serial print servopos current number received from bluetooth
myservo.write(servopos); // roate the servo the angle received from the android app
}}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
return;
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println(read_rfid);
if ((read_rfid==ok_rfid_1)||(read_rfid==ok_rfid_2)||(read_rfid==ok_rfid_3)||(read_rfid==ok_rfid_4)||(read_rfid==ok_rfid_5)||(read_rfid==ok_rfid_6)) {
open_lock();
}}
Mijn tweede versie:
#include <SPI.h>
#include <SoftwareSerial.h>
#include <MFRC522.h>
#include <Servo.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
String read_rfid;
String ok_rfid_1="909987ab"; //add as many as you need.
String ok_rfid_2="85aaba5";
String ok_rfid_3="13d4b228";
String ok_rfid_4="9ab7b97f";
String ok_rfid_5="33578aa7";
String ok_rfid_6="b2bc71d";
Servo myservo; // create servo object to control a servo
int posClosed = 0; // variable to store the servo position for locked
int posOpen = 360; //same for open...
int bluetoothTx = 0; // bluetooth tx to 0 pin
int bluetoothRx = 1; // bluetooth rx to 1 pin
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
/*
* Initialize.
*/
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
bluetooth.begin(9600);
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
myservo.attach(3); // attaches the servo on pin 3 to the servo object
}
/*
* Helper routine to dump a byte array as hex values to Serial.
*/
void dump_byte_array(byte *buffer, byte bufferSize) {
read_rfid="";
for (byte i = 0; i < bufferSize; i++) {
read_rfid=read_rfid + String(buffer[i], HEX);
}
}
void open_lock() {
//Use this routine when working with Servos.
myservo.write(90);
delay(2000);
myservo.write(0);
}
void loop() {
// Look for new cards
bool flag = false;
if (mfrc522.PICC_IsNewCardPresent())
{
// Select one of the cards
if (mfrc522.PICC_ReadCardSerial())
{
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println(read_rfid);
if ((read_rfid==ok_rfid_1)||(read_rfid==ok_rfid_2)||(read_rfid==ok_rfid_3)||(read_rfid==ok_rfid_4)||(read_rfid==ok_rfid_5)||(read_rfid==ok_rfid_6)) {
flag=true;
}
if (!flag)
//Read from bluetooth and write to usb serial
if(bluetooth.available()> 0 ) // receive number from bluetooth
{
int servopos = bluetooth.read(); // save the received number to servopos
Serial.println(servopos); // serial print servopos current number received from bluetooth
myservo.write(servopos); // roate the servo the angle received from the android app
}}
Wil er iemand aub naar kijken en mij helpen dit op te lossen.
Het is erg frustrerend aangezien ik al alle hardware klaar liggen heb en het zelfs afzonderlijk werkt. Het is nu al paar weken dat ik ermee zit.