user55545: I am frustrated with my arduino project can you help me?
I am using Arduino Uno, electric lock (high/low), 4 distance sonic modules. Basically I have 4 drawers, I am measuring the distance - how much are they opened and if they are opened in given limits, I set the electric lock open (high).
It is working, but after some while, it stops and it stops running. 
I have tried to comment all serial communication - didn't help. I have tried to use proper variable data types - same. I have tried to buy much powerful power source - 12V 3,5A and I bought Electrolyte condenzator 4,7mF to balance the power peaks while the lock closes - didn't help.
The lock is turned on using relay, connecting it directly to power source 12V.
Relay is powered from 5V arduino supply and controlled by arduino Digital pin.
4 Sonic distance modules are powered from arduino power supply.
Its even more funny that it starts running after ~10 minutes from nothing. Im confused.
Code:
    // defines pins numbers
const int trigPin1 = 2;
const int echoPin1 = 3;
const int trigPin2 = 6;
const int echoPin2 = 5;
const int trigPin3 = 9;
const int echoPin3 = 8;
const int trigPin4 = 12;
const int echoPin4 = 11;
const int relayPin = 7;
//distance limits
const int minSuplik4=27;//suplik4
const int maxSuplik4=29;
const int minSuplik3=22;//suplik3
const int maxSuplik3=24;
const int minSuplik2=32;//suplik2
const int maxSuplik2=34;
const int minSuplik1=9;//suplik1
const int maxSuplik1=11;
/*
double PreDistance1[4]={0,0,0,0};//supliky
double PreDistance2[4]={0,0,0,0};//supliky
double PreDistance3[4]={0,0,0,0};//supliky
*/
long distance[4]={0,0,0,0};//array with distances
int i;
int x;

int trigPinX[4]={trigPin1,trigPin2,trigPin3,trigPin4};//supliky
int echoPinX[4]={echoPin1,echoPin2,echoPin3,echoPin4};//supliky
int maxD[4]={maxSuplik1,maxSuplik2,maxSuplik3,maxSuplik4};//supliky
int minD[4]={minSuplik1,minSuplik2,minSuplik3,minSuplik4};//supliky
void setup() {
pinMode(relayPin, OUTPUT);
pinMode(trigPin1, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin1, INPUT); // Sets the echoPin as an Input
pinMode(trigPin2, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin2, INPUT); // Sets the echoPin as an Input
pinMode(trigPin3, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin3, INPUT); // Sets the echoPin as an Input
pinMode(trigPin4, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin4, INPUT); // Sets the echoPin as an Input
digitalWrite(relayPin, LOW);
Serial.begin(9600); // Starts the serial communication
}
void loop() {

for (int i=0; i<4; i++)
{
  delay(5);
  distance[i]=DistanceFunction(trigPinX[i],echoPinX[i]);
  delay(20);
  /*********** DEBUG ************
  Serial.print("Distance");// Prints the distance on the Serial Monitor
  Serial.print(i+1);
  Serial.print(":");
  Serial.print(distance[i]);
  Serial.print("<");
  Serial.print(minD[i]);
  Serial.print(":");
  Serial.print(maxD[i]);
  Serial.print(">");
  Serial.print(";");
  *********************************/
}

if(distance[0]>=minD[0] && distance[0]<=maxD[0] && distance[1]>=minD[1] && distance[1]<=maxD[1] && distance[2]>=minD[2] && distance[2]<=maxD[2] && distance[3]>=minD[3] && distance[3]<=maxD[3])
      {
        OpenFcn();
      }
else 
      {
        CloseFcn();
      }
delay(100);
}

int DistanceFunction(int trigPin, int echoPin)
{
  digitalWrite(trigPin, LOW);// Clears the trigPin
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);// Sets the trigPin on HIGH state for 10 micro seconds
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  long duration = pulseIn(echoPin, HIGH);// Reads the echoPin, returns the sound wave travel time in microseconds
  long distanceFcn= duration*0.034/2;// Calculating the distance
  return distanceFcn;
}

int OpenFcn()
{
  digitalWrite(relayPin, HIGH);
  //Serial.println("OPENED");
  for (int x=5; x>0; x--)
    {
      //Serial.println("Otevreno:");
      //Serial.println(x);
      delay(1000);
    }
}

int CloseFcn()
{
  digitalWrite(relayPin, LOW);
  //Serial.println("CLOSED");
}



user55545: It looks like that there was a problem in the relay module or power supply cord. I have changed these two HW elements and looks like it is working. The reason why it did malfuctioning the arduino was, that when i moved the drawer, it slightly moved the power cord which had bad connection, so it made voltage drop on arduino supply and started to do crazy things.
Thanks all for your time

55545: <p>I am frustrated with my arduino project can you help me?
I am using Arduino Uno, electric lock (high/low), 4 distance sonic modules. Basically I have 4 drawers, I am measuring the distance - how much are they opened and if they are opened in given limits, I set the electric lock open (high).</p>

