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");
}