delay in if statement not working

I have 4 leds in my project. Any one should turn on when given input, also should turn off immediately. I havve used delay in if to turn it off. But its not. Help me if code is going somewhere wrong
Thank you

sketch_may24a.ino (1.23 KB)

Attach your code using the </> icon on the left side of the posting menu.
Put your sketch between the code tags ``

Why do you need delay(), use BWD, see the blink without delay example in the IDE.

How are your switches wired?
You may need a PULLUP resistor or use INPUT_PULLUP .

.

const int upip=7;
const int downip=8;
const int leftip=11;
const int rightip=12;

const int upled=5;
const int downled=6;
const int leftled=9;
const int rightled=10;

int a1=0;
int a2=0;
int a3=0;
int a4=0; 

void setup() {
  // put your setup code here, to run once:
pinMode(7,INPUT);
pinMode(8,INPUT);
pinMode(11,INPUT);
pinMode(12,INPUT);

pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);

Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
a1=digitalRead(upip);
a2=digitalRead(downip);
a3=digitalRead(leftip);
a4=digitalRead(rightip);

if (a1 == HIGH) {
  digitalWrite(upled,HIGH);
  delay(2000);
  digitalWrite(upled,LOW);
  
}

  
 else {
    digitalWrite(upled,LOW);    // turn LED off
  }

if (a2 == HIGH){
  digitalWrite(downled,HIGH);
  delay(2000);
  digitalWrite(downled,LOW);
}

else {
    digitalWrite(downled,LOW);    // turn LED off
  }

if (a3 == HIGH) {
  digitalWrite(leftled,HIGH);
  delay(2000);
  digitalWrite(leftled,LOW);
}


else {
    digitalWrite(leftled,LOW);    // turn LED off
  }

if (a4 == HIGH) {
  digitalWrite(rightled,HIGH);
  delay(2000);
  digitalWrite(rightled,LOW);
  }
  
  
else {
    digitalWrite(rightled,LOW);    // turn LED off
  };
}

Thank you!

i am new to Arduino. Actually my project is basically to vibrate pager motors. I am trying my code with LEDs. I am not using switches. Its a project for deaf and dumb. When my mic will receive analog input, the motor who is getting this input should vibrate. But only for a sec or 2.
Let me also tell you that I have not connected the mics yet. Just checking if the motors are vibrating whenever they get input. So I am connecting inputs manually for now. When I give input the motor vibrates continuously.
Hope u get my problem.
Also tell me if my approach is wrong
Thank you!

Go back to LarryD’s reply and re-read the second paragraph. Using the BWOD (blink without delay) technique essential in most Arduino programs. Your code has many ‘delay(200);’ statements. What happens if a sound is detected during one of these delays? (hint: absolutely nothing)

There are threads here that discuss the logic of an Arduino program. Learn how to write a useful Arduino program before starting off on a complex topic such as this one. Otherwise you will find yourself starting over fresh after a while, and possibly more than once.

The demo Several Things at a Time is an extended example of BWoD and illustrates the use of millis() to manage timing.

...R

1 Like

Here is a start point:

//Not tested
//global variable area before setup()
byte a1Flag = false;
unsigned long a1Millis;


//somewhere in loop() 

//***********************************
a1 = digitalRead(upip);

if (a1Flag == false && a1 == HIGH) 
{
  digitalWrite(upled,HIGH); //led on
  a1Flag = true;  //allow timing
  a1Millis = millis(); //set to the time right now
}

//time to turn off the led?
if(a1Flag == true && millis() - a1Millis >= 2000)
{
  digitalWrite(upled,LOW); //led off
  
  a1Flag = false; //get ready for the next HIGH on a1
}

//***********************************  repeat these lines with a2, a3 and a4 stuff

.

Thank you all!
Now that i m working with switches.
Connected pull down resistor
I have used rocker on/off switch
when i make it off still the motor is not switching off
only when i make any other switch on the previous motor is turning off :frowning:
attaching rough sketch
now that for final sketch i m working on everything you all provided on delay
but for the time being i m not using delay
just trying to at least switch off motor when switch is off

Thank you

Testing your code sir ! Thank u so much @LarryD sir.