Offline
Newbie
Karma: 0
Posts: 9
|
 |
« on: January 10, 2013, 12:05:46 pm » |
Hello, I'm a Arduino Uno with a ID-12 RFID reader (with two tags) and a piranha RGB LED. What I'm trying to achieve is when the RGB LED turns one colour (e.g. red) you swipe the tag and it turns the LED off. I want this to happen with another colour (blue) and the other tag also. So far I have set everything up and I've got the tags reading with the RFID with this code: char val = 0; // variable to store the data from the serial port
void setup() { Serial.begin(9600); // connect to the serial port }
void loop () { // read the serial port if(Serial.available() > 0) { val = Serial.read(); Serial.write(val); //sends as char } } The two tag ID's are: 67005DC651AD and 67005DB143C8 I have also for my piranha LED lighting up the colours I need. With this code: int redPin = 11; int greenPin = 10; int bluePin = 9; void setup() { pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); } void loop() { setColor(255, 255, 0); // red delay(1000); setColor(0, 255, 255); // blue delay(1000); } void setColor(int red, int green, int blue) { analogWrite(redPin, red); analogWrite(greenPin, green); analogWrite(bluePin, blue); } The problem is I don't know where to start with putting the codes together to get them to do want I want. Could somebody please offer me some assistance? Thanks and have a good day!
|
|
|
|
|
Logged
|
|
|
|
|
Malaysia
Offline
Sr. Member
Karma: 7
Posts: 385
|
 |
« Reply #1 on: January 10, 2013, 12:12:23 pm » |
ouh this is some of my humblest of opinion. if you can make it read the rfid why not just use it as a switch case maybe like switch (val) case (*) insert the first adress then do something break case(*) insert the second adress then do something break. btw could you explain again what your program suppose to do? couse there is some part that i cant understand like What I'm trying to achieve is when the RGB LED turns one colour (e.g. red) you swipe the tag and it turns the LED off. I want this to happen with another colour (blue) and the other tag also. could you explain more what does that suppose to mean?
|
|
|
|
|
Logged
|
|
|
|
|
California
Offline
Edison Member
Karma: 51
Posts: 2192
|
 |
« Reply #2 on: January 10, 2013, 12:41:14 pm » |
Start by saving the RFID strings in to a char array, rather than writing them to the Serial monitor. You'll need a char array big enough to hold the IDs plus 1 for the null terminator, an index variable that keeps track of where you are in the array, and someway to determine when you've received the last char.
|
|
|
|
« Last Edit: January 10, 2013, 12:43:23 pm by Arrch »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #3 on: January 10, 2013, 06:07:36 pm » |
What I'm trying to achieve is when the RGB LED turns one colour (e.g. red) you swipe the tag and it turns the LED off. I want this to happen with another colour (blue) and the other tag also.
could you explain more what does that suppose to mean? ash901226 - What I mean is that I would like there to be two colours that will come on randomly that will notify the user to put the RFID tag on the reader. For example, I want a red LED to come on through the piranha and when the LED comes on, you place the RFID tag on and then it turns the LED off. I want this to happen with another colour through the piranha LED and another RFID tag also. So there is 2 different coloured LEDS and 2 RIFD tags. Sorry if it is a bit confusing. Start by saving the RFID strings in to a char array, rather than writing them to the Serial monitor. You'll need a char array big enough to hold the IDs plus 1 for the null terminator, an index variable that keeps track of where you are in the array, and someway to determine when you've received the last char. Thanks Arrch for the reply. How would I go about doing this, if you don't mind talking more in-depth about this? Cheers.
|
|
|
|
|
Logged
|
|
|
|
|
|
|
California
Offline
Edison Member
Karma: 51
Posts: 2192
|
 |
« Reply #5 on: January 11, 2013, 01:27:14 am » |
Thanks Arrch for the reply. How would I go about doing this, if you don't mind talking more in-depth about this?
Example code that takes an ASCII string from a serial device such as "<1234>" and converts it into the numerical 1234. /* * Example for Serial2Int * When reading from the serial monitor, there are two important things to note: * (1) Bytes are read one at a time. So when sending "246", will be read by your * code as '2', then '4', then '6'. If you want to identify them as related in some * way, you need a way to determine that. This example uses start and stop bytes. * (2) Sending a number through the monitor sends it's ASCII representation, not * the value itself. So typing 3 and hitting enter would send '3' or 51 as per the * ascii table. To account for this, we will be using atoi(), which takes a null * terminated array of chars, also known as a string, and produces the int equivalent. */
// To send a number through the serial monitor, put it between brackets const char startByte = '<'; const char stopByte = '>';
// Maximum characters in an int + null terminated character const int maxBuffer = 6;
void setup() { Serial.begin(115200); Serial.println("[Serial2Int]"); }
void loop() { // Stores the characters between the start and stop bytes static char buffer[maxBuffer]; // Keeps track of spot in buffer static int index=0; if (Serial.available() > 0 ) { char inChar = Serial.read(); if (inChar==startByte) { // If start byte is received index=0; // then reset buffer and start fresh } else if (inChar==stopByte) { // If stop byte is received buffer[index] = '\0'; // then null terminate processData(buffer); // and process the data index=0; // this isn't necessary, but helps limit overflow } else { // otherwise buffer[index] = inChar; // put the character into our array index++; // and move to the next key in the array } /* Overflow occurs when there are more than 5 characters in between * the start and stop bytes. This has to do with having limited space * in our array. We chose to limit our array to 5 (+1 for null terminator) * because an int will never be above 5 characters */ if (index>=maxBuffer) { index=0; Serial.println("Overflow occured, next value is unreliable"); } } }
void processData(char buffer[]) { unsigned int value = atoi(buffer); // convert string to int Serial.print("Value: "); Serial.println(value); }
|
|
|
|
« Last Edit: January 11, 2013, 01:29:19 am by Arrch »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #6 on: January 11, 2013, 09:24:34 am » |
Once again, thanks for the replies. I've gotten this code which I was able to get working with a single LED but when i'm not sure how to get it working with my piranha RGB LED. int redPin = 11; int greenPin = 10; int bluePin = 9; int RFID = 0;
//Register your RFID tags here char tag1[13] = "67005DC651AD"; char tag2[13] = "67005DB143C8";
void setup(){ Serial.begin(9600);
pinMode(RFID, OUTPUT); pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, 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 turnoffLED(2);
}else if(compareTag(tag, tag2)){ //if matched tag2, do this turnoffLED(3);
}else{ Serial.println(tag); //read out any unknown tag }
}
void turnoffLED(int pin){
//Turn on LED on pin "pin" for 250ms
digitalWrite(pin, LOW); delay(1000); digitalWrite(pin, HIGH); }
void resetReader(){ // Reset the RFID reader to read again
digitalWrite(RFID, LOW); digitalWrite(RFID, 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 } So what I would like to happen is both of the tags will be set up to match with each one of the LED colours. Then one of the two colours (red (RGB 255, 255, 0) or blue (RGB 0, 255, 255) will turn on at a random and then you put the RFID tag on which will turn on and then it will restart and another colour will come at random. For example if the colour red came on and then it was linked to tag 1, then you would need to place that tag on and then the LED will turn. I'm hoping this does not sound too confusing and would be gratefully for help. also I am new to Arduino, can you tell? lol. Cheers!
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 336
Posts: 36482
Seattle, WA USA
|
 |
« Reply #7 on: January 11, 2013, 07:33:24 pm » |
That code does something. What does it do? Does it turn the LED on or off?
|
|
|
|
|
Logged
|
|
|
|
|
Malaysia
Offline
Sr. Member
Karma: 7
Posts: 385
|
 |
« Reply #8 on: January 12, 2013, 04:17:52 am » |
PaulS i think that what the op wanted to do is to let a led light up in random which could me one of 2 which is red or blue, so when the red led light up, one of the rfid will turn the led off while the will not.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 336
Posts: 36482
Seattle, WA USA
|
 |
« Reply #9 on: January 12, 2013, 08:42:19 am » |
PaulS i think that what the op wanted to do is to let a led light up in random which could me one of 2 which is red or blue, so when the red led light up, one of the rfid will turn the led off while the will not. Probably. But, does that code correctly read the RFID tag? Does it correctly compare the tag read to the other two tags? What problem are we trying to solve? If all that need to change is what action happens when tag 1 or tag 2 is scanned, then that is a trivial change. If collecting the data from the RFID is not working correctly, then the changes needed to the code are far more extensive. Where do we need to start?
|
|
|
|
|
Logged
|
|
|
|
|
Malaysia
Offline
Sr. Member
Karma: 7
Posts: 385
|
 |
« Reply #10 on: January 12, 2013, 11:09:36 am » |
as far as my understanding of the op work. he have manage to read the RFID and transmit the value to computer via serial. however i think that he is unable to use the data to read and set the necessary action when the RFID card had been scan
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #11 on: January 13, 2013, 11:01:06 am » |
Hello - apologises for the confusion and lack of posting. That code does something. What does it do? Does it turn the LED on or off? That code in post 7 I got working through just one normal LED. so that when you scan a RFID tag it turned the LED on. I tried to change it to work with my RGB LED and to do the opposite so the LED is always on but when you scan the tag the LED goes off but I'm struggling a tad. As ash901226 as correctly said this is what I have managed to do so far: manage to read the RFID and transmit the value to computer via serial. however i think that he is unable to use the data to read and set the necessary action when the RFID card had been scan Right now, what I am trying to do is with the RGB Led and the RFID reader is: I have the colours red and blue on my RGB LED, I want to match tag 1 (67005DC651AD) with red and tag 2 (67005DB143C8) with blue. I want this to happen by the Arduino randomly selecting one of those two colours to come on and then when you scan the right tag, the LED will turn off. Then it select one of the two colours again to go back on after 10 seconds. At this present moment I found some code and added some bits in to make this but this doesn't seem to work: int redPin = 11; int greenPin = 10; int bluePin = 9; int RFID = 0;
int red = 0; int green = 0; int blue = 0;
int randomVal;
byte colours[3][3] = { // three colours 0-3, and // each has three components 0-2 for red, green, blue 255, 0, 0, // red 0, 0, 255, // blue 0, 255, 0, // green }; //Register your RFID tags here char tag1[13] = "67005DC651AD"; char tag2[13] = "67005DB143C8";
void setup(){ Serial.begin(9600);
pinMode(RFID, OUTPUT); pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); }
void loop(){ randomSeed(analogRead(0)); 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 }
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 red; if (red = LOW); {
}if(compareTag(tag, tag2)){ //if matched tag2, do this blue; if (blue = LOW);
}else{ Serial.println(tag); //read out any unknown tag }
}
digitalWrite(RFID, LOW); digitalWrite(RFID, 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 }
void chooseColour() { int randomVal = random(0, 2); // Choose a random value, 0-2 byte red = colours[randomVal][0]; byte blue = colours[randomVal][1]; byte green = colours[randomVal][2]; analogWrite(redPin, red); analogWrite(greenPin, green); analogWrite(bluePin, blue); delay(1000); }
Cheers everyone!
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 336
Posts: 36482
Seattle, WA USA
|
 |
« Reply #12 on: January 13, 2013, 11:57:34 am » |
randomSeed(analogRead(0)); randomSeed() should be called once. Not in every pass through loop. red; What do you think this is doing? It isn't, but we need to know what you think it should do in order to tell you how to "Make it so!". if (red = LOW); {
} Assigning LOW to red won't help. The semicolon on end is wrong. If you aren't going to do anything, get rid of the whole block. Absolutely nothing should come after the } on the same line.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #13 on: January 13, 2013, 12:49:53 pm » |
Thanks for the reply PaulS. What do you think this is doing? It isn't, but we need to know what you think it should do in order to tell you how to "Make it so!". I thought it said, if the selected colour is red then once tag 1 is read it will turn it off. But after looking back over it, it is just mush!
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 336
Posts: 36482
Seattle, WA USA
|
 |
« Reply #14 on: January 13, 2013, 01:22:26 pm » |
Lets forget about the RFID as the trigger, for the moment. Do you know how to make the LED turn red? Do you know how to make it turn blue?
If not, you should learn that. If you do, then randomly selecting a function to execute is easy. Calling that function is easy.
Create 4 functions - redOn(), redOff(), blueOn(), and blueOff().
Make sure that they work on their own. Then, copy the functions in the RFID sketch, and call the appropriate function at the appropriate time.
|
|
|
|
|
Logged
|
|
|
|
|
|