I have an Parallax RFID reader and a HI Tec HS-422 servo motor connected to my Arduino Uno R2. The Servo is being powered externally, and I have the 5v for the RFID connected to the Arduino.
When ever I read a tag on the RFID reader, the servo starts to become jittery if I'm use the write(); function to control the servo.
Anyone have any ideas what the problem is? I tried using some pulse width stuff but that didn't seem to many any difference.
below is the code
/*
RFID SOUT <---> Arduino Digital Pin 5
RFID ENABLE <---> Arduino Digital Pin 2
*/
#include <SoftwareSerial.h>
#include <Servo.h>
//Parallax RFID Reader
#define RFIDEnablePin 2 //Pin that enables reading. Set as OUTPUT and LOW to read an RFID tag
#define RFIDSerialRate 2400 //Parallax RFID Reader Serial Port Speed
#define servoPin 4 //servo pin
#define minPulse 500 //minimum servo position (originally 500)
#define maxPulse 2200 //max servo position (originally 2200)
int refreshTime = 20;
//arduino will calculate these values
int pulseWidth;
long lastPulse = 0;
//Using SoftwareSerial Library to locate the serial pins off the default set
//This allows the Arduino to be updated via USB with no conflict
#define RxPin 8 //Pin to read data from Reader
#define TxPin 9 //Pin to write data to the Reader NOTE: The reader doesn't get written to, don't connect this line.
SoftwareSerial RFIDReader(RxPin,TxPin);
boolean balanced = true;
int myVal = 0;
int pulse, switchVal;
Servo myservo;
#define TOGGLE_LED_STATE_ON 't'
#define TOGGLE_LED_STATE_OFF 'o'
String RFIDTAG=""; //Holds the RFID Code read from a tag
String DisplayTAG = ""; //Holds the last displayed RFID Tag
int firstSensor = 0;
int secondSensor = 0;
int trackNum = 0;
int inByte = 0;
char val;
int ledPin = 13;
int sensorVal = 0;
int servoPos = 0;
void setup()
{
// RFID reader SOUT pin connected to Serial RX pin at 2400bps
RFIDReader.begin(RFIDSerialRate);
// Set Enable pin as OUTPUT to connect it to the RFID /ENABLE pin
pinMode(RFIDEnablePin,OUTPUT);
// Activate the RFID reader
// Setting the RFIDEnablePin HIGH will deactivate the reader
// which could be usefull if you wanted to save battery life for
// example.
digitalWrite(RFIDEnablePin, LOW);
Serial.begin(9600); // set up Serial library at 9600 bps
//Serial.println("Hello world --!"); // prints hello with ending line break
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
myservo.attach(4);
}
void loop()
{
if(RFIDReader.available() > 0) // If data available from reader
{
ReadSerial(RFIDTAG); //Read the tag number from the reader. Should return a 10 digit serial number
}
//This only displays a tag once, unless another tag is scanned
if(DisplayTAG!=RFIDTAG)
{
DisplayTAG=RFIDTAG;
//Serial.println(RFIDTAG);
}
if(RFIDTAG == "3600095756"){
trackNum = 1;
}
if(RFIDTAG == "190035D511"){
trackNum = 2;
}
if(RFIDTAG == "190035E473"){
trackNum = 3;
}
if(RFIDTAG == "17007EFC59"){
trackNum = 4;
}
if(RFIDTAG == "190035E3D2"){
trackNum = 5;
}
sensorVal ++;
Serial.print(trackNum);
Serial.print(",");
Serial.println(sensorVal);
if(Serial.available()){
val = Serial.read();
if(val == TOGGLE_LED_STATE_ON){
digitalWrite(ledPin, HIGH);
myservo.write(90);
}
if(val == TOGGLE_LED_STATE_OFF){
digitalWrite(ledPin, LOW);
myservo.write(0);
}
}
}
void ReadSerial(String &ReadTagString)
{
int bytesread = 0;
int val = 0;
char code[10];
String TagCode="";
if(RFIDReader.available() > 0) { // If data available from reader
if((val = RFIDReader.read()) == 10) { // Check for header
bytesread = 0;
while(bytesread<10) { // Read 10 digit code
if( RFIDReader.available() > 0) {
val = RFIDReader.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(bytesread == 10) { // If 10 digit read is complete
for(int x=0;x<10;x++) //Copy the Chars to a String
{
TagCode += code[x];
}
ReadTagString = TagCode; //Update the caller
while(RFIDReader.available() > 0) //Burn off any characters still in the buffer
{
RFIDReader.read();
}
}
bytesread = 0;
TagCode="";
}
}
}