want to make output high for 1 min

const int trigger = 2; //Pin2 as trigger pin.
const int output = 3; //Pin3 as Output pin.
int Time = 500; //Preset time delay.

void setup() {
pinMode(output, OUTPUT);
pinMode(trigger, INPUT);
}

void loop() {
if (digitalRead(trigger) == LOW) {
digitalWrite(output, HIGH);
// HERE required timer so that output remains high for 1 min
digitalWrite(output, LOW);
}
else {
digitalWrite(output, LOW);
}
}

This looks suspiciously like homework?

This could be answered by looking at the most basic sketch, the Blink example in the IDE. Also, they "gave away" the right location of the timer code lines, instead of just specifying the behaviour and allowing the student to figure out where it is supposed to go.

hint: use delay() function

Here you go.

#include <timeObj.h>
#include <mechButton.h>

const int trigger = 2; //Pin2 as trigger pin.
const int output =  3; //Pin3 as Output pin.
bool  LEDOn;
mechButton  trigButton(trigger);
timeObj     LEDTimer(60.0 * 1000.0,false);  // Create a 60 sec (1 minute) timer.


void setup(void) {
   LEDOn = false;
   pinMode(output, OUTPUT);
}


void loop(void) {

   if (!trigButton.trueFalse()) {         // If the button has been pressed..
      digitalWrite(output, HIGH);         // Turn on your LED
      LEDTimer.start();                   // Start your LED timer.
      LEDOn = true;                       // Make a note of it.
      while(!trigButton.trueFalse()) {    // While the button is still being held..
         if (LEDTimer.ding()) {           // If the timer expires while button is held down.
            digitalWrite(output,LOW);     // Shut the damn thing off anyway.
            LEDOn = false;;               // Make a note of it.
         }
      }
   }
   if (LEDOn && LEDTimer.ding()) {        // If the LED is on AND timer has expired..
      digitalWrite(output,LOW);           // Shut off the LED.
      LEDOn = false;;                     // Make a not of it.
   }
}

You’ll need to install LC_baseTools from the library manager to compile this.

Good luck!

-jim lee

Hint: delay() is in milliseconds. There are 1000 milliseconds to the second. There are 60 seconds to the minute. The delay() argument is type 'unsigned long' so use 'unsigned long' constants: 60ul * 1000ul

You'll need to install LC_baseTools from the library manager to compile this.

I think most instructors would not accept this solution to a beginner exercise in learning how to use 'delay()'. It's possible the class hasn't even reached the, "library manager" part yet... :slight_smile:

aarg:
I think most instructors would not accept this solution to a beginner exerciseI

I saw a post just the other day where a member (who may or may not have been jimLee, can't recall) said they deliberately posted homework solutions way above the expected level. The idea was that the OP would hand it in, understanding just about none of it, and get caught for cheating.

I have been a teacher, there is some evidence that sometimes (not always), at certain junctures of learning, a person can best learn by just being handed the answer. But when the hand becomes constantly held out, something is wrong.

Edit - it goes with a caveat that the answer should be revealed within 5 seconds of the question being posed. The mind has some kind of "window" operating then.

aarg:
I think most instructors would not accept this solution to a beginner exercise in learning how to use 'delay()'. It's possible the class hasn't even reached the, "library manager" part yet... :slight_smile:

Yeah, maybe.. But its fun to see if you can come up with a slick solution to a problem.

-jim lee