Hello fellow coders and builders,
i'm trying to control 2 servo's with 2 RFID tags, and a reader in between. So far i've had the reader activate 2 LED's in the right way, The 2 servo's work perfectly fine in the code i wrote for them.
and then the mayhem begins, when combining these 2 codes the servo's wont move an inch, they are active, that i can tell from the constant shivering when they stay in "0" position, but when i hold a tag for the reader nothing happens, and im clueless why.
So plz help!
the 1st code for the RFID reader, controlling LED's:
int RFIDResetPin = 13;
//Register your RFID tags here
char tag1[13] = "060097889A83";
char tag2[13] = "060097889F86";
void setup(){
Serial.begin(9600);
pinMode(RFIDResetPin, OUTPUT);
digitalWrite(RFIDResetPin, HIGH);
//ONLY NEEDED IF CONTROLING THESE PINS - EG. LEDs
pinMode(12, OUTPUT);
pinMode(8, OUTPUT);
}
void loop(){
char tagString[13];
int index = 0;
boolean reading = false;
while(Serial.available()){
int readByte = Serial.read(); //read next available byte
if(readByte == 2) reading = true; //begining of tag
if(readByte == 3) reading = false; //end of tag
if(reading && readByte != 2 && readByte != 10 && readByte != 13){
//store the tag
tagString[index] = readByte;
index ++;
}
}
checkTag(tagString); //Check if it is a match
clearTag(tagString); //Clear the char of all value
resetReader(); //eset the RFID reader
}
void checkTag(char tag[]){
///////////////////////////////////
//Check the read tag against known tags
///////////////////////////////////
if(strlen(tag) == 0) return; //empty, no need to contunue
if(compareTag(tag, tag1)){ // if matched tag1, do this
lightLED(12);
}else if(compareTag(tag, tag2)){ //if matched tag2, do this
lightLED(8);
}else{
Serial.println(tag); //read out any unknown tag
}
}
void lightLED(int pin){
///////////////////////////////////
//Turn on LED on pin "pin" for 250ms
///////////////////////////////////
Serial.println(pin);
digitalWrite(pin, HIGH);
delay(250);
digitalWrite(pin, LOW);
}
void resetReader(){
///////////////////////////////////
//Reset the RFID reader to read again.
///////////////////////////////////
digitalWrite(RFIDResetPin, LOW);
digitalWrite(RFIDResetPin, HIGH);
delay(500);
}
void clearTag(char one[]){
///////////////////////////////////
//clear the char array by filling with null - ASCII 0
//Will think same tag has been read otherwise
///////////////////////////////////
for(int i = 0; i < strlen(one); i++){
one[i] = 0;
}
}
boolean compareTag(char one[], char two[]){
///////////////////////////////////
//compare two value to see if same,
//strcmp not working 100% so we do this
///////////////////////////////////
if(strlen(one) == 0) return false; //empty
for(int i = 0; i < 12; i++){
if(one[i] != two[i]) return false;
}
return true; //no mismatches
}
the above works like a charm.
then i wrote this, to control the servo's :
#include <Servo.h>
Servo myservo;
Servo myservo1;
int pos = 0;
void setup(){
myservo.attach(10);
myservo1.attach(9);
}
void loop(){
myservo.write(90);
delay(200);
myservo.write(0);
delay(800);
myservo1.write(90);
delay(200);
myservo1.write(0);
delay(800);
}
nothing to special, but it works like a charm.
then I combined the 2:
int RFIDResetPin = 13;
char tag1[13] = "060097889A83";
char tag2[13] = "060097889F86";
#include <Servo.h>
Servo myservo;
Servo myservo1;
int pos = 0;
void setup(){
Serial.begin(9600);
pinMode(RFIDResetPin, OUTPUT);
digitalWrite(RFIDResetPin, HIGH);
myservo.attach(10);
myservo1.attach(9);
}
void loop(){
char tagString[13];
int index = 0;
boolean reading = false;
myservo.write(0);
myservo1.write(0);
while(Serial.available()){
int readByte = Serial.read(); //read next available byte
if(readByte == 2) reading = true; //begining of tag
if(readByte == 3) reading = false; //end of tag
if(reading && readByte != 2 && readByte != 10 && readByte != 13){
//store the tag
tagString[index] = readByte;
index ++;
}
}
checkTag(tagString); //Check if it is a match
clearTag(tagString); //Clear the char of all value
resetReader(); //eset the RFID reader
}
void checkTag(char tag[]){
///////////////////////////////////
//Check the read tag against known tags
///////////////////////////////////
if(strlen(tag) == 0) return; //empty, no need to contunue
if(compareTag(tag, tag1)){ // if matched tag1, do this
Drop1(10);
}
else if(compareTag(tag, tag2)){ //if matched tag2, do this
Drop2(9);
}
else{
Serial.println(tag); //read out any unknown tag
}
}
void Drop1(int pin){
Serial.println(pin);
myservo.write(90);
delay(200);
myservo.write(0);
delay(800);
}
void Drop2(int pin){
myservo1.write(90);
delay(200);
myservo1.write(0);
delay(800);
}
void resetReader(){
///////////////////////////////////
//Reset the RFID reader to read again.
///////////////////////////////////
digitalWrite(RFIDResetPin, LOW);
digitalWrite(RFIDResetPin, HIGH);
delay(500);
}
void clearTag(char one[]){
///////////////////////////////////
//clear the char array by filling with null - ASCII 0
//Will think same tag has been read otherwise
///////////////////////////////////
for(int i = 0; i < strlen(one); i++){
one[i] = 0;
}
}
boolean compareTag(char one[], char two[]){
///////////////////////////////////
//compare two value to see if same,
//strcmp not working 100% so we do this
///////////////////////////////////
if(strlen(one) == 0) return false; //empty
for(int i = 0; i < 12; i++){
if(one[i] != two[i]) return false;
}
return true; //no mismatches
}
And nothing happens, this is the 3rd try on combining the first 2 codes, and none works properly.
I added myservo.write(0) in setup because i notices that the servo's weren't turned on at first, and when i kept a tag near the reader they went "on", after witch nothing happens...
Im sorry for not explaining every bit of code, but most of it is plain simple, im not that good with arduino yet
If any of you got some suggestions on improving this code, and making it work, don't hesitate and respond please.
btw, the idea behind this is: when a tag comes near the reader, the servo's remove a pin, so a golf-ball rolls down a pipe.
Thanx in advance
Koen de Knegt