Hi all! I have a project in which I'm using a RFID reader to read a tag and subsequently, move a servo when the right tag is read. I was able to successfully have the servo move, but it doesn't sweep it's entire 180 degree limits. It only moves something like 10 degrees. I've done the sweep tutorial and tried different methods, but somehow they don't work. I also tried using the servo library, but it doesn't seem to move the servo at all when the code is uploaded.
This is my first time doing programming for devices, so a lot of the advanced stuff is beyond my reach (that and I'm a mech E, so I'm really lost when it comes to this stuff =).
Here's my code:
// Modified by R. Moreno
#include <SoftwareSerial.h>int val = 0;
char code[10];
int bytesread = 0;
int validate = 0; // Create a variable to store value when a tag is read
// Recognized tags = 1, unrecognized tags = 2/** Adjust these values for your servo and setup, if necessary **/
int servoPin = 4; // control pin for servo motor
int minPulse = 500; // minimum servo position
int maxPulse = 2500; // maximum servo position
int turnRate = 100; // servo turn rate increment (larger value, faster rate)
int refreshTime = 20; // time (ms) between pulses (50Hz)/** The Arduino will calculate these values for you **/
int centerServo; // center servo position
int pulseWidth; // servo pulse width
long lastPulse = 0; // recorded time (ms) of the last pulseint ledPin = 13; //Sets pin 13 to LED
#define rxPin 8
#define txPin 9
// RFID reader SOUT pin connected to Serial RX pin at 2400bps to pin8void setup()
{
Serial.begin(9600); // Hardware serial for Monitor 9600bpspinMode(2,OUTPUT); // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
digitalWrite(2, LOW); // Activate the RFID readerpinMode(servoPin, OUTPUT); // Set servo pin as an output pin
centerServo = maxPulse - ((maxPulse - minPulse)/2);
pulseWidth = minPulse; // Give the servo a starting point (or it floats)
// Original value of "centerServo"pinMode(ledPin, OUTPUT); //Sets the digital pin as output
Serial.println(" Arduino RFID Door Unlocker");
Serial.println();
}void loop()
{
scan(); //Scan tags
if (validate == 1) //if tag is recognized
{
open(); //Do function "open" (see below)
delay (500);
}
if (validate == 2) //if tag is not recognized
{
blink(); //Do function "blink" (see below)
}
delay (500);
}void scan()
{
SoftwareSerial RFID = SoftwareSerial(rxPin,txPin);
RFID.begin(2400);validate = 0; //Reset validate variable
if((val = RFID.read()) == 10)
{ // check for header
bytesread = 0;
while(bytesread<10)
{ // read 10 digit code
val = RFID.read();
if((val == 10)||(val == 13))
{ // if header or stop bytes before the 10 digit reading
break; // stop reading
}
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
}if( strncmp(code,"1D009F07D1", 10) == 0)
{
// if 10 digit code equals "1D009F07D1"
Serial.print("TAG code is valid "); // possibly a good TAG
Serial.println(code); // print the TAG code
validate = 1; //Sets "validate" to 1, accepting tag
}else if( strncmp(code,"1D009FB1E6", 10) == 0)
{
// if 10 digit code equals "1D009FB1E6"
Serial.print("TAG code is valid "); // possibly a good TAG
Serial.println(code); // print the TAG code
validate = 1; //Sets "validate" to 1, accepting tag
}else if( strncmp(code,"12002EF4CC", 10) == 0)
{
// if 10 digit code equals "12002EF4CC"
Serial.print("TAG code is valid "); // possibly a good TAG
Serial.println(code); // print the TAG code
validate = 1; //Sets "validate" to 1, accepting tag
}else
{
Serial.print("TAG code is not valid: "); // possibly a bad TAG
Serial.println(code); // print the TAG code
validate = 2; //Sets "validate" to 2, denying tag
}
bytesread = 0;
delay(500); // wait for a second
}
}void open()
{
pulseWidth = maxPulse;// pulse the servo every 20 ms (refreshTime) with current pulseWidth
// this will hold the servo's position if unchanged, or move it if changed
if (millis() - lastPulse >= refreshTime)
{
digitalWrite(servoPin, HIGH); // start the pulse
delayMicroseconds(pulseWidth); // pulse width
digitalWrite(servoPin, LOW); // stop the pulse
lastPulse = millis(); // save the time of the last pulse
}
delay(5000);
pulseWidth = minPulse;if (millis() - lastPulse >= refreshTime)
{
digitalWrite(servoPin, HIGH); // start the pulse
delayMicroseconds(pulseWidth); // pulse width
digitalWrite(servoPin, LOW); // stop the pulse
lastPulse = millis(); // save the time of the last pulse
}
delay(1000);
}void blink()
{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(2000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(500); // waits for a 5 msec
}
Thanks to anyone who can help me out!