Want to make a simple program using timer. if input is IR Sensor is High for 3 Sec output light will be off
permission granted.
(do you have a question?)
Cool.
Welcome to the community.
thanks kindly help me out in this simple program
Sure. Post your code that you have wrote, in code tags, describe what the program is supposed to do and describe what the program does.
#define relayPin4 5
#define IRPin 6
#define delay1 300
int state;
static unsigned long timestamp = 0;
unsigned long timeNow = millis();
void setup() {
// put your setup code here, to run once:
pinMode(relayPin4, OUTPUT);
pinMode(IRPin, INPUT);
Serial.begin(9600);
digitalWrite(IRPin,HIGH);
}
void loop() {
// put your main code here, to run repeatedly:
state=digitalRead(IRPin);
Serial.println(state);
if (timeNow - timestamp > 3000)
{
digitalWrite(relayPin4,LOW);
}
else{
digitalWrite(relayPin4,HIGH);
}
}
i need to make a program using IR sensor in which the condition is if IR sensor senses something in front of it for 3 seconds the output light will turn off.
If you would use code tags (</>), your sketch of post #6 would appear like below:
#define relayPin4 5
#define IRPin 6
#define delay1 300
int state;
static unsigned long timestamp = 0;
unsigned long timeNow = millis();
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(relayPin4, OUTPUT);
pinMode(IRPin, INPUT);
digitalWrite(IRPin, HIGH);
}
void loop()
{
// put your main code here, to run repeatedly:
state = digitalRead(IRPin);
Serial.println(state);
if (timeNow - timestamp > 3000)
{
digitalWrite(relayPin4, LOW);
}
else
{
digitalWrite(relayPin4, HIGH);
}
}
Is your sketch working or not?
its good here but not working as i needed
The variable timeNow started with most probably 0; do you think that the variable should be changing as the time passes? If so, how?
really i left programming a long ago now i am revising it. i think there is a big flaw in this program kindly read my problem statement then give me solutions accordingly
kindly use a bit your brain, even if it's been a long time. We are trying to help you get back in shape ![]()
you have this test in the loop()
if (timeNow - timestamp > 3000)
the question is who is updating timeNow or timestamp ?
dear i told you i quit programming so dont go for my program just see my problem statement and give solution on your own. i know there is a problem in my program but i simply need solution
if you don't want to use your own brain cells, why would I use mine for YOUR problem...
if you don't want to learn, this seems a great ask for the Jobs and Paid Consultancy forum, make sure you mention your budget.
This forum is for helping people, not doing the coding on their behalf.
May be you'll find someone more helpful than me... good luck
Do you think that a brief note on the working principle of millis() function will help you to answer the question raised in post #12 by @J-M-L?
➜ Using millis() for timing. A beginners guide
but I don't think @mughalusman21 wants to do any work. He thinks he is entitled to a full code written on his behalf...
i just got stuck on 3 sec timer for IR sensor that if it detects any object for 3 sec it will turn off the output. but in my program that section which were raised by JML is really a mistake. all is that i was trying to make a program like a newbie
i know at that point i was unable to understand what to do thats why i am asking you guys for best possible solution
This statement:
timeNow = millis();
needs to go in your loop, combined with an if-statement that reads the IR sensor input pin.
Go ahead and give it a try, report back if you have problems with it.
As you can tell, people are often not willing to write the code for you. There's no fun in it and you don't learn much from it.
yes it works thanks alot for your effective response
simple program to do the thing, not tested.
#include "sdkconfig.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "freertos/event_groups.h"
#define evtIR ( 1 << 0 ) // declare an event
EventGroupHandle_t eg; // variable for the event group handle
#define relayPin4 5
#define IRPin 6
void IRAM_ATTR oneshot_timer_callback( void* arg )
{
BaseType_t xHigherPriorityTaskWoken;
xEventGroupSetBitsFromISR( eg, evtIR, &xHigherPriorityTaskWoken );
} //void IRAM_ATTR oneshot_timer_callback( void* arg )
void setup()
{
pinMode(relayPin4, OUTPUT);
pinMode(IRPin, INPUT);
digitalWrite(IRPin, HIGH);
eg = xEventGroupCreate();
esp_timer_create_args_t oneshot_timer_args = {}; // initialize High Resoulition Timer (HRT) configuration structure
oneshot_timer_args.callback = &oneshot_timer_callback; // configure for callback, name of callback function
esp_timer_create( &oneshot_timer_args, &oneshot_timer ); // assign configuration to the HRT, receive timer handle
xTaskCreatePinnedToCore( fTaskTheThing, "fTaskTheThing", 7000, NULL, 5, NULL, 1 );
}
//
void fTaskTheThing(void *pvParaeters )
{
for (;;)
{
if ( digitalRead(IRPin) == HIGH )
{
//start timer
esp_timer_start_once( oneshot_timer, 3000000 ); // trigger one shot timer for a 3000000us timeout
xEventGroupWaitBits (eg, evtIR, pdTRUE, pdTRUE, portMAX_DELAY ); // event will be triggered by the timer expiring, wait here for the 280uS
if ( digitalRead(IRPin) == HIGH )
{
digitalWrite(relayPin4, LOW);
}
else
{
digitalWrite(relayPin4, HIGH);
}
}
}
vTaskDelete( NULL );
}
void loop()
{
}
Good luck.
ETA added the timer callback.