How do i create a timed threshold?

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
Bryan7346
 
Posts: 4
Joined: Thu Jun 02, 2011 8:14 am

How do i create a timed threshold?

Post by Bryan7346 »

Hello, I am very new to this and I was just wondering if someone could let me know how I can make a timed threshold? I would like to monitor a blinking LED with a photo resistor. When the blinking LED goes out for 10 seconds I would like to turn on the LED in digital pin 13. Do i need to know how to use millis for this?

// These constants won't change:

const int analogPin = A4; // pin that the LDR is attached to
const int ledPin = 13; // LED in 13
const int threshold = 20; // below light


void setup() {
// initialize the 13 pin as an output:
pinMode(ledPin, OUTPUT);

// initialize serial communications:
Serial.begin(9600);

}

void loop() {

int analogValue = analogRead(analogPin); // read the value of the ldr:

Serial.println(analogValue, DEC);

if (analogValue < threshold) {
digitalWrite(ledPin, HIGH);
delay(2000);

}

else {
digitalWrite(ledPin,LOW);
}



delay(500);

}

User avatar
tomasreabe
 
Posts: 50
Joined: Tue Jul 14, 2009 10:14 pm

Re: How do i create a timed threshold?

Post by tomasreabe »

This can be working into what you are trying to do.
What you are going to do is keep looking for the Analog to go low. And after it is low for 10 sec turn on the other Led
So in your main Loop.

If Analog is High set last high time to current.
Then
If Current Time - Last High time is more than 10 Sec turn on Pin 13 led
Else Turn off LED


Example of debounce from Arduino

Code: Select all

/* 
 Debounce
 
 Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
 press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
 a minimum delay between toggles to debounce the circuit (i.e. to ignore
 noise).  
 
 The circuit:
 * LED attached from pin 13 to ground
 * pushbutton attached from pin 2 to +5V
 * 10K resistor attached from pin 2 to ground
 
 * Note: On most Arduino boards, there is already an LED on the board
 connected to pin 13, so you don't need any extra components for this example.
 
 
 created 21 November 2006
 by David A. Mellis
 modified 3 Jul 2009
 by Limor Fried
 
This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/Debounce
 */

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button 
  // (i.e. the input went from LOW to HIGH),  and you've waited 
  // long enough since the last press to ignore any noise:  

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  } 
  
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    buttonState = reading;
  }
  
  // set the LED using the state of the button:
  digitalWrite(ledPin, buttonState);

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;
}

Bryan7346
 
Posts: 4
Joined: Thu Jun 02, 2011 8:14 am

Re: How do i create a timed threshold?

Post by Bryan7346 »

Thank you for the quick response. I will try to combine the 2 codes today and hopefully I'll have some luck :) Thank you again.

Bryan7346
 
Posts: 4
Joined: Thu Jun 02, 2011 8:14 am

Re: How do i create a timed threshold?

Post by Bryan7346 »

Got it going !!! Thank you for pointing me in the right direction.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”