building a door lock. Arduino freezes after 2 days

hi
i am trying to build a 12v door lock connected to a Arduino uno . it works perfectly for about 2 days and after that the arduino freezes and requires a power reset to function again. please help.

Thanks in advance

Get working a watch dog in your code.

hi
i am a newbie can you please explain what is a watch dog.

thanks

Raj

Watch dog works like the literal meaning of watch dog that is it can be used to get your microcontroller out of sleep or logical blocking or hanging.

For how to use it read ATmega328s datasheet and also look on the Arduino Playground.

alternatively lookonto your code that what are you doing wrong? that is causing the chip land into deadlock.

The Atmega328, in common with most microprocessors has a device called a watch-dog-timer. When you set it going it expects your code to reset it within X milliseconds and if it doesn't the watch-dog assumes your code has crashed and it causes the Arduino to reset.

It's not trivial to use, however, because when it is set it interferes with the bootloader when you want to update your sketch.

If your code is not too long post a copy here (between code tags please) and someone may spot what's causing the trouble. I have an Atmega328 controlling my fridge and its been running for several months.

...R

i was wondering if need a transistor between arduino signal pin and relay and also whether a diode is required on the relay and door strike to control emf which can hang the arduino.

#include <SPI.h>
#include <Ethernet.h>
#define BUFSIZ 64

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  
  0xDE, 0xAD, 0xBD, 0xEF, 0xFE, 0xED 
};
IPAddress ip(192,168,1,110);
// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
EthernetServer iPhoneServer(2560);

int Led9 = 9;
int Led9status = 0;

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  // print your local IP address:
   Serial.println(Ethernet.localIP());
   iPhoneServer.begin();           // start the server

  pinMode(Led9, OUTPUT);  
}
void loop()
{
  iPhoneInterface();

}



void iPhoneInterface() {

  int index = 0;
  char Remote[BUFSIZ];

  EthernetClient client = iPhoneServer.available();
  if (client) {
    if (client.connected()) {
      while (client.available()) {
        char c = client.read();
        //Serial.print(c);                      // print to the screen
        if (c != '\n' && c != '\r') {           // no linefeed or CR so keep reading
          Remote[index] = c;
          index++;
          if (index >= BUFSIZ) 
            index = BUFSIZ -1;
          continue;
        }
        Remote[index] = '\0';
      }

      Serial.println(Remote);                  // print string sent from iPhone

        if (strstr(Remote, "Local")) {           // initialsend
        client.println("OK");
      }

      

      if (strstr(Remote, "LED9 status")) {   // set the LED9 switch according the Status
        client.println(Led9status);          // answer with the onText
      }

      if (strstr(Remote, "LED9 on")) {       // set the LED9
        client.println("OK");
        digitalWrite(Led9, HIGH);        // turn the LED9 on (HIGH is the voltage level)
        Led9status = 1;

      }

      if (strstr(Remote, "LED9 off")) {      // set the LED9
        client.println("OK");
        digitalWrite(Led9, LOW);             // turn the LED9 off (LOW is the voltage level)
        Led9status = 0;
      }

      
      }

      


    } //----- End of if client.connected
  } //------- End of if client

i am using the netio app from my iphone to control the arduino

You are probably running out of RAM. ditch any use of the String class (use string.h instead) and store string constants in Flash.

rajbadri:
i was wondering if need a transistor between arduino signal pin and relay and also whether a diode is required on the relay and door strike to control emf

'Yes' to both of those. The Arduino has a maximum current of 40mA per I/O pin and it's recommended to keep below 20mA. Very few relays will operate on 40 mA or less. Relays and other inductive loads also need a flyback diode to avoid a back EMF spike from trying to stop the current abruptly.

i was wondering if need a transistor between arduino signal pin and relay and also whether a diode is required on the relay and door strike to control emf which can hang the arduino.

External power to the relay.

diode.

and transistor are very much needed, I wonder how you started it even without this! google drive relay arduino to get the circuitry connections.

Your code seems to be willing to accept any number of bytes from the IPhone - apparently into a circular buffer. You add a 0 at the end of the received data to make a null terminated string. It looks as if, sometimes, the 0 will go into the first location in the buffer. I wonder what that does for the Serial.println(Remote); statement?

