I would like some help or tell me where I can look for help.
What I am going to use is= one Led and one switch.
What I am shooting for is=
When the switch is held down for more then 10 minutes or more, after the switch is let go the led will go on (high).
But if the switch is held for 2 minutes or (less then the 10min.) and then let go, the led will be off or (low).
const int buttonPin = 11; // the number of the pushbutton pin
const int ledPin = 3; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
I think it's something like this. But I don't know how to do the time, as for how long the switch is held down.
const int buttonPin = 11; // the number of the pushbutton pin
const int ledPin = 3; // the number of the LED pin
int held;
int time;
int buttonState = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop(){
held = digitalRead(buttonPin);
time = (held + 1);
Serial.println(time);
if (time >= 100) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
In my experience, 2 is not ever greater than or equal 100. YMMV.
Look at the state change detection example, to learn how to detect when the switch becomes pressed, and when the switch becomes released. Then, as AWOL says, use millis() to record when each event happens. The difference between the event times tells you how long the switch was held down.
Once a "5 consecutive second" threshold is crossed, LED 13 goes On.
And it stays on till the Reset button is pushed or you include a separate reset option
// trippoint5sec
//
// wait for 5 seconds
// of constant detection
//
unsigned long refTime;
unsigned long trippoint;
const long threshold = 5000; // 5 sec or your choice
byte DET;
const byte pirPin = 8; // == button, etc
const byte ledPin = 13;
void setup ()
{
//pirPin is INPUT by default, use ExtPullup
pinMode (ledPin,OUTPUT);
digitalWrite (ledPin,LOW);
}
void loop ()
{
DET = digitalRead(pirPin);
if (DET == 1) // inactive
{
refTime = millis();
trippoint = refTime;
}
else // Active Low
{
pir_active();
}
}
void pir_active ()
{
trippoint = millis();
if ((trippoint-refTime) > threshold)
{
digitalWrite(ledPin,HIGH);
}
}