Go Down

Topic: Change the color of a RGB strip while the RFID tag is in place. (Read 851 times) previous topic - next topic

irv_cr

I started the week trying to achieve what the "title" says.
Thanks to the forum and some examples from other sites, I got
what I wanted to do.

So here, thanking for the help, I post my code. Hope it is useful to someone.


Description of the program:

I needed the Serial to print a number that corresponds to the tag of a RFID card

(Tag 1F002990BF19 stands for number 1
Tag 1F0017B6B04E stands for number 2 and so on with another tags)

The numbers will be read by VVVV or Unity to show a Standby screen when "0" is read
show Video A when "1" is read
show Video B when "2" is read
show Video C when "3" is read.

When screen is in Standby mode   "0 in the Serial"   the RGB strip light on a Cold White color.
When screen is in Video A   "1 in the Serial"   the RGB strip light on a Soft Blue color
When screen is in Video B   "2 in the Serial"   the RGB strip light on a Magenta color
When screen is in Video C   "3 in the Serial"   the RGB strip light on a Soft Green color

If you place the RFID tag/card over the reader, depending on which tag is placed, the leds will
change color and (VVVV or Unity) play the right video, and when the tag is removed from the reader, leds will change back to cold white color and standby screen.

Code here:

Code: [Select]
/*Im using an Arduino UNO with the ID-12LA RFID reader
 * with the little "sub-shield" from Sparkfun to help use the RFID reader.
 * https://www.sparkfun.com/products/9963
 * TX and RX pins from the ID-12LA Sparkfun "sub-shield" are connected to pins 2(RX), 3(TX) from arduino
 * because im using SoftwareSerial.
 *
 * Pin 6 from the ID-12LA (not from the RFID "sub-shield" from Sparkfun)
 * is conected to Pin 7 of Arduino.
 *
 * Pin 6 from the ID-12LA turns HIGH when a RFID Tag is in range of reading
 * and LOW when is out of range.
 *
 * Pins 11,10 and 9 are used to control RGB Leds with PWM
 */



#include <SoftwareSerial.h>

SoftwareSerial RFID(2, 3);

int PinRango = 7;
int TagEnRango= 0;
int RangoPrevio = 0;

int R = 11;
int G = 10;
int B = 9;

int fadeR = 0;
int fadeG = 0;
int fadeB = 0;

int Rprevio = 0;
int Gprevio = 0;
int Bprevio = 0;

int retrazo = 2;
int cant = 1;

String tarjetaString;
int tarjval;
String comparar;
int leds;

int tag = 0;

void setup() {
  pinMode(R, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(TagEnRango, INPUT);

Serial.begin(9600);
RFID.begin(9600);
Serial.println("0");     //I wanted to start the program with 0
      tag = 0;           //in order to display everything in "stanby" mode.
      leds = LED_RGB(tag);  

}

void loop() {
while(RFID.available()>0){

tarjetaString = RFID.readString();

tarjetaString.remove(13);//removes "garbage" at the end of the String
tarjetaString.remove(0,1);//removes "garbage" at the beginning of the String

////////////////////TAG PlACED/////////////////////////////

comparar = tarjetaString; //saves the "clean" String in the *comparar* variable
 
 //Serial.println(comparar); Use it to know the "clean" HEX number of your cards

if(comparar == "1F002990BF19"){  //
 tarjval = 1;
 Serial.println(tarjval);
 leds = LED_RGB(tarjval);
}

if(comparar == "11008827CD73"){
 tarjval = 2;
 Serial.println(tarjval);
 leds = LED_RGB(tarjval);
}

if(comparar == "1F0017B6F04E"){
 tarjval = 3;
 Serial.println(tarjval);
 leds = LED_RGB(tarjval);
}


} //end of while(RFID.available()>0)


TagEnRango = digitalRead(PinRango); //read the state if the Tag is in Range
if (TagEnRango != RangoPrevio) {
    if (TagEnRango == HIGH) {  //If a tag is placed over the ID-12LA reader
      tag = 1;
   }
      else {                  //If a tag is removed off the ID-12LA reader
      Serial.println("0");
      tag = 0;
      leds = LED_RGB(tag);    
    }
    delay(10);
  }
  RangoPrevio = TagEnRango;


} //end of main program void loop()


 int LED_RGB (int x){ //secundary program for running the RGB leds

switch (x) {
    
    case 0:             //Cold White
      
      for (fadeR = Rprevio ; fadeR <= 255; fadeR += 2*cant)
      {
      analogWrite(R, fadeR);
      delay(retrazo);
      }
      
       for (fadeG = Gprevio ; fadeG <= 255; fadeG += 2*cant)
      {
      analogWrite(G, fadeG);
      delay(retrazo);
      }
      
       for (fadeB = Bprevio ; fadeB <= 150; fadeB += 2*cant)
      {
      analogWrite(B, fadeB);
      delay(retrazo);
      }

      break;
////////////////////////////////////////////////////////////////////////////////////      
    case 1:            //Soft Blue
        
      for (fadeR = 255 ; fadeR >= 0; fadeR -= cant)
      {
      analogWrite(R, fadeR);
      delay(retrazo);
      }
      
       for (fadeG = 255 ; fadeG >= 150; fadeG -= cant)
      {
      analogWrite(G, fadeG);
      delay(retrazo);
      }
      
       for (fadeB = 150 ; fadeB >= 255; fadeB -= cant)
      {
      analogWrite(B, fadeB);
      delay(retrazo);
      }

      Rprevio = 0;
      Gprevio = 150;
      Bprevio = 255;
      
      break;
/////////////////////////////////////////////////////////////////////////////////////        
    case 2:           //Magenta
      
       for (fadeR = 255 ; fadeR >= 255; fadeR -= cant)
      {
      analogWrite(R, fadeR);
      delay(retrazo);
      }
      
       for (fadeG = 255 ; fadeG >= 20; fadeG -= cant)
      {
      analogWrite(G, fadeG);
      delay(retrazo);
      }
      
       for (fadeB = 150 ; fadeB >= 150; fadeB -= cant)
      {
      analogWrite(B, fadeB);
      delay(retrazo);
      }

      Rprevio = 255;
      Gprevio = 20;
      Bprevio = 150;
      
      break;
/////////////////////////////////////////////////////////////////////////////////////        
    case 3:           //Soft Green
        
      for (fadeR = 255 ; fadeR >= 100; fadeR -= cant)
      {
      analogWrite(R, fadeR);
      delay(retrazo);
      }
      
       for (fadeG = 255 ; fadeG >= 255; fadeG -= cant)
      {
      analogWrite(G, fadeG);
      delay(retrazo);
      }
      
       for (fadeB = 150 ; fadeB >= 15; fadeB -= cant)
      {
      analogWrite(B, fadeB);
      delay(retrazo);
      }
      
      Rprevio = 100;
      Gprevio = 255;
      Bprevio = 15;
      
      break;

    }//end of switch (x) or case
  
 }//end of the secundary program LED_RGB



Go Up