Hello fellow buiders and coders,
some of you might have seen me posing before with a lot of questions, but now i have quite a code, and I would realy like it if you guys could give me some feadback.
so the idea is this, we have a buoy with a RFID tag strapped onto it, and a hovercraft with an Arduino Uno and a RFID reader.
so the hovercraft makes it way towards the buoy, and at a range of approx 0.3m the tag is sensed, this data stream flows into arduino, and is used to give a specific order to the servo controlling a "valve", or better said, the servo releases golf balls into the water.
so here is my code version 1.1, and I dont think its that good yet, so could you guys give me some feedback?
im not to familiar with all the different commands, so any suggestions on re-phrasing certain code blocks are welcome as well
#include <Servo.h>
Servo myservo; //servo is in the system
int servoPin = 9; //at pin 9
int RFIDResetPin = 13; //RFID reader data stream on pin 13
int val = 0;
int pos = 0; //servo start position 0, schould be a set position once implemented in the disign
char tag1[13] = "1E009A4067A3"; //the ID codes are not from the tags i have in position, how can i find those tag numbers?
char tag2[13] = "010230F28243";
void setup(){
pinMode(servoPin, OUTPUT); //servo = output
myservo.attach(9);
Serial.begin(9600); //I copied this serial.begin(9600), what does it stand for?
pinMode(RFIDResetPin, OUTPUT);
digitalWrite(RFIDResetPin, HIGH); //i cant help but to notice in my setup I have no INPUT, is this nesseserily?
}
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[]){
if(strlen(tag) == 0) return; //empty, no need to contunue
if(compareTag(tag, tag1)){ // if matched tag1, do this: make the servo move, i have not yet updated the positions, this is a test.
for(pos = 0; pos < 180; pos +=1) //from 0 to 180
{
myservo.write(pos);
delay(15); //wait 15ms
}
for(pos = 180; pos>=1; pos-=1) //from 180 to 0
{
myservo.write(pos);
delay(15); //wait 15ms
}
}else if(compareTag(tag, tag2)){ //if matched tag2, do this
for(pos = 0; pos < 180; pos +=1) //same story, i got 2 tags, so i can make different commands for each tag
{
myservo.write(pos);
delay(15);
}
for(pos = 180; pos>=1; pos-=1)
{
myservo.write(pos);
delay(15);
}
}else{
Serial.println(tag); //read out any unknown tag, does it show these readings somewhere?
}
}
void resetReader(){
///////////////////////////////////
//Reset the RFID reader to read again.
///////////////////////////////////
digitalWrite(RFIDResetPin, LOW);
digitalWrite(RFIDResetPin, HIGH);
delay(150);
}
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
}
read it trough, and plz tell me what you think.
this topic is purely about the code btw, tomorrow I will start testing it with the hardware.