<p>It is working, but after some while, it stops and it stops running. 
I have tried to comment all serial communication - didn't help. I have tried to use proper variable data types - same. I have tried to buy much powerful power source - 12V 3,5A and I bought Electrolyte condenzator 4,7mF to balance the power peaks while the lock closes - didn't help.</p>

<p>The lock is turned on using relay, connecting it directly to power source 12V.
Relay is powered from 5V arduino supply and controlled by arduino Digital pin.
4 Sonic distance modules are powered from arduino power supply.</p>

<p>Its even more funny that it starts running after ~10 minutes from nothing. Im confused.</p>

<p>Code:</p>

<pre><code>    // defines pins numbers
const int trigPin1 = 2;
const int echoPin1 = 3;
const int trigPin2 = 6;
const int echoPin2 = 5;
const int trigPin3 = 9;
const int echoPin3 = 8;
const int trigPin4 = 12;
const int echoPin4 = 11;
const int relayPin = 7;
//distance limits
const int minSuplik4=27;//suplik4
const int maxSuplik4=29;
const int minSuplik3=22;//suplik3
const int maxSuplik3=24;
const int minSuplik2=32;//suplik2
const int maxSuplik2=34;
const int minSuplik1=9;//suplik1
const int maxSuplik1=11;
/*
double PreDistance1[4]={0,0,0,0};//supliky
double PreDistance2[4]={0,0,0,0};//supliky
double PreDistance3[4]={0,0,0,0};//supliky
*/
long distance[4]={0,0,0,0};//array with distances
int i;
int x;

int trigPinX[4]={trigPin1,trigPin2,trigPin3,trigPin4};//supliky
int echoPinX[4]={echoPin1,echoPin2,echoPin3,echoPin4};//supliky
int maxD[4]={maxSuplik1,maxSuplik2,maxSuplik3,maxSuplik4};//supliky
int minD[4]={minSuplik1,minSuplik2,minSuplik3,minSuplik4};//supliky
void setup() {
pinMode(relayPin, OUTPUT);
pinMode(trigPin1, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin1, INPUT); // Sets the echoPin as an Input
pinMode(trigPin2, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin2, INPUT); // Sets the echoPin as an Input
pinMode(trigPin3, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin3, INPUT); // Sets the echoPin as an Input
pinMode(trigPin4, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin4, INPUT); // Sets the echoPin as an Input
digitalWrite(relayPin, LOW);
Serial.begin(9600); // Starts the serial communication
}
void loop() {

for (int i=0; i&lt;4; i++)
{
  delay(5);
  distance[i]=DistanceFunction(trigPinX[i],echoPinX[i]);
  delay(20);
  /*********** DEBUG ************
  Serial.print("Distance");// Prints the distance on the Serial Monitor
  Serial.print(i+1);
  Serial.print(":");
  Serial.print(distance[i]);
  Serial.print("&lt;");
  Serial.print(minD[i]);
  Serial.print(":");
  Serial.print(maxD[i]);
  Serial.print("&gt;");
  Serial.print(";");
  *********************************/
}

if(distance[0]&gt;=minD[0] &amp;&amp; distance[0]&lt;=maxD[0] &amp;&amp; distance[1]&gt;=minD[1] &amp;&amp; distance[1]&lt;=maxD[1] &amp;&amp; distance[2]&gt;=minD[2] &amp;&amp; distance[2]&lt;=maxD[2] &amp;&amp; distance[3]&gt;=minD[3] &amp;&amp; distance[3]&lt;=maxD[3])
      {
        OpenFcn();
      }
else 
      {
        CloseFcn();
      }
delay(100);
}

int DistanceFunction(int trigPin, int echoPin)
{
  digitalWrite(trigPin, LOW);// Clears the trigPin
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);// Sets the trigPin on HIGH state for 10 micro seconds
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  long duration = pulseIn(echoPin, HIGH);// Reads the echoPin, returns the sound wave travel time in microseconds
  long distanceFcn= duration*0.034/2;// Calculating the distance
  return distanceFcn;
}

int OpenFcn()
{
  digitalWrite(relayPin, HIGH);
  //Serial.println("OPENED");
  for (int x=5; x&gt;0; x--)
    {
      //Serial.println("Otevreno:");
      //Serial.println(x);
      delay(1000);
    }
}

int CloseFcn()
{
  digitalWrite(relayPin, LOW);
  //Serial.println("CLOSED");
}
</code></pre>

<p><a href="https://i.stack.imgur.com/eBJgp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eBJgp.png" alt="This is the moment when it start working crazy"></a></p>
 55545: <p>It looks like that there was a problem in the relay module or power supply cord. I have changed these two HW elements and looks like it is working. The reason why it did malfuctioning the arduino was, that when i moved the drawer, it slightly moved the power cord which had bad connection, so it made voltage drop on arduino supply and started to do crazy things.
Thanks all for your time</p>
