I got a minor issue - with turning off 3 LED`s when a surden RFID tag is reconized.
Got this:
else if(compareTag(tag, tag4))
{
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5)
{
// sets the value (range from 0 to 255):
analogWrite(3, fadeValue);
analogWrite(5, fadeValue);
analogWrite(6, fadeValue);
delay(30);}
}
Bu this turns on all LED`s and them dim them - how do I integrate IF statements - so I only go into dim mode on each LED - if the LED are above level 200 ?
int RFIDResetPin = 13;
//Reconized RFID tags
char tag1[13] = "4500F7739E5F"; //RØD
char tag2[13] = "4B00DA2EB40B"; //GRØN
char tag3[13] = "4500D9558049"; //BLÅ
char tag4[13] = "4500F76F63BE"; //SORT
void setup(){
Serial.begin(9600);
pinMode(RFIDResetPin, OUTPUT);
digitalWrite(RFIDResetPin, HIGH);
//LEDs
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, 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
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5)
{
// sets the value (range from 0 to 255):
analogWrite(3, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
else if(compareTag(tag, tag2))
{ //if matched tag2, do this
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5)
{
// sets the value (range from 0 to 255):
analogWrite(5, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
else if(compareTag(tag, tag3))
{
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5)
{
// sets the value (range from 0 to 255):
analogWrite(6, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
else if(compareTag(tag, tag4))
{
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5)
{
// sets the value (range from 0 to 255):
analogWrite(3, fadeValue);
analogWrite(5, fadeValue);
analogWrite(6, fadeValue);
delay(30);}
}
else
{
Serial.println(tag); //read out any unknown tag
}
}
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
}
When you scan a tag, it fades the matching LED up to full brightness, in a blocking fashion. You need to keep track of having done that, in an array.
bool ledState[3]; // global
After fading the LED up, in each case, set ledState[n] to true (where n is 0 for tag 1, 1 for tag 2, etc.).
Then, in the tag 4 case:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5)
{
// sets the value (range from 0 to 255):
if(ledState[0]
analogWrite(3, fadeValue);
with similar additions for the other two pins.
After fading them all off, set ledState[n] to false, for n=0, 1, and 2.