I have a project working with RFID sensor sensing 3 different Tags which would change 3 set of 5mm RGB LEDs' color combinations.
The first Tag is okay,
The second Tag, I wanna keep swiching red and orange, but just one set show the result, other two are red and green
The third Tag, I wanna have all RGB (255, 255, 255) which would similar to white. But just one set show the result correct, other two are green or yellow..
(code tags added by Moderator - next time use the </> button to do the same please)
#include <SoftwareSerial.h>
SoftwareSerial RFID(2, 0); // RX and TX
int data1 = 0;
int ok = -1;
int yes = 13;
int no = 12;
char cardNumber[12];
int tag1[14] = {2, 48, 56, 48, 48, 48, 66, 49, 55, 54, 49, 55, 53, 3 }; //yellow
int tag2[14] = {2, 48, 56, 48, 48, 48, 65, 67, 53, 68, 56, 49, 70, 3 }; //blue
int tag3[14] = {2, 50, 56, 48, 48, 49, 53, 68, 53, 51, 51, 68, 66, 3 }; // red
int newtag[14] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // used for read comparisons
int card1 = 0;
int card2 = 0;
int card3 = 0;
//RGB LED pins
int ledDigitalOne[] = {14, 15, 16};
//the three digital pins of the first digital LED A0 = redPin, A1 = greenPin, A2 = bluePin
int ledDigitalTwo[] = {9, 10, 11};
//the three digital pins of the second digital LED 9 = redPin, 10 = greenPin, 11 = bluePin
int ledDigitalThree[] = {3, 5, 6};
//the three digital pins of the third digital LED 3 = redPin, 5 = greenPin, 6 = bluePin
const boolean ON = LOW;
//Define on as LOW (this is because we use a common Anode RGB LED (common pin is connected to +5 volts)
const boolean OFF = HIGH;
//Define off as HIGH
//Pedefined Colors
// const boolean RED[] = {OFF, ON, ON};
// const boolean GREEN[] = {ON, OFF, ON};
// const boolean BLUE[] = {ON, ON, OFF};
// const boolean YELLOW[] = {OFF, OFF, ON};
// const boolean WHITE[] = {OFF, OFF, OFF};
// const boolean MAGENTA[] = {OFF, ON, OFF};
// const boolean BLACK[] = {ON, ON, ON};
int red[] = {255,0,0};
int green[] = {0,255,0};
int chin[] = {0,255,255}; //chin
int chin2[] = {0, 100, 50};
int tiffiney[] = {0, 100, 100};
int yellow[] = {255,255,0};
int white[] = {255,255,255};
int black[] = {0, 0, 0};
int orange[] = {200, 100, 0};
int chin3[] = {255,150, 0};
int blue[] = {0,0,255};
int purple[] = {255,0,255};
int light1[] = {10, 50 , 200};
int light2[] = {255, 255 , 255};
int light3[] = {255, 255 , 255};
//An Array that stores the predefined colors (allows us to later randomly display a color)
// const boolean* COLORS[] = {RED, GREEN, BLUE};
// const int* COLORS[] = {red, green, blue, tiffiney, yellow, white};
const int* colorList1[] = {red, green, blue}; // for christmas
const int* colorList2[] = {chin, chin2}; // new year
const int* colorList22[] = {red, chin3};
const int* colorList222[] = {blue, white};
const int* colorList3[] = {white, white, white}; //normal
//--- eof RGBL - RGB Digital Preamble
int flag1 = 0, flag2 = 0;
int i = 0,j = 0,k = 0;
long previousMillis1 = 0; // will store last time LED was updated
long previousMillis2 = 0; // will store last time LED was updated
long previousMillis3 = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000; // interval at which to blink (milliseconds)
void setup(){
RFID.begin(9600); // start serial to RFID reader
Serial.begin(9600); // start serial to PC
}
boolean comparetag(int aa[14], int bb[14]){
boolean ff = false;
int fg = 0;
for (int cc = 0 ; cc < 14 ; cc++){
Serial.println(aa[cc]);
if (aa[cc] == bb[cc]){
fg++;
}
}
if (fg == 14){
ff = true;
}
return ff;
}
void checkmytags(){ // compares each tag against the tag just read
ok = 0; // this variable helps decision-making,
if (comparetag(newtag, tag3) == true){
card3 = 1;
} else {
card3 = 0;
}
if (comparetag(newtag, tag1) == true){
card1 = 1;
} else {
card1 = 0;
}
if (comparetag(newtag, tag2) == true){
card2 = 1;
} else {
card2 = 0;
}
}
void readTags(){
ok = -1;
if (RFID.available() > 0){
// read tag numbers
delay(100); // needed to allow time for the data to come in from the serial buffer.
for (int z = 0 ; z < 14 ; z++){ // read the rest of the tag
data1 = RFID.read();
newtag[z] = data1;
}
RFID.flush(); // stops multiple reads
// do the tags match up?
checkmytags();
}
// now do something based on tag type
if (ok > 0){ // if we had a match
Serial.println("Accepted");
ok = -1;
}
if (card1 == 1) // if we had a match
{
Serial.println("colorList1");
unsigned long currentMillis = millis();
if(currentMillis - previousMillis1 > 1000) {
Serial.println("Reach previousMillis1!");
previousMillis1 = currentMillis;
Serial.println(previousMillis1);
setColor(ledDigitalOne, colorList1[i++]);
//setColor1(ledDigitalOne,white);
if(i == 3)
i=0;
}
if(currentMillis - previousMillis2 > 1000) {
Serial.println("Reach previousMillis2!");
previousMillis2 = currentMillis;
Serial.println(previousMillis2);
setColor(ledDigitalTwo, colorList1[j++]);
if(j == 3)
j=0;
}
if(currentMillis - previousMillis3 > 1000) {
Serial.println("Reach previousMillis2!");
previousMillis3 = currentMillis;
Serial.println(previousMillis3);
setColor(ledDigitalThree, colorList1[k++]);
if(k == 3)
k=0;
}
}
if (card2 == 1) // if we had a match
{
Serial.println("colorList2");
unsigned long currentMillis = millis();
if(currentMillis - previousMillis1 > 1500) {
Serial.println("Reach previousMillis3!");
previousMillis1 = currentMillis;
Serial.println(previousMillis1);
setColor(ledDigitalOne, colorList22[i++]);
//setColor1(ledDigitalOne,white);
if(i == 2)
i=0;
}
// setColor(ledDigitalOne, red);
if(currentMillis - previousMillis2 > 1200) {
Serial.println("Reach previousMillis3!");
previousMillis2 = currentMillis;
Serial.println(previousMillis2);
setColor(ledDigitalTwo, colorList222[j++]);
if(j == 4)
j=0;
}
// setColor(ledDigitalTwo, blue);
if(currentMillis - previousMillis3 > 1000) {
Serial.println("Reach previousMillis2!");
previousMillis3 = currentMillis;
Serial.println(previousMillis3);
setColor(ledDigitalThree, colorList2[k++]);
if(k == 2)
k=0;
}
// setColor(ledDigitalThree, green);
}
if (card3 == 1) // if we had a match
{
Serial.println("colorList3");
setColor(ledDigitalOne, light2);
setColor(ledDigitalTwo, light1);
setColor(ledDigitalThree, light3 );
// unsigned long currentMillis = millis();
//
// if(currentMillis - previousMillis1 > 100) {
// Serial.println("Reach previousMillis1!");
// previousMillis1 = currentMillis;
// Serial.println(previousMillis1);
// setColor(ledDigitalOne, colorList2[i++]);
// //setColor1(ledDigitalOne,white);
// if(i == 2)
// i=0;
// }
// if(currentMillis - previousMillis2 > 500) {
// Serial.println("Reach previousMillis2!");
// previousMillis2 = currentMillis;
// Serial.println(previousMillis2);
// setColor(ledDigitalTwo, colorList2[j++]);
// if(j == 2)
// j=0;
// }
// if(currentMillis - previousMillis3 > 1000) {
// Serial.println("Reach previousMillis2!");
// previousMillis3 = currentMillis;
// Serial.println(previousMillis3);
// setColor(ledDigitalThree, colorList2[k++]);
//
// if(k == 2)
// k=0;
// }
}
}
void loop(){
readTags();
}
//color pins (led[0] = redPin, led[1] = greenPin, led[2] = bluePin) color
//boolean array (color[0] = red value (LOW = on, HIGH = off),
//color[1] = green value, color[2] =blue value)*/
void setColor(int* led, const int* color) {
for (int i = 0; i < 3; i++) {
analogWrite(led[i], color[i]);
}
}