Digital outpin pins (new user) :]

Hi, so this is my first projects that I have really tried to do any coding with, im new so take it easy on me please :]

I see a lot of people try rfid door lock projects and there is a ton of info in the web I have been using. And I know there is already completed code, but I really want to try this myself, so I am editing a code from another user to do what I want.

I started with post #4 of this topic
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1192543512

In the code I added 2 more digitalWrite pin outputs to control relays for locking and unlocking a car door (uses a actuator)

This is what I have so far, it gives me a error when compiling

#define TAG_LEN 12

char tag[12] = {‘1’, ‘9’, ‘0’, ‘0’, ‘2’, ‘2’, ‘4’, ‘D’, ‘D’, ‘A’};
char code[12];
int bytesread = 0;
int redPin = 13; // Connect red LED to pin 13
int grnPin = 8; // Connect green LED to pin 8
int lockPin = 7; // Connect to relay for lock
int unPin = 6; // Conect to relay for unlock
int rfidPin = 2; // RFID enable pin connected to digital pin 2
int val=0;
long time;
long lastUpdate=0;
void setup() {

Serial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps
pinMode(rfidPin,OUTPUT); // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
pinMode(redPin,OUTPUT); // Set ledPin to output
pinMode(grnPin,OUTPUT);
digitalWrite(rfidPin, LOW); // Activate the RFID reader

}

void loop() {
time = millis();
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
if(strcmp(code, tag) == 0) {
Serial.print("Tag matches: ");
Serial.println(code);
lastUpdate = millis();
Serial.print(“time elapsed = “);
Serial.println(time);
digitalWrite(grnPin,HIGH); // good read light green LED
digitalWrite(redPin,LOW); // turn off red LED
digitalWrite(doorPin,LOW);
digitalWrite(unPin,HIGH); // unlock
}
else {
Serial.print(code);
Serial.println(” does not match”);
digitalWrite(redPin,HIGH); // not recognized tag light red LED
digitalWrite(grnPin,LOW); // turn off green LED
digitalWrite(lockPin,HIGH); // lock door
digitalWrite(unPin, LOW);
}
}
bytesread = 0;
delay(500); // wait for a 1/2 second
}
}
if((time - lastUpdate) >= 60000) {
blink_fast();
}
}

void blink() {
digitalWrite(redPin, HIGH);
delay(250);
digitalWrite(redPin, LOW);
delay(250);
}

void blink_fast() {
Serial.print("Time = ");
Serial.println(time);
digitalWrite(grnPin,LOW); //turn off green led
digitalWrite(redPin, HIGH);
delay(50);
digitalWrite(redPin, LOW);
delay(50);
}

// extra stuff
// digitalWrite(2, HIGH); // deactivate RFID reader

Im still a little confised about what I did wrong, the error is:

In function ‘void loop()’:
error: ‘doorPin’ was not declared in this scope

Also from looking at this code, and comparing it with other door lock codes I am unsure on how to change how long the time for it to be open/close is.

Any help would be great.

Thanks,
Blake

EDIT:
I noticed that when I copied the code to the forum it shifted my Digitalwrite for controlling he HIGH and LOW out of line with the others. When I typed it I just did spaces, is this correct?

I don’t see where you declared ‘doorPin’.
Did you mean lockPin?

doorpin is not declared at the top of the program. So or the line digitalWrite(doorPin,LOW); needs to be removed or doorPin must be changed in lockpin ? which is used in the else part with opposite flag.

oops, that fixed it thanks :] I didnt even notice I did that.

Any ideas on the part about how long the pins are held high/low? Do I just need to change the value for this delay?

delay(500); // wait for a 1/2 second

just give it a try and you will know ;)

Ha I was just now :]

Im just plugging in a small dc motor in place of the actuator for now, and when I swipe the correct card it, spins, and spins till I swipe the "bad" card.

I tried editing

delay(500); // wait for a 1/2 second

and

if((time - lastUpdate) >= 60000) {

But both seemed to do nothing that I notice, at least related to my current problems :[

Time for google ha.

EDIT: Also I dont guess i need to beable to lock and unlock it with rfid, just unlock. So That should help me simplify the coding and the hardware some.

Numbers entered in a program are assumed to be integers. 60000 is NOT an integer, which has an upper limit of 32767. When the compiler interpreted 60000 as in integer, it got -27233, which might explain why your code does not produce the values you expect.

You can tell the compiler to treat 60000 as an unsigned long by adding UL to the end:

if((time - lastUpdate) >= 60000UL)

See if that gives better results.

That worked great man, thanks!

But could you help me understand why that worked? You lost me ha. Or just a link would be fine.

EDIT: its working great :] My only other thing is, I guess because its running and 2400 baud it will some times read it multiple time in the like 1/2 sec i have the card in its rang, any thing I could do about that?

The UL on the end tells the compiler to treat the value as an unsigned long. Since the test now involves all unsigned long values, the result is what you expect.

Before the statement involved different types (and values) from what you saw, with unexpected/incorrect results.

it will some times read it multiple time in the like 1/2 sec i have the card in its rang, any thing I could do about that?

Don't leave the card in range that long.

Or, set a flag when the door is unlocked, and ignore tags until the door is locked again. Of course, this requires knowing when the door gets locked.