Delay in my project is not working

Hi,

Im writting simple code for my project and I have problems with "delay" that is no happening in two situations.

I wrote:

.
.
.
  if (digitalRead(stop1)==LOW)   // working ok
  {
  digitalWrite(speed1, LOW);  // working ok
  digitalWrite(speed2, LOW);  // working ok
  digitalWrite(speed3, LOW);  // working ok

  digitalWrite(lock1, HIGH);  // working ok
  delay(1000);                   // NOT working
  digitalWrite(lock1, LOW);  // NOT working
  }
.
.
.
          if (digitalRead(start2)==HIGH)  // working ok   
          {
          digitalWrite(down2, LOW);  // working ok
          
          digitalWrite(lock2, HIGH);  // working ok
          delay(1000);                    // NOT working
          digitalWrite(lock2, LOW);  // NOT working
          }

and "lock1" and "lock2" get HIGH but it stays HIGH, even if it is "delay# for 1 sec and then it should get LOW.

What I did wrong?

Complete code:

// *ELEVATOR PROJECT* //

const int start1 = 2;        
const int start2 = 3;          
const int stop1 = 4;           
const int down1 = 5;          
const int door1 = 6;          
const int door2 = 7;          
const int down2 = 8;         
const int speed1 = 9;        
const int speed2 = 10;       
const int speed3 = 11;       
const int lock1 = 12;   
const int lock2 = 13;   


void setup()

{
pinMode(start1, INPUT);
pinMode(start2, INPUT);   
pinMode(stop1, INPUT);    
pinMode(door1, INPUT);   
pinMode(door2, INPUT);   
pinMode(down1, INPUT);   

pinMode(speed1, OUTPUT);    
pinMode(speed2, OUTPUT);    
pinMode(speed3, OUTPUT);    
pinMode(down2, OUTPUT);      
pinMode(lock1, OUTPUT);     
pinMode(lock2, OUTPUT);     
}

  
void loop()
{

  //  LIFTING  //
  if (digitalRead(start1)==HIGH)  // Push button to start Lifting
  if (digitalRead(start2)==HIGH)
  if (digitalRead(stop1)==HIGH)
  if (digitalRead(door1)==HIGH)
  if (digitalRead(door2)==HIGH)
  if (digitalRead(down1)==LOW)
  {
  digitalWrite(speed1, HIGH);  
  delay(1000);
  digitalWrite(speed1, LOW);    

  digitalWrite(speed2, HIGH);   
  delay(1000);
  digitalWrite(speed2, LOW);    

  digitalWrite(speed3, HIGH);   
  }
  
  if (digitalRead(stop1)==LOW)   
  {
  digitalWrite(speed1, LOW);
  digitalWrite(speed2, LOW);
  digitalWrite(speed3, LOW);

  digitalWrite(lock1, HIGH);
  delay(1000);
  digitalWrite(lock1, LOW);
  }  
    
          //  LOWERING  //
          if (digitalRead(down1)==HIGH)  // Push button to start Lowering
          if (digitalRead(start1)==LOW)
          if (digitalRead(start2)==LOW)
          if (digitalRead(stop1)==LOW)   
          if (digitalRead(door1)==HIGH)
          if (digitalRead(door2)==HIGH)
          {
          digitalWrite(down2, HIGH);    
          }

          if (digitalRead(start2)==HIGH)  
          {
          digitalWrite(down2, LOW);
          
          digitalWrite(lock2, HIGH);
          delay(1000);
          digitalWrite(lock2, LOW);  
          }
              
}

I have adjusted the pin numbers and tried your code on my system and it works as expected in respect of lock1 going HIGH then LOW.

How are your inputs wired ?

UKHeliBob thanke you for your quick answer.
Im using Arduino uno and for inputs im using switches from 5V to input and input is connected to gnd with 10k resistor. If I test inputs they are working ok.

Did you tested complete code?

Did you tested complete code?

I ran your complete code with 2 pin numbers adjusted for my test system but I did not fully test your code as frankly I could not be bothered to create the wiring for it.

A suggestion. Try putting some Serial.print()s in your code so that you know exactly which code is being executed and the value of relevant variables at important points in the code such as where values are tested or compared.

Yes I tryed to add Serial.print() but didnt know how :confused:

In setup() add

Serial.begin(9600);

