Go Down

Topic: Weird Delay Before Code Starts (Read 1 time) previous topic - next topic


I am having an issue with code not starting when the chip is powered up. I am using a stand alone ATMEGA328 (programmed with the arduino ide and with the arduino bootloader). I have successfully run code on this chip (in the exact same circuit) without any issues. Here is the code that has a delay (about 1-2 minutes) before the program gets to the main loop:

Code: [Select]
#include <NewSoftSerial.h>  //this library communicates with the rfid reader

NewSoftSerial rfid= NewSoftSerial(10, 11);  //connect to the rfid reader on pins 10 (rx) and 11 (tx)
byte cards[360];  //this is the master list of cards allowed to open the doors
byte gcode[6];  //this value is array is used to hold the values read to be used globally

int check = 0;  //this value is the counter used in checking the card values
int lock = 12;  //attach the base of the MOSFET to control the lock to pin 9
int numcards = 0;  //this is the active number of cards in the network

const int CARDLIMIT = 60;
const int ADD_CARD = 1;
const int UPDATE_DATA = 2;
const int EXECUTE = 3;
const int CONTINUE = 4;

int command;
int ledBlue = 5;
int ledRed = 6;
int ledGreen = 7;

//Xbee information
byte input[6];  //this is the array that stores data recieved from the XBEE
byte output[6];  //this array holds the values to be sent to the XBEE (server)
boolean data_fail;  //this is used to sigify that the data available from the xbee is mor that the allowable
boolean match;

