Hi, so i'm making a RFID Door, with an XBEE shield and a NFC module. When a card shows up at the door, the servo turns, but wont do anything else after it, while it should close again after 5 seconds
#include <Servo.h>
byte data[5]; //For holding the ID we receive
int val = 0;
byte Marius[5] = {0x0D,0x00,0x58,0xF5,0xB7};
byte Gast[5] = {0x0A,0x00,0x8B,0x9A,0xD9};
int servopin = 11;
int pos = 0;
//Create Servo Object
Servo voordeur;
void setup()
{
voordeur.attach(servopin);
Serial.begin(19200);
Serial.write(0xFF); //Header
Serial.write(0x01); //Reserved
Serial.write(0x09); //Length (Command + Data)
Serial.write(0x87); //Command (0x87 sets auto mode behavior
Serial.write(0x01); //Data 1: Enable Auto-Read
Serial.write(0x03); //Data 2: Mode – Parity decoded – Manchester RF/64
Serial.write(0x02); //Data 3: Total number of block to be read (2)
Serial.write((byte)0x00); //Data 4: No password expected
Serial.write(0x10); //Data 5: Password byte 1
Serial.write(0x20); //Data 6: Password byte 2
Serial.write(0x30); //Data 7: Password byte 3
Serial.write(0x40); //Data 8: Password byte 4
Serial.write(0x37); //Checksum
delay(500);
while(Serial.available()>0)
{
Serial.read();
}
Serial.println();
Serial.println("Secured.");
}
void loop()
{
val = Serial.read();
while (val != 0xff)
{ //eerste byte is altijd 0xFF
val = Serial.read();
delay(1000);
}
Serial.read();
Serial.read();
Serial.read();
data[0] = Serial.read();
data[1] = Serial.read();
data[2] = Serial.read();
data[3] = Serial.read();
data[4] = Serial.read();
Serial.read();
boolean MC = true;
boolean GC = true;
Serial.print("gevonden kaart: "); //translate: found a card
for (int i=0; i<5; i++)
{
if (data[i] < 16) Serial.print("0");
Serial.print(data[i], HEX);
if (data[i] != Marius[i]) MC = false;
if (data[i] != Gast[i]) GC = false;
}
Serial.println();
if (MC)
{
Serial.println("Welkom Mario!"); //Translate Welcome Mario
for (int i = 0; i < 12; i++)
{
for(pos = 0; pos < 180; pos += 1)
{
voordeur.write(pos);
delay(5000);
}
for(pos = 180; pos>=1; pos-=1)
{ voordeur.write(pos);
}
}
}
else if (GC)
{
Serial.println("Hoi Gast!"); //translate Hello Guest
for (int i = 0; i < 12; i++)
{
for(pos = 0; pos < 180; pos += 1)
{
voordeur.write(pos);
delay(5000);
}
for(pos = 180; pos>=1; pos-=1)
{ voordeur.write(pos);
}
}
}
else
{
Serial.println("Dief, Ga weg!"); //translate: Thief! go away
for (int i = 0; i < 6; i++)
{
}
}
}
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(11); // attaches the servo on pin 9 to the servo object
}
void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
It works but when i Change the delay to 5000 to get it to wait 5 seconds it wont turn at all
The delay is inside the for loop so it will delay 5000 millisecond every time through the loop. 180 * 5000 mS =900 second to go through the loop. Another way to look at is 5 seconds per degree.
Firstly, you don't have to sit in a delay while the servo does its job or move it one degree oat a time unless you need to slow it down for some reason. If it is at 0 and you want it to go to 180, just write 180 to it and it will go there as fast as it can (about 1 second). So in your case, when you want to open the lock try:
Could it possibly be the powersource? (When the servo turns or is buzzing/shaking. The led on the NFC module is off and when i unplug the servo the led is back on
So it moves from 0 to 180 and stops? Did you take the for loop out? Please show the code you have to move the servo now. You are not powering the servo from the Arduino supply? That is not going to work. You need an external supply (5-6V) for the servo. 4 AA batteries will work. Connect battery, arduino and servo grounds together. Battery + to servo supply + .
the loop itself should be as short as possible.
servo needs updating a lot so in loop it should say if open then servo.180 else servo.0
try to avoid any delay as timing is affected better use millis()
Notwithstanding the unfortunate fact that the tutorials or "Playground" suggest that it may be possible, in practice, you should never attempt to power a servo from the internal regulator in the Arduino. You may get away with using the Vcc terminal if you are deriving power from a USB port, but even that is inadvisable.
This is a common question here, and the answer is almost always the same - power servo from separate supply such as a 6V battery pack - and the bizarre and erratic problem disappears.