DC Motor & L293D

Hi!
I just wondered if somesone could help me solve my problem

I want to program Arduino Uno in order to control a system which consists in a window that opens automatically whenever there is not enough sun and it closes automatically at night.
If there is enough sun the window will not even open.

So in other words I could resume this as the following:

  1. If the window opens in the morning ( lets say the weather is not sunny) cause the LDR the system has, must receive sun, the window must close at night.
  2. If the windows does not open, obviously it will not close.

I submit the image of the circuit I am using and the arduino's program

Diagram

Program

int valor_luz=0;
int entradamotor1 = 4;
int entradamotor2 = 5;
 
void setup() 
{  
 pinMode(entradamotor1, OUTPUT);
 pinMode(entradamotor2, OUTPUT); 
 pinMode(2, OUTPUT);  // Pin for the led
 Serial.begin(9600); 
 
 
  valor_luz=analogRead(A4);
  delay (500);
  
  if (valor_luz>410) // If the LDR receives light the led will NOT turn itself on
  digitalWrite (2,HIGH);  
  if (valor_luz<410) // If the LDR does not receive light the led will turn itself on
  digitalWrite (2,LOW);
      
  if (valor_luz>410 ) // 2V
  {  

    subir (2000);
    neutral (2000);
    bajar (1000); //This intruction just continues executing itself indefinetely. It does not respect the parameter it has for time "1000 ms"
  
  }
  
  
} 

void loop() 
{ 
}


void subir (int w) 
{ 
digitalWrite(entradamotor1,HIGH);
digitalWrite(entradamotor2,LOW);
delay(w);
}
  
void neutral (int x)
{
digitalWrite(entradamotor1,LOW);
digitalWrite(entradamotor2,LOW);
delay (x);
}

void bajar (int y) 
{ 
digitalWrite(entradamotor1,LOW);
digitalWrite(entradamotor2,HIGH);
delay(y);
}

As you can see I typed most of the code in the setup area cause I read in one topic here at the forum that if I did not wanted to use loops, I had to place the program in the setup part and leave the loop part blank.

The circuit works so that the LDR produces a voltage depending of the sun light it receives.
Then, thank to its output, the voltaje goes through an OPAM and the voltage is duplicated thanks to the OPAM configuration. Then the output voltage of the OPAM is conected to Arduino's analog input A4 and in the other part, 2 terminals are conected in order to enable the different combinations which will work for the H bridge in order to control the motor connected to it.

One of the other things that I saw if I left the program in the loop part was that whenever the program executed itself, it excuted again and again... This caused that the motor kept turning and never stop, even if the light that went trough the LDR was the corret to have more than 2V, which is the voltage that should had worked for the Arduino in order to stop the motor from working and just close the window at night.
So I wondered if placing the code just in setup space worked for me, but I did not work either...

Thanks in advance :slight_smile:

Hi,
Your problem is probably the Arduino resetting itself due to insufficient or noisey power.

What are you using for power ?

Is there any decoupling on the power between the motors and the Arduino ?

Are you putting the same voltage into the 293 through VSS and VS ?

Duane B

Hi DuaneB

What am I using for power ?
Well I am just simulating the circuit through ISIS software from Proteus.
The Arduino is powered I dont know with what haha but in the last simulations I ran with ISIS and Arduino everything worked well.
For the analogic part of the system, I mean the OPAM and the LDR I am using 12V in the simulation.

Is there any decoupling on the power between the motors and the Arduino ?
What do you mean?

Are you putting the same voltage into the 293 through VSS and VS ?
Yes I am man. I am using the same 12V for both pins.

Any idea for what the solution to this problem could be?

Thx in advance again :slight_smile: