Problem with the code when I connect the reles.

Hi, I just compiled my code and everything was working fine but when I connected the power source to the reles (12V), the code is stuck on the void setup and never goes to the void loop, when the power source isn't conected, the reles and everything works fine... any ideas?

thank you very much for your help in advance!

CODE JUST IN CASE IT HELPS:

#include <AFMotor.h>

AF_DCMotor motorA(1);

const int solenoide = 51;
const int led_inicial = 49;

float metal_SI;
int reading_SI;
int metalPin_SI = 8;

const int BT0 = 31;
int reading_BT0;
const int BT1 = 33;
int reading_BT1;
const int BT2 = 35;
int reading_BT2;
const int BT3 = 37;
int reading_BT3;

int posicio_emmagatzemable =0;

void setup()
{
//Motor speed
motorA.setSpeed(250);

//Led inicial
pinMode(led_inicial, OUTPUT);
digitalWrite(led_inicial, HIGH);
delay(500);
digitalWrite(led_inicial, LOW);
delay(500);

//Declarem solenoide com un output
pinMode(solenoide, OUTPUT);
//High solenoide = apagada
Serial.begin(9600);
delay(1000);

//Declarem butons com a INPUTS
pinMode(BT0, INPUT);
pinMode(BT1, INPUT);
pinMode(BT2, INPUT);
pinMode(BT3, INPUT);
Serial.print("Ojo que estem dins del inicialitzador ");
Serial.print("\n");
Serial.print("\n");
}

void loop()
{
int selected_button = 0;
int NO_button =0;

int i=0;
int comptador=0;
motorA.run(RELEASE);

reading_BT0 = digitalRead(BT0);
reading_BT1 = digitalRead(BT1);
reading_BT2 = digitalRead(BT2);
reading_BT3 = digitalRead(BT3);

if (reading_BT0 == LOW or reading_BT1 == LOW or reading_BT2 == LOW or reading_BT3 == LOW) {NO_button = 1;}

if (reading_BT0 == LOW) {selected_button = 0;}
if (reading_BT1 == LOW) {selected_button = 1;}
if (reading_BT2 == LOW) {selected_button = 2;}
if (reading_BT3 == LOW) {selected_button = 3;}

if (NO_button == 1){comptador = selected_button - posicio_emmagatzemable;
if(comptador == -1){comptador = 3;}
if(comptador == -2){comptador = 2;}
if(comptador == -3){comptador = 1;}
Serial.print(" Estava a la posicio ");
Serial.print(posicio_emmagatzemable);
Serial.print("\n");
Serial.print(" Volem anar a la posicio = : ");
Serial.print(selected_button);
Serial.print("\n");
Serial.print(" Haurem de moure = : ");
Serial.print(comptador);
Serial.print("\n");
posicio_emmagatzemable = selected_button;
}

//Serial.print(" ");
//Serial.print("Cops detectat= : ");
//Serial.print(i);

if (i != comptador){
digitalWrite(solenoide, LOW);
delay(1000);
motorA.run(FORWARD);
delay(1500);}

while (i != comptador) {
delay(750);
reading_SI = analogRead(metalPin_SI);
metal_SI = (float)reading_SI*100/1024.0;

if(reading_SI<100){
i++;
digitalWrite(led_inicial, HIGH);
delay(100);
digitalWrite(led_inicial, LOW);
//Serial.print("Ojo que estem dins del comptador ");
}
}
//Serial.print("RES ");
motorA.run(RELEASE);
delay(1000);
digitalWrite(solenoide, HIGH);

}

What is your wiring?
What do you see in the serial output?

It is not a good idea to use the same power supply for an Arduino and for a solenoid and for relays and for motors. There should be more than one power supply, and the grounds should be tied together.

It is likely that the Arduino is starting, and that when it activates a motor, a solenoid, or a relay, the Arduino loses power momentarily, resets, and starts again from the beginning. It will appear stuck but is not.

setup() is a function that returns void. It is not a void setup.

To post code and/or error messages:

  1. Use CTRL-T in the Arduino IDE to autoformat your complete code.
  2. Paste the complete autoformatted code between code tags (the </> button)
    so that we can easily see and deal with your code.
  3. Paste the complete error message between code tags (the </> button)
    so that we can easily see and deal with your messages.
  4. If you already posted without code tags, you may add the code tags by
    editing your post. Do not change your existing posts in any other way.
    You may make additional posts as needed.

Before posting again, you should read the three locked topics at the top of the Programming Questions forum, and any links to which these posts point.

If your project involves wiring, please provide a schematic and/or a wiring diagram and/or a clear photograph of the wiring.

Good Luck!