If you have control of what the IPhone sends I suggest you get it to enclose the data between start and end markers and write your Arduino code to work with a max of X characters. Normally the IPhone will send less than X, but if there are more characters the whole thing should be rejected and the Arduino should wait for the next valid data.

Even if you don't have control of the IPhone code I suggest you make the Arduino reject messages longer than X.

...R

is this the right schematic for a arduino door lock

thanks in advance

Raj

rajbadri:
is this the right schematic for a arduino door lock

thanks in advance

Raj

That is not a real schematic but rather a fritizing imitation so it’s hard to tell for sure, however I think you have the diode in the wrong place. It needs to wire directly across the two coil terminals of the relay.

On another account Fritzing sucks!
I think you need some real circuit programme or just make the circuit on paper and photo it and attach finally.

Hi
I have rewired my arduino according to the image i have attached will someone please guide me if its correct.

Thanks in advance.

Raj

What problems you are encountering now? Still the freezing?

yes works for 15-20 hrs and then arduino freezes needs a reset. but the ethernet shield is active and answers to ping

Thanks

Raj

some how a timer counter is adding off bounds and not overflowing getting chip hung.
Please post code also. the latest code.

this is the latest code

/*

 
 Net IO Project
 
 */

// include library 
#include <SPI.h>
#include <Ethernet.h>

// the media access control (ethernet hardware) address for the shield:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  
//the IP address for the shield:
byte ip[] = { 192, 168, 1, 110 };    

//----- iPhone Remote -------

#define BUFFER 20
EthernetServer iPhoneServer(2560);      // server port number of Ethernet shield
int Led9 = 9;
int Led6 = 6;

int Led9status = 0;
int Led6status = 0;

void setup() {
  Serial.begin(9600);

  // initialize the ethernet device
  Ethernet.begin(mac, ip);
  //print out the IP address
  Serial.println("IP = ");
  Serial.print(Ethernet.localIP());
  
  iPhoneServer.begin();           // start the server

  pinMode(Led9, OUTPUT);    
  pinMode(Led6, OUTPUT);
  
}
// end setup

// main loop
void loop()
{
  iPhoneInterface();

}


void iPhoneInterface() {

  int index = 0;
  char Remote[ BUFFER ];

  EthernetClient client = iPhoneServer.available();
  if (client) {
    if (client.connected()) {
      while (client.available()) {
        char c = client.read();
        //Serial.print(c);                      // print to the screen
        if (c != '\n' && c != '\r') {           // no linefeed or CR so keep reading
          Remote[index] = c;
          index++;
          if (index >= BUFFER) 
            index = BUFFER -1;
          continue;
        }
        Remote[index] = '\0';
      }

      Serial.println(Remote);                  // print string sent from iPhone

        if (strstr(Remote, "Local")) {           // initialsend
        client.println("OK");
      }
      if (strstr(Remote, "LED9 status")) {   // set the LED9 switch according the Status
        client.println(Led9status);          // answer with the onText
      }
      if (strstr(Remote, "LED9 on")) {       // set the LED9
        client.println("OK");
        delay (300) ;
        digitalWrite(Led9, HIGH);            // turn the LED9 on (HIGH is the voltage level)
        Led9status = 1;
        
      }

      if (strstr(Remote, "LED9 off")) {      // set the LED9
        client.println("OK");
        delay (300) ;
        digitalWrite(Led9, LOW);             // turn the LED9 off (LOW is the voltage level)
        Led9status = 0;
        
      }

     if (strstr(Remote, "LED6 status")) {   // set the LED6 switch according the Status
        client.println(Led9status);          // answer with the onText
      }
      if (strstr(Remote, "LED6 on")) {       // set the LED6
        client.println("OK");
        delay (300) ;
        digitalWrite(Led6, HIGH);            // turn the LED6 on (HIGH is the voltage level)
        Led6status = 1;
        
      }

      if (strstr(Remote, "LED6 off")) {      // set the LED9
        client.println("OK");
        delay (300) ;
        digitalWrite(Led6, LOW);             // turn the LED9 off (LOW is the voltage level)
        Led6status = 0;
        
      }

 } //----- End of if client.connected
 } //------- End of if client
} //--------- End of iPadInterface()

It appears this is server code. I don't see where you are closing the connection. That is the server's duty to close the connection when it is finished sending. You need a client.stop() somewhere or you will probably run out of sockets. There are only 4 of them.