Arduino UNO R3, keeps on resetting itself on regular intervals.

Hello, I'm using arduino UNO and it keeps on resetting it self on regular intervals.
Initially, it kept on resetting it self after every 6 minutes and 48 seconds. I experimented a little by taking out the statement "serial.begin(9600)" and now my arduino resets itself after every 50 seconds. I really would appreciate any kind of help.

here is my code

int no;
int case0=0;
int case1=0;
int case2=0;
int case3=0;
int case4=0;
int case5=0;


void setup() {

  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(A3,OUTPUT);
  pinMode(8,INPUT);
  pinMode(9,INPUT);
  pinMode(10,INPUT);
  pinMode(11,INPUT);
  pinMode(12,INPUT);
  digitalWrite(0,LOW);
  digitalWrite(1,LOW);
  
  
  //CODE HERE IS RUNNING FOR THE FIRST TIME, NOW BEFORE THE PROGRAME GOES INTO THE LOOP, THIS WOULD RUN FIRST AND CAN GIVE YOU THE IDEA OF PORTS THAT ARE EIETHER ON OR OFF.
  start:
  
  int sensorValue = analogRead(A4);
  float voltage = sensorValue * (5.0 / 1023.0);
  
  if(voltage<=0.5)
  {
    digitalWrite(2,HIGH);
  }
  else if(voltage >=0.51 && voltage <=1.50)
  {
    digitalWrite(3,HIGH);
    
  }
  else if(voltage >=1.51 && voltage <2.5)
  {
    digitalWrite(4,HIGH);
  }
  else if(voltage >= 2.51 &&voltage<3.5)
  {
    digitalWrite(5,HIGH);
  }
  else if(voltage > 3.51 && voltage<4.5)
  {
    digitalWrite(6,HIGH);
  }
  else if(voltage >= 4.51)
  {
    digitalWrite(A3,HIGH);
  }
  else
  {
    goto start; // If values are in between those spacing for protection, this will take the function to start and will start all over again.
  }
 
  
}

// the loop routine runs over and over again forever:
void loop() {
  

  
  no = checkno(); //-------------------------> THIS
  
  int sensorValue = analogRead(A4);
  float voltage = sensorValue * (5.0 / 1023.0);
  Serial.println(voltage); 
  
  int casevalue = getcasevalue(voltage);
  if(casevalue==10)
  {
    casevalue = getcasevalue(voltage);    // another scheme for protection
  }
  
  runcase(casevalue);
   
  
}


int getcasevalue(float voltage1)
{
  int valueforcase;
  if(voltage1 <= 0.5)
  {
    valueforcase = 0;
  }
  else if(voltage1 >= 0.51 && voltage1 <= 1.5)
  {
    valueforcase =1;
    
  }
  else if(voltage1 >= 1.52 && voltage1 <= 2.5)
  {
    valueforcase=2;
  }
  else if(voltage1>= 2.52 &&voltage1<=3.5)
  {
    valueforcase=3;
  }
  else if(voltage1>=3.52 && voltage1<=4.5)
  {
    valueforcase=4;
  }
  else if(voltage1 >= 4.52)
  {
    valueforcase=5;
  }
  else
  {
   return 10;
  }
  return valueforcase;

}

