Go Down

Topic: Not switching off fast (Read 146 times) previous topic - next topic

comarad

Hi

I have been doing some simple programming the last two days and i got stock with something.

I have made a simple program to explain my problem and so you guys can see if i am missing something,

The problem is why doesn't the output pin switch directly off when the input that activated it has gone from HIGH to LOW, my intent is to make an ON DELAY but it acts as an ON&OFF DELAY instead,
_______________________________________________________________________________
int MAIN = 0;


//   I/O Configuration Setup
void setup() {
  pinMode(2, OUTPUT);
  pinMode(3, INPUT);

}

void loop(){
  //_____________________________________
 
MAIN = digitalRead(3);

if (MAIN==HIGH){
   delay(1500);
   digitalWrite (2,HIGH);
}

else if (MAIN==LOW){
   digitalWrite (2,LOW);

}

}



Thanks in Advance  :)

Bd611

#1
Oct 13, 2014, 03:15 am Last Edit: Oct 13, 2014, 03:18 am by Bd611 Reason: 1
Hi,
the delay in the shut off is due to every time the arduino runs through the loop it's delaying 1500 milliseconds then writing pin 2 high so when you switch the switch off the arduino might still be in the middle of the 1500 millisecond delay. to get rid of this  another condition should be added to the if statement this way the arduino only sets pin 2 high if it is low and avoids the 1500 millisecond delay.

Code: [Select]
int MAIN = 0;


//   I/O Configuration Setup
void setup() {
 pinMode(2, OUTPUT);
 pinMode(3, INPUT);

}

void loop(){
 //_____________________________________
 
MAIN = digitalRead(3);

if (MAIN==HIGH && digitalRead(2)==LOW){
  delay(1500);
  digitalWrite (2,HIGH);
}

else if (MAIN==LOW){
  digitalWrite (2,LOW);

}

}

PaulS

Some questions. Why is MAIN a variable? By convention, all capital letter names are constants.

Why is MAIN global? Only one function cares about it. It should be local to that function.

What other values, besides HIGH and LOW can MAIN possibly take on? If it isn't HIGH, is there a hope in hell that it will be anything other than LOW? If not (and there isn't), then there is no need to test that it is LOW if it isn't HIGH.

Robin2


The problem is why doesn't the output pin switch directly off when the input that activated it has gone from HIGH to LOW, my intent is to make an ON DELAY but it acts as an ON&OFF DELAY instead,


The delay() function takes up all of the Arduino's time until it is finished so that any button pressed is not detected until the delay period is finished.

The proper way to get an immediate response from your button is to stop using delay() and use millis() to do the timing as in the Blink Without Delay example sketch and as shown in the demo several things at a time.  Try that demo and see how it works.

...R

BulldogLowell

You can do it this way using BlinkWithoutDelay approach, where there is an example included in the IDE.

Code: [Select]
//int MAIN = 0;  // you don't need this.
unsigned long timerStartTime;

void setup()
{
  pinMode(2, OUTPUT);
  pinMode(3, INPUT);
}

void loop()
{
  if (digitalRead(3) == HIGH)
  {
    timerStartTime = millis();
  }
  ledFunction();
}

void ledFunction()
{
  if (millis() - timerStartTime <= 1500UL)
  {
    digitalWrite(2, HIGH);
  }
  else
  {
    digitalWrite(2, LOW);
  }
}

Go Up