Relay switching problem with ATmega328P + 10 relays I am trying to switch ON/OFF 10 relays connected to ATmega328P via Bluetooth HC-06 module. The problem I am facing is that when I try to Switch ON more than 3-4 relays, some of the previously ON relays switch OFF and the ATmega328 restarts. 

I am using a 12V 2A power supply.

I have set up my circuit as shown below



My code

int r1 = 4, r2 = 5, r3 = 6, r4 = 7, r5 = 8, r6 = 9, r7 = 10, r8 = 11, r9 = 12, r10 = 13;
char state;
String msg;
int r = 0;

void setup() 
{ 

    pinMode(r1, OUTPUT); 
    pinMode(r2, OUTPUT);
    pinMode(r3, OUTPUT);
    pinMode(r4, OUTPUT);
    pinMode(r5, OUTPUT);

    pinMode(r6, OUTPUT);
    pinMode(r7, OUTPUT);
    pinMode(r8, OUTPUT);
    pinMode(r9, OUTPUT);
    pinMode(r10, OUTPUT);

    Serial.begin(9600);
}

void loop() 
{

    while (Serial.available())
    {
      state = Serial.read();

      if (state == '\n')
        break;

      msg += state;

    }


    if (msg.length() &gt; 0) 
    {
      /*Serial.print("Data: ");
      Serial.println(msg);*/

      if (msg == "on1") 
      {
          digitalWrite(r1, HIGH);
          Serial.print("O1");
      }
      else if (msg == "on2") 
      {
          digitalWrite(r2, HIGH);
          Serial.print("O2");
      }
      else if (msg == "on3") 
      {
          digitalWrite(r3, HIGH);
          Serial.print("O3");
      }
      else if (msg == "on4") 
      {
          digitalWrite(r4, HIGH);
          Serial.print("O4");
      }
      else if (msg == "on5") 
      {
          digitalWrite(r5, HIGH);
          Serial.print("O5");
      }
      else if (msg == "on6") 
      {
          digitalWrite(r6, HIGH);
          Serial.print("O6");
      }
      else if (msg == "on7") 
      {
          digitalWrite(r7, HIGH);
          Serial.print("O7");
      }
      else if (msg == "on8") 
      {
          digitalWrite(r8, HIGH);
          Serial.print("O8");
      }
      else if (msg == "on9") 
      {
          digitalWrite(r9, HIGH);
          Serial.print("O9");
      }
      else if (msg == "on10") 
      {
          digitalWrite(r10, HIGH);
          Serial.print("O10");
      }

      //off---------------------------------------------------------

      else if (msg == "off1") 
      {
          digitalWrite(r1, LOW);
          Serial.print("F1");
      }
      else if (msg == "off2") 
      {
          digitalWrite(r2, LOW);
          Serial.print("F2");
      }
      else if (msg == "off3") 
      {
          digitalWrite(r3, LOW);
          Serial.print("F3");
      }
      else if (msg == "off4") 
      {
          digitalWrite(r4, LOW);
          Serial.print("F4");
      }
      else if (msg == "off") 
      {
          digitalWrite(r5, LOW);
          Serial.print("F5");
      }
      else if (msg == "off6") 
      {
          digitalWrite(r6, LOW);
          Serial.print("F6");
      }
      else if (msg == "off7") 
      {
          digitalWrite(r7, LOW);
          Serial.print("F7");
      }
      else if (msg == "off8") 
      {
          digitalWrite(r8, LOW);
          Serial.print("F8");
      }
      else if (msg == "off9") 
      {
          digitalWrite(r9, LOW);
          Serial.print("F9");
      }
      else if (msg == "off10") 
      {
          digitalWrite(r10, LOW);
          Serial.print("F10");
      }
      else if (msg == "offall") 
      {
          digitalWrite(r1, LOW);
          digitalWrite(r2, LOW);
          digitalWrite(r3, LOW);
          digitalWrite(r4, LOW);
          digitalWrite(r5, LOW);
          digitalWrite(r6, LOW);
          digitalWrite(r7, LOW);
          digitalWrite(r8, LOW);
          digitalWrite(r9, LOW);
          digitalWrite(r10, LOW);
      }

    }

    msg = "";


    delay(1000);
}
I can see lot of problems.


No decoupling capacitors on ATMega.
Capacitors on both sides voltage regulators are usualy essential for regulator stability. 
Big voltage drop on regulator - if it's overheating, thermal shutdown might happen. For example 7V drop @ 350mA (about four relays) results into 2.45 Watts turned into heat.
Bare HC06 doesn't like 5 Volts on its 3.3V logic level input
Xtal oscilator usualy requies two capacitors (22pF) for better stability.