void runcase(int casevalue)
{
  int volt = signal();
  switch(casevalue)
  {
     case 0:
     if(no==8) // this is because, you need to keep on c1 if UV IS ON
     {
       digitalWrite(2,HIGH);
       break;       
     }
    if(case0==0)
     {   //BRACKETS OFF IFF HAVE BEEN EXTENDED AND FOR LOOP IS NOW A PART OF THIS IFF, CONDITION FOR EXPERIMENT PURPOSES
      digitalWrite(7,HIGH);
      delay(100);
          
     for(int i=2 ; i<7 ; i++)
     {
       digitalWrite(i,LOW);
       digitalWrite(A3,LOW);
     }
     
     }
     if(digitalRead(no)==LOW)
     {
       digitalWrite(2,HIGH);
       
     }
            
   case0++;
   case1=0;
   case2=0;
   case3=0;
   case4=0;
   case5=0;
   digitalWrite(7,LOW);
   
   break;
   
              
   case 1:
   digitalWrite(2,LOW);   
   
   if(case1==0)
   {
     digitalWrite(7,HIGH);
     delay(100);
   
   for(int i=2 ; i<7 ; i++)
     {
       digitalWrite(i,LOW);
       digitalWrite(A3,LOW);
     }
     
   }
     if(digitalRead(no)==LOW)
     {
       digitalWrite(3,HIGH);
       
     }    
   case0=0;
   case1++;
   case2=0;
   case3=0;
   case4=0;
   case5=0;
   if(digitalRead(3+5)==HIGH)
   {
     digitalWrite(7,LOW);
   }   
   break;
      
    case 2:
    if(case2==0)
   {
     digitalWrite(7,HIGH);
     delay(100);
   
   for(int i=2 ; i<7 ; i++)
     {
       digitalWrite(i,LOW);
       digitalWrite(A3,LOW);
     }
     
   }
     if(digitalRead(no)==LOW)
     {
       digitalWrite(4,HIGH);
       
     }
   case0=0;
   case1=0;
   case2++;
   case3=0;
   case4=0;
   case5=0;
   if(digitalRead(4+5)==HIGH)
   {
     digitalWrite(7,LOW);
     
   }
        
      break;
      
    case 3:
    if(case3==0)
   {
     digitalWrite(7,HIGH);
     delay(100);
   
   for(int i=2 ; i<7 ; i++)
     {
       digitalWrite(i,LOW);
       digitalWrite(A3,LOW);
     }
     
   }
     if(digitalRead(no)==LOW)
     {
       digitalWrite(5,HIGH);
       
     }
   case0=0;
   case1=0;
   case2=0;
   case3++;
   case4=0;
   case5=0;
   
   if(digitalRead(5+5)==HIGH)
   {
     digitalWrite(7,LOW);
     
   }
     
      break;
          
    case 4:
    digitalWrite(A3,LOW);
    
    if(case4==0)
   {
     digitalWrite(7,HIGH);
     delay(100);
   
   for(int i=2 ; i<7 ; i++)
     {
       digitalWrite(i,LOW);
       digitalWrite(A3,LOW);
     }
     
   }
     if(digitalRead(no)==LOW)
     {
       digitalWrite(6,HIGH);
       
        }
   case0=0;
   case1=0;
   case2=0;
   case3=0;
   case4++;
   case5=0;
   
   if(digitalRead(6+5)==HIGH)
   {
     digitalWrite(7,LOW);
     
   }
   
   break;
      
    case 5:
    if(digitalRead(11)==HIGH)
    {
      digitalWrite(A3,HIGH);
      break;      
    }
    
       
    if(case5==0)
   {
     digitalWrite(7,HIGH);
     delay(100);
   
   for(int i=2 ; i<7 ; i++)
     {
       digitalWrite(i,LOW);
       digitalWrite(A3,LOW);
     }
     
   }
     if(digitalRead(no)==LOW)
     {
       digitalWrite(A3,HIGH);
       
     }
   case0=0;
   case1=0;
   case2=0;
   case3=0;
   case4=0;
   case5++;
   digitalWrite(7,LOW);
   
   break;    
           
  }
}

int signal()
{
  int sensorValue = analogRead(A4);
  float voltage = sensorValue * (5.0 / 1023.0);
  return voltage;
}

int checkno()
{
  for(int i=8; i<13 ; i++)
  {
    int val = digitalRead(i);
    if(val==HIGH)
    {
      return i;
      break;
    }
  }
}

There is a memory leak.
In the Playground is a function to return free memory that you can print to console. Add the function and put in a print statement to locate which function is leaky.

Ray

I'm sorry, i forgot to mention I'm new to arduino. Could you please be a little more elaborative or add in the function to see memory leakage or stack overflow in my code and repost.

Sure:
See freeRam() at bottom of:
AvailableMemory

Read this:

Ray

someone have to help me out please, I still can't cater to my problem.

How do you know it is resetting?

  Serial.println(voltage);

I don't see a Serial.begin (baudrate) there.

I don't see the memory leak...

Can you add the Serial.begin(), at the very beginning of setup() and write a message Serial.println("Started !"); so you know that the sketch has started.

Why do you make pin 0 and 1 low ? Please remove those.

Can you make a better layout of the text. Use indents always in the same way. You can use the auto-format, it is in the menu.

You check if the voltage is <2.5 and also if voltage >= 2.51
What if a voltage is 2.505 ? That value drops through the if-statements. You have to rewrite that part of the sketch.

Please make the improvements and show us your new sketch.

I also encounter similar issue with below sketch. I am using an Ardunio Nano (China Cheapo!)

It is quite weird behavior in the sense If I open Serial Monitor everything OK. (No reset)

If not the LCD count goes up only upto 15, after which it starts from 1 again.

