# turn on led if stand 5 sec in front of sensor

Dear forum users,

I am a beginner in programming Arduino. As the title describes I cannot manage to write a sketch were the led turns on after six seconds standing in front of an IR sensor.
I wrote a sketch with some help of a friend but cannot make progress on my own.
Can anyone please help me with a short description by every step in the sketch?

I already wrote a sketch to show the distance between the GP2Y0A21YK and an obstacle.

const int IR_PROXIMITY_SENSOR = A1; // Analog input pin that is attached to the sensor
const int ADC_REF = 5;//reference voltage of ADC is 5v.If the Vcc switch on the Seeeduino
//board switches to 3V3, the ADC_REF should be 3.3
float voltage;//the sensor voltage, you can calculate or find the distance
// to the reflective object according to the figures
//on page 4 or page 5 of the datasheet of the GP2Y0A21YK.

void setup()
{
// initialise serial communications at 9600 bps:
Serial.begin(9600);
}

void loop()
{
voltage = getVoltage();
Serial.print("distance = " );
Serial.println(voltage);
// wait 500 milliseconds before the next loop
delay(500);
}
/****************************************************************************/
/Function: Get voltage from the sensor pin that is connected with analog pin/
/*Parameter:-void */
/*Return: -float,the voltage of the analog pin */
float getVoltage()
{
int sensor_value;
int sum = 0;
for (int i = 0;i < 20;i ++) //Continuous sampling 20 times
{
sum = sum + sensor_value;
}
sensor_value = sum / 20;
float voltage;
voltage = 1/((float)sensor_value/205); //= ±75 if you want 0.80 at 80cm
sum = 0;
return voltage;
}

That’s my sketch. the float voltage is calculated by dividing it with 205. (need to test it)
It will show 0.80 if an object is at 80cm if you divide it by ±75.

I just want to have a led on for 5 seconds and then turns off again when obstacle was standing in front of the IR sensor for 6 seconds. Then the whole loop can start over.

You would really help me.

Thank you in advance

you need to establish threshold(s) that determine when a person is in front of the sensor and when not.

when you DON'T detect a person, capture a timestamp using millis(). Separate from the code , compare that timestamp to the current time (using millis()) and if that difference > 5 sec (5000 msec) take some action.

you can use a similar method to keep an LED on for a period of time

you may need to keep track of when the person has moved away (state variable) to detect when someone else in in front of the sensor.

What happens if you remove the declaration for the local variable "voltage", since you already have a variable of the same name declared globally?

gcjr:
you need to establish threshold(s) that determine when a person is in front of the sensor and when not.

when you DON’T detect a person, capture a timestamp using millis(). Separate from the code , compare that timestamp to the current time (using millis()) and if that difference > 5 sec (5000 msec) take some action.

you can use a similar method to keep an LED on for a period of time

you may need to keep track of when the person has moved away (state variable) to detect when someone else in in front of the sensor.

I wrote a short sketch with switch… case

how can I combine these? just want get output 13 (with led) HIGH for 3 sec if something was standing 6 seconds in front of the sensor and is took away.

this is my sketch:

const int IR_PROXIMITY_SENSOR = A1;
int ventiel1=13;
int sensor=A1; //distance sensor on analog pin 1
int goeshigh=0;
int state=state;

void setup() {
Serial.begin(9600);
pinMode(ventiel1, OUTPUT);
}

void loop() {

switch (state) {
case 1:
if (sensor >= 200 && sensor < 600)
delay (2000);
break;

if (sensor >= 200 && sensor < 600)
delay (2000);
break;

if (sensor >= 200 && sensor < 600)
delay (2000);
break;

if (sensor >= 600);
state = 1; // or call a function
break;

if (state= 1);
digitalWrite (ventiel1, HIGH);
delay (3000);
digitalWrite (ventiel1, LOW);
}}

can someone help with combining these sketches?

anyone?

From my (beginner’s) perspective it seems you already have a variable ‘sensor_value’ (convention discourages the use of the underscore in favour of the capital letter, so consider renaming it ‘sensorValue’).

In pseudo code I would compare the analogue value (let’s use your 80cm example distance) with a threshold, producing a new variable (which must be declared in your code) timeNow.

