Hello everyone quick ? I'm as bout green as you can get with this stuff so, I bought the starter kit Arduino Uno and a Radio Shack RFID with two key fobs, I'm using a script from someone elses setup. I can get one key working but not the other they both come up in the monitor, so am I missing something here? Any help many thanks..
#define CODE_COUNT 2
#define MIN_ROTATION 150
#define MAX_ROTATION 30
int val = 0;
char code[10];
int bytesread = 0;
int ledPin = 13;
int speakerOut = 5;
int firstworked = 0;
char *goodCode1 = "1700BD4917";
char *goodCode2 = "1700BD4876";
int Cnote = 1915;
int Dnote = 1000;
#include <Servo.h>
Servo servo;
void soundwave(int note, int howHard ) { // function that takes 2 parameters, the note we want to play and how hard the sensor has been hit
unsigned long endSoundWave = micros() + (35000 * howHard); // start a count from the microseconds currently registered since the program first ran. And add on an arbitary value multiplied by howHard value
while(micros() < endSoundWave){ // while the count is not reached
analogWrite(speakerOut, 1023); // set the speaker to on/ high
delayMicroseconds(note); // wait the number of microseconds for the note
analogWrite(speakerOut, 0); // set the speaker to off/ low
delayMicroseconds(note); // wait for the same number again to complete our oscillation/ soundwave.
} // repeat for the length of time.
}
void setup() {
Serial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps
pinMode(2,OUTPUT); // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
digitalWrite(2, LOW); // Activate the RFID reader
pinMode(ledPin, OUTPUT);
servo.attach(3);
pinMode(speakerOut,OUTPUT);
analogWrite(speakerOut, 0);
}
void loop() {
if(Serial.available() > 0) { // if data available from reader
if((val = Serial.read()) == 10) { // check for header
bytesread = 0;
while(bytesread<10) { // read 10 digit code
if( Serial.available() > 0) {
val = Serial.read();
if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading
break; // stop reading
}
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
}
}
if(bytesread == 10) { // if 10 digit read is complete
Serial.print("TAG code is: "); // possibly a good TAG
Serial.println(code); // print the TAG code
boolean success = true;
firstworked = 1;
for(int i=0;i<10;i++) {
if(goodCode1[i] != code[i]) {
success = false;
Serial.print("BAD TAG SUKKA");
firstworked = 0;
break;
}
}
if(success){
digitalWrite(ledPin, HIGH);
soundwave(500, 5);
servo.write(MAX_ROTATION);
delay(1000);
analogWrite(speakerOut, 0);
}
else {
soundwave(2000, 20);
}
}
bytesread = 0;
delay(500);
}
}
digitalWrite(ledPin, LOW);
servo.write(MIN_ROTATION);
analogWrite(speakerOut, 0);
}
Sorry I forgot to remove this line. if true (
this should be in the code... I just don't no what to do with the
char *goodCode2 = "1700BD4876";
again thank you for your help on this.
Sorry about that I see the button now... But still don't know if this is what you wanted.[/code]
#define CODE_COUNT 2
#define MIN_ROTATION 150
#define MAX_ROTATION 30
int val = 0;
char code[10];
int bytesread = 0;
int ledPin = 13;
int speakerOut = 5;
int firstworked = 0;
char *goodCode1 = "1700BD4917";
char *goodCode2 = "1700BD4876";
int Cnote = 1915;
int Dnote = 1000;
#include <Servo.h>
Servo servo;
void soundwave(int note, int howHard ) { // function that takes 2 parameters, the note we want to play and how hard the sensor has been hit
unsigned long endSoundWave = micros() + (35000 * howHard); // start a count from the microseconds currently registered since the program first ran. And add on an arbitary value multiplied by howHard value
while(micros() < endSoundWave){ // while the count is not reached
analogWrite(speakerOut, 1023); // set the speaker to on/ high
delayMicroseconds(note); // wait the number of microseconds for the note
analogWrite(speakerOut, 0); // set the speaker to off/ low
delayMicroseconds(note); // wait for the same number again to complete our oscillation/ soundwave.
} // repeat for the length of time.
}
void setup() {
Serial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps
pinMode(2,OUTPUT); // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
digitalWrite(2, LOW); // Activate the RFID reader
pinMode(ledPin, OUTPUT);
servo.attach(3);
pinMode(speakerOut,OUTPUT);
analogWrite(speakerOut, 0);
}
void loop() {
if(Serial.available() > 0) { // if data available from reader
if((val = Serial.read()) == 10) { // check for header
bytesread = 0;
while(bytesread<10) { // read 10 digit code
if( Serial.available() > 0) {
val = Serial.read();
if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading
break; // stop reading
}
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
}
}
if(bytesread == 10) { // if 10 digit read is complete
Serial.print("TAG code is: "); // possibly a good TAG
Serial.println(code); // print the TAG code
boolean success = true;
firstworked = 1;
for(int i=0;i<10;i++) {
if(goodCode1[i] != code[i]) {
success = false;
Serial.print("BAD TAG SUKKA");
firstworked = 0;
break;
}
}
for(int i=0;i<10;i++) {
if(goodCode2[i] != code[i]) {
success = false;
Serial.print("BAD TAG SUKKA");
firstworked = 0;
break;
}
}
if(success){
digitalWrite(ledPin, HIGH);
soundwave(500, 5);
servo.write(MAX_ROTATION);
delay(1000);
analogWrite(speakerOut, 0);
}
else {
soundwave(2000, 20);
}
}
bytesread = 0;
delay(500);
}
}
digitalWrite(ledPin, LOW);
servo.write(MIN_ROTATION);
analogWrite(speakerOut, 0);
}
What you are doing here:-
char goodCode1 = "1700BD4917";
is generating a pointer to the start of a string.
Then you use that as if it were an array:-
if(goodCode1 _!= code)*_ If I was writing this I would declare it as an array to begin with then I think you would find the test would work correctly.
@Grumpy_Mike
That method of allocation and pointer assignment, and then using array notation works just fine. There is nothing wrong with it. There would be if the code was incrementing the pointer.
I copy this and now the first key doesn't work
What happens when you scan the first tag? Does it print BAD TAG SUKKA? If that is the "problem", the solution is to change the message(s). Print "This is not tag 1" and "This is not tag 2" where you currently print "BAD TAG SUKKA". I think that you will see that it is telling you that the tag 1 is not tag 2.
So, when you scan tag 1, it tells you that it is not tag 2, and when you scan tag 2, it tells you that it is not tag 1. What is the problem?
It seems to me that you want to print a message only when the tag is valid.
Or, set a boolean variable to false. Set it to true if either loop completes (the tag is valid). After both loops, if it is still false, the tag is not valid. If it is true, the tag is valid.
Not sure if anyone would like it but here is the working code just add
your own key fob numbers.. Thanks again everyone for the help.
#define CODE_COUNT 2
#define MIN_ROTATION 150
#define MAX_ROTATION 30
int val = 0;
char code[10];
int bytesread = 0;
int ledPin = 13;
int speakerOut = 5;
int firstworked = 0;
char *goodCode1 = ""; // Enter your key fob numbers here
char *goodCode2 = ""; // Enter your key fob numbers here
int Cnote = 1915;
int Dnote = 1000;
#include <Servo.h>
Servo servo;
void soundwave(int note, int howHard ) { // function that takes 2 parameters, the note we want to play and how hard the sensor has been hit
unsigned long endSoundWave = micros() + (35000 * howHard); // start a count from the microseconds currently registered since the program first ran. And add on an arbitary value multiplied by howHard value
while(micros() < endSoundWave){ // while the count is not reached
analogWrite(speakerOut, 1023); // set the speaker to on/ high
delayMicroseconds(note); // wait the number of microseconds for the note
analogWrite(speakerOut, 0); // set the speaker to off/ low
delayMicroseconds(note); // wait for the same number again to complete our oscillation/ soundwave.
} // repeat for the length of time.
}
void setup() {
Serial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps
pinMode(2,OUTPUT); // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
digitalWrite(2, LOW); // Activate the RFID reader
pinMode(ledPin, OUTPUT);
servo.attach(3);
pinMode(speakerOut,OUTPUT);
analogWrite(speakerOut, 0);
}
void loop() {
if(Serial.available() > 0) { // if data available from reader
if((val = Serial.read()) == 10) { // check for header
bytesread = 0;
while(bytesread<10) { // read 10 digit code
if( Serial.available() > 0) {
val = Serial.read();
if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading
break; // stop reading
}
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
}
}
if(bytesread == 10) { // if 10 digit read is complete
Serial.print("TAG code is: "); // possibly a good TAG
Serial.println(code); // print the TAG code
boolean success = true;
firstworked = 1;
for(int i=0;i<10;i++) {
if(goodCode1[i] != code[i]) {
success = true;
Serial.print("this is not tag 1");
firstworked = 0;
break;
}
}
for(int i=0;i<10;i++) {
if(goodCode2[i] != code[i]) {
success = true;
Serial.print("this is not tag 2");
firstworked = 0;
break;
}
}
if(success){
digitalWrite(ledPin, HIGH);
soundwave(500, 5);
servo.write(MAX_ROTATION);
delay(1000);
analogWrite(speakerOut, 0);
}
else {
soundwave(2000, 20);
}
}
bytesread = 0;
delay(500);
}
}
digitalWrite(ledPin, LOW);
servo.write(MIN_ROTATION);
analogWrite(speakerOut, 0);
}