Then where you need to print a value do something like this

  if (digitalRead(stop1) == LOW)
  {
    Serial.println("stop1 is LOW";
    Serial.println("Setting 3 speeds LOW");
    digitalWrite(speed1, LOW);
    digitalWrite(speed2, LOW);
    digitalWrite(speed3, LOW);
    Serial.println("Setting lock1 HIGH");
    digitalWrite(lock1, HIGH);
    delay(1000);
    Serial.println("Setting lock1 LOW");
    digitalWrite(lock1, LOW);
  }

Thanke you I Will try that and I Will see what is happening.

It is often illuminating to print the value of variables before testing them and taking action based on their values.

I add Serial.print() like you wrote and now is working but is blinking it should only get HIGH and after 1sec LOW with no repeate.
If I delete Serial.print then is not working.

OK, thanks for letting us know.

kikitron:
I add Serial.print() like you wrote and now is working but is blinking it should only get HIGH and after 1sec LOW with no repeate.
If I delete Serial.print then is not working.

What you are describing does not make sense if the switches and LEDs are wired correctly. What happens if you increase the delay to 10 seconds with the debug prints commented out ?

I add 10sec and i get this:

Stop1 is LOW
Setting all 3 speeds are LOW
Setting lock1 is HIGH
Stop1 is LOW
Setting all 3 speeds are LOW
Setting lock1 is HIGH
Setting lock1 is LOW
Stop1 is LOW
Setting all 3 speeds are LOW
Setting lock1 is HIGH
Setting lock1 is LOW
Stop1 is LOW
Setting all 3 speeds are LOW
Setting lock1 is HIGH
Setting lock1 is LOW
Stop1 is LOW
Setting all 3 speeds are LOW
Setting lock1 is HIGH
Setting lock1 is LOW
Stop1 is LOW
Setting all 3 speeds are LOW
Setting lock1 is HIGH

It is switching from HIGH to LOW in some miliseconds.

I think that the time has come for you to post a circuit diagram even if it is only hand drawn and photographed. Don't be tempted to post a photograph of the actual circuit as it is rarely possible to interpret them.

Yes I will do that and post it.

I draw it by hand so sorry on quality :grinning: Insted of leds I will use relays at the end.

Here is the link for full size: schematic

I can't see anything wrong but that does not mean that there is not a problem.

As a test leave the wiring as it is and try the Blink sketch from the IDE examples to blink the lock1 LED. Does it flash at the correct rate ? If so then modify it to only flash when the stop1 input is LOW as in your program. Does it work as expected ?

I tryed with Blink sketch and its not working if I disable last "delay(1000), led is constantly HIGH it doesnt go LOW.
But if I enable last delay then is blinking like it should. I only need HIGH and after delay LOW only ones.

const int lock1 = 12;

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 12 as an output.
  pinMode(lock1, OUTPUT);

  Serial.begin(9600);
}

// the loop function runs over and over again forever
void loop() {
  Serial.println("LED HIGH");
  digitalWrite(lock1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  Serial.println("LED LOW");
  digitalWrite(lock1, LOW);    // turn the LED off by making the voltage LOW
 // delay(1000);              // DISABLE
}

Serila.print:

LED HIGH
LED LOW
LED HIGH
LED LOW
LED HIGH
LED LOW
LED HIGH
.
.
.

Here is video for Serial.print: VIDEO

if I disable last "delay(1000), led is constantly HIGH it doesnt go LOW

Acyually it does go LOW but because there is no delay() it immediately goes HIGH again at the beginning of the loop() function. The point of the test program was to ensure that the hardware was working, which it seems to be. How about the second test that I suggested ?

If I add stop1 input in code and set it to LOW, blink is working ok.

Code:

const int lock1 = 12;
const int stop1 = 4;

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  pinMode(lock1, OUTPUT);

  pinMode(stop1, INPUT);

  Serial.begin(9600);
}

// the loop function runs over and over again forever
void loop() {
  
if (digitalRead(stop1)==LOW)   
  {
  Serial.println("Stop1 is LOW");
  Serial.println("Setting lock1 is HIGH");
  digitalWrite(lock1, HIGH);
  delay(1000);
  Serial.println("Setting lock1 is LOW");
  digitalWrite(lock1, LOW);
  delay(1000);
  }  
}

Sorry, but I am out of ideas. I will try to give it some more thought, but no promises.