If I change he delay to 3000 it will reset itself after counting upto 5. So it is consistently reset after 15 sec.

Same results if I comment out Serial.println(n)

Any Idea whats going on?

/*
** Example Arduino sketch for SainSmart I2C LCD Screen 16x2
** based on https://bitbucket.org/celem/sainsmart-i2c-lcd/src/3adf8e0d2443/sainlcdtest.ino
** by
** Edward Comer
** LICENSE: GNU General Public License, version 3 (GPL-3.0)

** This example uses F Malpartida's NewLiquidCrystal library. Obtain from:
** https://bitbucket.org/fmalpartida/new-liquidcrystal 

** Modified - Ian Brennan ianbren at hotmail.com 23-10-2012 to support Tutorial posted to Arduino.cc

** Written for and tested with Arduino 1.0
**
** NOTE: Tested on Arduino Uno whose I2C pins are A4==SDA, A5==SCL

*/
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR    0x27 // <<----- Add your address here.  Find it from I2C Scanner
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

int n = 1;

LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

void setup()
{
lcd.begin (16,2); //  <<----- My LCD was 16x2
Serial.begin(9600);


// Switch on the backlight
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.home (); // go home

lcd.print("Bishan Cader");  
}

void loop()
{
// Backlight on/off every 3 seconds
lcd.setCursor (0,1);        // go to start of 2nd line
lcd.print(n++,DEC);
//lcd.setBacklight(LOW);      // Backlight off
delay(1000);
lcd.setBacklight(HIGH);     // Backlight on
delay(1000);
Serial.println(n);
}

Try replacing your code for this:

/*
 ** Example Arduino sketch for SainSmart I2C LCD Screen 16x2
 ** based on https://bitbucket.org/celem/sainsmart-i2c-lcd/src/3adf8e0d2443/sainlcdtest.ino
 ** by
 ** Edward Comer
 ** LICENSE: GNU General Public License, version 3 (GPL-3.0)

 ** This example uses F Malpartida's NewLiquidCrystal library. Obtain from:
 ** https://bitbucket.org/fmalpartida/new-liquidcrystal

 ** Modified - Ian Brennan ianbren at hotmail.com 23-10-2012 to support Tutorial posted to Arduino.cc

 ** Written for and tested with Arduino 1.0
 **
 ** NOTE: Tested on Arduino Uno whose I2C pins are A4==SDA, A5==SCL

 */
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR    0x27 // <<----- Add your address here.  Find it from I2C Scanner
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

#define LCD_BL_CS_DELAY 3000 // In milliseconds - LCD BackLight Change State Delay

bool lcd_bl_state = false; // false = off; true = on - LCD Backlight state.
unsigned long int current_millis = 0;
unsigned long int last_lcd_change_millis = 0;

int n = 1;


LiquidCrystal_I2C lcd (I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);

void setup ()
{
    lcd.begin (16, 2); //  <<----- My LCD was 16x2
    Serial.begin (9600);


    // Switch on the backlight
    lcd.setBacklightPin (BACKLIGHT_PIN, POSITIVE);
    lcd.setBacklight (HIGH);
    lcd.home (); // go home

    lcd.print ("Bishan Cader");
    last_lcd_change_millis = millis ();
}

void loop ()
{
    current_millis = millis ();

    if ((current_millis - last_lcd_change_millis) > LCD_BL_CS_DELAY)
    {
	lcd.setCursor (0, 1); // go to start of 2nd line
	lcd.print (n++, DEC);
	if (!lcd_bl_state)
	{
	    lcd.setBacklight (LOW); // Backlight off
	}
	else
	{
	    lcd.setBacklight (HIGH); // Backlight on
	}
	lcd_bl_state = !lcd_bl_state;
	Serial.println (n);
	last_lcd_change_millis += LCD_BL_CS_DELAY;
    }
}

And let us know what happens.

Your code works fine for me on my Uno R3 with and without the serial monitor. I don't think there is a software or memory problem with your code.

Do all your sketches reset after 15 seconds with that nano, or is it only this one? If its only this one, I don't know what it could be, but if its all of them I can think of possible interaction between the dtr and rx/tx pins. Do you know which serial interface chip is on the nano?

Thanks for your responses.

I found the issue is to do with powering the Nano with Win7 PC USB port. Some how USB port powers down if
there is no communication going on.
If I power the Nano externally no issue.
Also if serial monitor is open and Arduino sends data to Serial monitor, USB port is kept alive.