1
int PUMP = 13; 
int sensor = 8; 
int val; 

void setup() {
  pinMode(13,OUTPUT);
  pinMode(8,INPUT);
}

void loop() { 
  val = digitalRead(8);  
  if(val == HIGH) 
  {
    digitalWrite(13,HIGH); 
  }
  else
  {
    digitalWrite(13,LOW);
  }

  delay(0);
}
  • the digital input (pin8) has four basic states that you can be used to trigger an event (pin13) ..... the four are low, rising edge, high and falling edge ..... assume that the low and high states can be indefinitely long .... you have to decide which state triggers the 1 minute timer and whether some state re-triggers the 1 minute timer before it times out – jsotola Apr 24 '18 at 18:31
3

Just add delay after you turn on pin 13. So it would be:

int PUMP = 13;
int sensor = 8;
int val;

void setup() {
  pinMode(13, OUTPUT);
  pinMode(8, INPUT);
}

void loop() {

  val = digitalRead(8);

  if (val == HIGH)
  {
    digitalWrite(13, HIGH);
    delay(60000); //60000ms is 1 minute
  }
  else
  {
    digitalWrite(13, LOW);
  }
}
2

I want to add to the answer of Ian:

  • If you later want to do something within that minute, you should not use delay. Instead use millis, see Blink without delay and use the same principle. It is considered bad practice to use delays, since it prevents from doing anything else in the mean time. For your small example it is not a problem though.

  • Also check what you really mean with requirement 'set pin 13 high if pin 8 is high' :

    • What if pin 13 stays high for 2 minutes, should pin 8 stay high 1 minute from the start that pin 13 got high (thus will be low when pin 13 is still high)
    • Or should pin 8 stay high 1 minute after pin 13 gets to low again. In this case you need to check for flanks instead of only the current state.
1

That's a poor way to do it, the uC is then sitting there for a minute unable to do anything else. A better way is to capture the time of the input pin, then keep checking the time until 1 minute has elapsed.

void loop(){
   if ((digitalRead(8) == HIGH) && (timerRunning == 0)){
      startTime = millis(); // all time variables are type unsigned long
      digitalWrite (13, HIGH);
      timerRunning = 1;
   }
   if (timerRunning == 1){
      timeNow = millis();
      if ((timeNow - startTime)>= 60000UL){
         timerRunning = 0;
         digitalWrite (13, LOW);
      } //end time pass check
   } // end timer running check
   // now you can do other stuff while waiting for time to pass
} // end loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.