timer problems

Hello everyone,

I'm having a little trouble with making a code for Arduino. I hope you can help me.
I'm trying to set a count down timer when LDR1 gets light. When LDR2 gets light when the timer is still counting down, a LED must go burning.
The problem is that I can't find a code for a timer witch you can set and reset.
I hope you guys can help me.

The millis() function can tell you when LDR1 triggered. It can tell you when LDR2 triggered. If the difference in times is less than some value, do something (like light the LED).

Creating a countdown timer has meaning only if you want to display the time. That's not listed as one of your requirements.

Loop is called in an infinite loop. You can test the current time, as reported by millis() against the time that LDR1 triggered, and do something if more than some amount of time has passed.

Hey PaulS,

Thank you for your reaction. I will try this today and let you know if it works.

The problem is that I can't find a code for a timer witch you can set and reset.

I would suggest writing some ;D

and let you know if it works.

you mean "let you know if I get it working" :wink:

I don't know very much about arduino, but I can still search for timers. And writing one is a litlle to high for me as a beginner :P.
And of course I mean of I get it working :D.

And writing one is a litlle to high for me as a beginner

Well I'm sure that with a bit of research you could get a good start amd you learn the most by doing something yourself.

Just put it this way, I have only been doing arduino for just over a year now. :slight_smile:

Mowcius

I tried the code, but I can't get it working. Maybe something is wrong with my code.

unsigned long time;
int LDR1 = 2;
int LDR2 = 3;
int LED1 = 13;

void setup()
{
  Serial.begin(9600);
  pinMode (LED1, OUTPUT);
  
}
void loop()
{
  Serial.print("Time: ");
  time = millis();                  //prints time since program started
  Serial.println(time);             //wait a second so as not to send massive amounts of data
  delay(1000);
    if (time <= 8333)               //if time is less then 8333ms
     {
       digitalWrite(LED1, HIGH);    //light LED1
       delay(2000);                 //wait 2 seconds
       digitalWrite(LED1,LOW);      //turn off LED1
     }
     else
     {
       digitalWrite(LED1,LOW);     // else turn off LED1, just do nothing
     }
    
}

I there something wrong with?

I tried the code, but I can't get it working. Maybe something is wrong with my code.

I'd say that's a valid assumption.

What is printed in the Serial Monitor? What is not working?

arduino says there's nothing wrong with the code, but when I look at the arduino bord--> no LED goes burning when I use light on LDR1 and LDR2.

Just naming a variable and giving it a value equal to a pin number does nothing about reading the value of the thing connected to that pin.

Somewhere, you need a analogRead() call or two.

if (time <= 8333)

This will be true only in the first eight seconds or so after downloading your code.
You need to make your timing relative to the last time you read the timer.

And writing one is a litlle to high for me as a beginner

That's the great thing about the Arduino - someone else has already done it for you.

What do you mean with:

You need to make your timing relative to the last time you read the timer.

?

In place of:-
if (time <= 8333)

you need to have the time you are going to end
endtime = millis() + 8333;
then you need to see if you have reached that time
if(millis() > endTime)
The millis counter keeps on going so what you have will only work one time after that time will always be greater than 8333 no matter what.

so I get this code now:

unsigned long time;
int analogPin1 = 2; // LDR1 connected to analogpin 2
int analogPin2 = 3; // LDR2 connected to analogpin 3
int LED1 = 13;      // LED connected to digitalpin 13
int val = 0;         // variable to store the value read
int endTime;

void setup()
{
  Serial.begin(9600);
  pinMode (LED1, OUTPUT);
  
}
void loop()
{
  Serial.print("Time: ");
  time = millis();                  // prints time since program started
  Serial.println(time);             // wait a second so as not to send massive amounts of data
  delay(1000);
  endTime = millis() + 8333;
  analogRead(analogPin1);
  analogRead(analogPin2);
    if (millis() > endTime)               // if time is less then 8333ms
     {
       digitalWrite(LED1, HIGH);    // light LED1
       delay(2000);                 // wait 2 seconds
       digitalWrite(LED1,LOW);      // turn off LED1
       delay(5);                     // wait 0.005 seconds
     }
     else
     {
       digitalWrite(LED1,LOW);      // else turn off LED1, just do nothing
     }
    
}

You are setting
endTime = millis() + 8333;

every time through the loop so it will never time out.
If you want it to be a one time only then put this in the setup.
However all that will happen is the light will flash after 8.3 seconds and then nothing will happen ever again.
I think you need to define what you actually want to do.

I want the ''timer'' to start at 0ms again when the loop starts again.
The ''timer'' doesn't have to count further than 8333ms actually.

It isn't easy resetting the timer's count to zero.
What is easy is setting your own "zero" by reading "millis" at the start of "loop", and basing your timings relative to this.