void setup() {
  pinMode(lock, OUTPUT);  //Set the lock pin as an output
  //digitalWrite(lock, HIGH);
  pinMode(ledBlue, OUTPUT);
  pinMode(ledRed, OUTPUT);
  pinMode(ledGreen, OUTPUT);

  Serial.begin(9600);   // connect to the XBEE via serial communication
  rfid.begin(9600);  //begin the virual serial connection with the rfid reader

void loop () {   //THIS IS MY MAIN LOOP
  digitalWrite(lock, HIGH);

  //Check Card ID, Transmit the ID, Unlock the door if necessary
  if(Serial.available() > 0){
    command = Serial.read();
    if(command == ADD_CARD || command == UPDATE_DATA) {



///////////////////////////LAND OF THE FUNCITONS/////////////////////////////////////

void unlock()
  digitalWrite(lock, LOW);  //unlock the door
  digitalWrite(ledRed, LOW);
  digitalWrite(ledGreen, HIGH); //Green

  for(int i=0; i<6; i++){
  digitalWrite(ledGreen, LOW);
  digitalWrite(ledRed, HIGH);

  digitalWrite(lock, HIGH);  //lock the door

boolean checkid() {
  boolean found = false;
  for(int i=0;i<CARDLIMIT;i++) {
    if(gcode[0] == cards[6*i]) {
      for(int j=0;j<6;j++) {
        if(gcode[j] != cards[(6*i)+j]) {
        } else if(j == 5 && gcode[5] == cards[(6*i)+5]) {
          found = true;
    if(found == true) {
  return found;

byte scan_card()
  byte i = 0;
  byte val = 0;
  byte checksum = 0;
  byte bytesread = 0;
  byte tempbyte = 0;
  byte code[6];
  byte gsum = 0;
  if(rfid.available() > 0) {
  if((val = rfid.read()) == 2) {                  // check for header
    bytesread = 0;
    while (bytesread < 12) {                        // read 10 digit code + 2 digit checksum
      if( rfid.available() > 0) {
        val = rfid.read();
        if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) { // if header or stop bytes before the 10 digit reading
          break;                                    // stop reading

        // Do Ascii/Hex conversion:
        if ((val >= '0') && (val <= '9')) {
          val = val - '0';
        } else if ((val >= 'A') && (val <= 'F')) {
          val = 10 + val - 'A';

        // Every two hex-digits, add byte to code:
        if (bytesread & 1 == 1) {
          // make some space for this hex-digit by
          // shifting the previous hex-digit with 4 bits to the left:
          code[bytesread >> 1] = (val | (tempbyte << 4));

          if (bytesread >> 1 != 5) {                // If we're at the checksum byte,
            checksum ^= code[bytesread >> 1];       // Calculate the checksum... (XOR)
        } else {
          tempbyte = val;                           // Store the first hex digit first...

        bytesread++;                                // ready to read next digit
   if (bytesread == 12) {  //check to see if the right number of bytes were read

    for(i=0; i<6; i++)  //this loop transfers the actual code
      gcode[i] = code[i];  //this function moves the code one at a time
  for(int i=6;i<6;i++) {
    gsum += gcode[i];
  bytesread = 0;  //this is part of the code that decodes the message from the rfid reader
  return gsum;

void execute_command(int command) {
  digitalWrite(ledRed, LOW);

  digitalWrite(ledBlue, HIGH);
  while(Serial.read() != CONTINUE) {
    if(Serial.read() == EXECUTE) {
      if(command == ADD_CARD) {
        for(int i=0;i<6;i++) {
          gcode[i] = 0;
        byte sum = 0;
        while(sum == 0) {
          sum = scan_card();
        for(int i=0;i<6;i++) {
        command = UPDATE_DATA;
        while(Serial.read() != EXECUTE) {
      if(command == UPDATE_DATA) {
        for(int i=0;i<360;i++) {
          cards[i] = Serial.read();
        numcards = Serial.read();
  digitalWrite(ledBlue, LOW);
  digitalWrite(ledRed, HIGH);

I have uploaded this code onto the ATMEGA328 and given it power. But it takes about 1-2 minutes for the program to reach the main loop. I think that this is the case because the first thing in the main loop is to turn pin 12 to HIGH. It take about 1-2 minutes (from when the power is switched on) for pin 12 to go high. I think that this is a problem with the code because I have been able to run code (that only turned on pin 12, that's it) and it worked fine. The circuit is in two parts (one part goes indoors and the other goes outdoors) and is on a pcb. Basically this is a rfid door lock system that sends information to a computer via an xbee network. I have attached images of the indoor and outdoor schematics as well. Anyway the real problem is that the program takes about 1-2 minutes to get into the main loop (At least it appears that way). Any help is greatly appreciated.


What is the mosfet device number drive the door lock? If it's not a logic level mosfet that might be a source for your apparent 'delay' to turning on.



The mosfet is the RFP30N06LE. It is the one they sell at Sparkfun (http://www.sparkfun.com/products/10213). That may be the case but I didn't notice any delay when the code was working. But I could be wrong. I think there is a datasheet on that link also if you want have a look.


The mosfet is the RFP30N06LE. It is the one they sell at Sparkfun (http://www.sparkfun.com/products/10213). That may be the case but I didn't notice any delay when the code was working. But I could be wrong. I think there is a datasheet on that link also if you want have a look.

Then that's not your problem as that is a logic level mosfet.


How about some debugging flashes? Put an LED on pin D2, and make a small "flash the LED" routine (like on for 500 ms, pause 500 ms, off again).

Then call it once immediately at the start of setup. And then two times after Serial.begin and 3 times after rfid.begin. See how many flashes you get.

Is that really a 10 millifarad capacitor on the VCC line? 10000 microfarads?
Please post technical questions on the forum, not by personal message. Thanks!

More info:


I presume the cct you supplied is all the components.?
You don't seem to have included any High frequency bypass capacitors.
Digital electronics (esp TTL devices) need a 0.1uF capacitor across every IC, to stop high frequency spikes upsetting other devices.

The larger capacitors are good at smoothing out larger voltage flucuations, but they are uesless at the fast high frequency spikes.
I would include one acoss pin 7 and 22 (the supply pins) of the 328, the +5v and gnd of the xbee, and both of the 10uF caps on your regulator.

its also good practice to include caps across the supply to other boards, or the incoming supply.

Good luck


Wow those are all great suggestions. I will add debugging flashes the next chance that I get. Also the capacitor you speak of (Nick Gammon) is a 10 microfarad. I didn't know exactly how to type in the character "ยต" into eagle at the time. Also I had never thought about bypass capacitors (in fact I just figured out what they were). I will add those to the 328, xbee (it is on the xbee adapter from adafruit though), and on both ends of the wires to the outside board. The final length of the wire that connects the two boards will probably be about 5 feet and will need the capacitors for sure. Also I will include them on the rfid reader on the outside board (it is shown as 2 headers on the schematic but is really the id-20 rfid reader). Hopefully that will clear up some of my problems. I will try it out and reply as soon as possible about how well it works.


It could be the   scan_card()   function as it waits until 12 bytes are received. If one is missed it might take quite a while...

Code: [Select]
    while (bytesread < 12) {                        // read 10 digit code + 2 digit checksum
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)


It very well could be and it may not be receiving these because of the circuit. But I turned on the lock (it is a fail safe lock) before I called scan_card(). It still had the delay. I will try skipping that function to see if it gets rid of the delay. But the more I think about it I am sure it is the circuit. I noticed that while it is in the delay the rgb led is blue and the correct brightness. But after the delay, when the lock turns on, the led goes to red (which it is supposed to) but the brightness is considerably lower.


Well I added bypass capacitors to both the indoor and outdoor circuits (except the xbee because there is already one on the adapter I am using). Unfortunately it did not stop the delay but I have a hunch that it makes the circuit run better. For one thing the regulator isn't getting as hot. I need to try the debugging flashes still but have not found the time. I should be able to to it within the next few days however as time permits. Anyway thanks for the suggestion (about the bypass capacitors) I certainly learned something new that I didn't know before (I did some more research on bypass capacitors). I will try the debugging flashes and see how that goes soon.

Go Up