Problem with timers

I have a thesis to install a LED lighting system in a stair case with the use of laser trip wires as sensors. the system is automatically activated when it is dark.it has dim mode for energy conservation.
I am a newbie when it comes to arduino. I am almost done with the program except the timers.I got problems with the timers.

what i want to accomplish that i can't do is this scenario :
the sensor(laser tripwire) is triggered then the timer starts counting until it reach its maximum. i want to reset the timer when the sensor is triggered again even if timer haven't reach is max counting from the previous trigger.

can any one give me examples...or anything
i really don't know what to do. pls do help me :frowning:

thank you :slight_smile:

open the example blink without delay.

put that after an if statement about the switch.

if that does not make sense, open debounce and see how that handles resetting the timer if the switch is not the same state as it was the last time through the program.

thesis? Towards a degree?

CrossRoads:
thesis? Towards a degree?

A few steps up from a diploma ?

...R

Carllamorie:
i want to reset the timer when the sensor is triggered again even if timer haven't reach is max counting from the previous trigger.

As @dave-in-nj has said, look at the BWoD example.

Every time your tripwire is triggered update previousMillis = currentMillis; That way the timer gets reset.

...R

hello! thank you so much for your suggestions!!! :slight_smile: ...but i still cant understand how can i use millis as 22 seconds timer.so please do help me

so far this is the program i have but the timer doesnt seem to work

‪#‎include‬ <TimerOne.h>
//Reads and analog sensor and displays the value
int sensePin = A0;
int relayA = 6;
int senseUp = A1;
int senseDown = A2; //1ST-2ND FLOOR IN
int relayB = 9; //1ST-2ND FLOOR OUT
int relayC = 12;

void setup(){
//Allows us to listen to serial communications from the arduino
Serial.begin(9600);
pinMode(relayA,OUTPUT);
pinMode(sensePin,INPUT);
Timer1.initialize(2200000);
Timer1.stop();
//Timer1.restart();
}

void loop(){

int val = analogRead(sensePin);
val = constrain(val, 276, 960);
int ledLevel = map(val, 276, 960, 0, 255);

if(ledLevel < 14 ){
Serial.println("off");
digitalWrite(relayA,LOW);}
else{
//Serial.println("on");
digitalWrite(relayA,HIGH);

int valDown = analogRead(senseDown);
Serial.println(analogRead(senseDown));
valDown = constrain(valDown, 790, 970);
int ledLevelDown = map(valDown, 790, 970, 0, 255);
//int valUp = (analogRead(senseUp));
if(ledLevelDown > 12){
//digitalWrite(relayB,HIGH);
Timer1.restart();
Timer1.start();
Timer1.attachInterrupt(OnDown);}
else{
digitalWrite(relayB,LOW);}
int valUp = analogRead(senseUp);
Serial.println(analogRead(senseUp));
valUp = constrain(valUp, 790, 970);
int ledLevelUp = map(valUp, 790, 970, 0, 255);
//int valUp = (analogRead(senseUp));

if(ledLevelUp > 120){
//digitalWrite(relayC,HIGH);
Timer1.restart();
Timer1.start();
Timer1.attachInterrupt(OnUp);}
else{
digitalWrite(relayC,LOW);}}}

void OnDown(){
digitalWrite(relayB,HIGH);
}

void OnUp(){
digitalWrite(relayC,HIGH);}

Sorry, I don't understand the Timer library - only the use of millis() as in the demo several things at a time

I think you want something like this pseudo code

void loop() {
   currentMillis = millis()
   if (tripwire triggered) {
      startMillis = currentMillis;
      ledON = true;
   }

   operateLED();
}

void operateLED() {
  if (ledON == false) {
      return;
  }
  if (currentMillis - startMillis >= 22000) {
     digitalWrite(ledPin, LOW);
     ledON = false;
  }
  else {
     digitalWrite(ledPin, HIGH);
  }
}

...R

thank you so much robin2, if i am to manipulate 2 led should i make 2 void operateLED or should i use if else case inside the "void operateLED"

what does karma means?

Carllamorie:
thank you so much robin2, if i am to manipulate 2 led should i make 2 void operateLED or should i use if else case inside the "void operateLED"

You can do it either way. If you are a beginner it may be easier to understand if you make a separate function. Note that they are "functions" not "voids". Void just tells the compiler that the function does not return a value. Some functions return values and they are prefixed with byte or int rather than void. In the demo several things at a time I used separate functions

Back in the day (i.e. a few weeks ago before they broke the Forum with an unwanted upgrade) it used to show how many Karma points each person had earned. You can still click the button to award someone a point and you can see your own Karma if you look at your profile by clicking your username at the side of your post.

...R

Carllamorie:
what does karma means?

at one time this forum would display a karma number.
the idea was that if you received a valuable answer or someone really spent time trying to help you, Then you would click the 'add karma' button to say thanks.
then, when you see two people with reply's and one had a lot of karma, they might be the one who has helped others more and the reply would be more valuable.
I have seem some people post silly replies on multiple threads. they have a very high post count, but the value of their answers is low.
I think folks like Robin2 and crossroads broke the counter as it did not go up that high and the site stopped posting that number.
but, the short answer is that someone went out of their way to help, click the karma to say 'thanks'