If (sensorValue < threshold) //note the ‘less than’ comparator, counter intuitive, but have a think of what would //happen if you used ‘greater than’
{
timeNow = millis(); //timeNow will be set to the value of millis(), an internal running clock in milliseconds,
// but will be constantly refreshed every loop until sensorValue is greater than threshold.

/* by the way, if you use this comment marker, rather than double forward-slash
you can wrap your comment over several
lines
*/

//next you must compare millis() now with timeNow (which stays static as long as object is closer than 80cm

if (millis() - timeNow > 6000); // 6000 milliseconds = 6 s
{ turn on the led; //that’ll be a digitalWrite (high) to pin 13

then all you have to do is consider the implications when the object moves away from the sensor just after the led goes on, or indeed if the object stays there much longer than 10 seconds…your design spec was to ‘illuminate the led for 3 seconds’.

I hope this helps, from one beginner to another.
GM
}

The library TimerOne helps a lot in timing using an interrupt.

Here is a sketch that could help you:

``````#include <TimerOne.h>

int counter = 0;
bool flag = 0;

void hundredMillis() {
if ( !digitalRead(10) ){
counter++;
if ( counter >= 60 ){
flag = 1;
}
}else{
counter = 0;
}
}

void setup() {
Timer1.initialize(100000); //uS
Timer1.attachInterrupt(hundredMillis);
pinMode( 10 , INPUT_PULLUP );
pinMode( 13 , OUTPUT );
}

void loop() {
if ( flag ){
digitalWrite( 13 , HIGH );
delay(3000);
digitalWrite( 13 , LOW );
flag = 0;
counter = 0;
}

}
``````

You should add the readings and calculations of the analog stuff in the loop() and the condition for the sensor goes inside the first if() in the interrupt.
Also, sometimes interrupts cause problems with communications, so dont put lots of code inside them and if you dont use Serial you dont need to initialize it.

here’s a sketch that may do what you want. I use a button (A1) to simulate your sensor but only wait 1.5 sec. releasing the button simulates a person moving away from the sensor and immediately deactivates the LED

``````#define ButPin  A1
#define LedPin  10

// ---------------------------------------------------------
#define SIX_SEC     1500

void
loop (void)
{
static unsigned long msecLst  = 0;
unsigned long msec     = millis ();

if (HIGH == digitalRead (ButPin))  {
msecLst = msec;
digitalWrite (LedPin, HIGH);    // off
}

if (msec - msecLst > SIX_SEC)  {
digitalWrite (LedPin, LOW);     // on
}
}

// ---------------------------------------------------------
void
setup (void)
{
Serial.begin (115200);
pinMode (ButPin, INPUT_PULLUP);
pinMode (LedPin, OUTPUT);
digitalWrite (LedPin, HIGH);
}
``````

Maxwinkens:
I wrote a short sketch with switch… case

there are several things wrong with the code you posted

``````int state=state;
``````

you can’t initialize a variable to itself

``````   switch (state) {
case 1:
``````

you use a switch statement to conditionally execute code based on state, but there is only one state, why not use an if

``````       if (sensor >= 200 && sensor < 600)
delay (2000);
break;

if (sensor >= 200 && sensor < 600)
``````

any code following a “break” in a switch statement will never be executed

``````       if (sensor >= 600);
``````

you have a semicolon “;” following an if without any braces ({}) which means the if will execute what precedes the “;” which means it does nothing.

``````     if (state= 1);
``````

you use an assignment operator “=” instead of a “equal comparision”, “==” in an if which means the condition is the value (1) of the assignment. Learn to put the constant, the “1” on the left side so that the compiler reports an error if you forget to use “==”

in order to get more experience with coding, why don’t you add some simple button switches and LEDs and play with code that results in various LED behavior depending on the button presses.

If you want to check that something is constant for a period of time you can usually do it very simply like this pseudo code - which is the same as the logic proposed in Reply #1. It may seem upside-down-logic because it updates the time when the person is not present.

``````if (personNotPresent) {
lastTimePersonNotPresent  = millis();
}

if (millis() - lastTimePersonNotPresent >= 6000) {
// person has been present continuously for 6 secs
// do something
}
``````

…R