Simple timer function with LDR sensor ?

Hello everybody

As so many others i am very new to this, i have no experience what so ever, but i wat to lern…

My question is: Is there any easy way to get the timer to work in Hour, Minutes and Seconds instead of milliseconds?

My goal is to get a led light to light when the LDR reached a specific value and stay on for, lets say 7 hour and 28 minutes and to stay off untill the next dawn, i think it would be easiest to define the “delay” in Hours and Minutes instead of do the math every time that i want to change the time.

This is what my litle code looks ike at this point:

  int Pin =  2;               // LED connected to digital pin 2
  int photoPin = 0;
  int photoReading;    
  void setup()               // The setup() method runs once, 
{                            // when the sketch starts

  pinMode(2, OUTPUT);        // initialize the digital pin as an output: 
  Serial.begin(9600);  

}                
                  
void loop()                 // The loop() method runs over and over again,
{                           // as long as the Arduino has power
  photoReading=analogRead(photoPin);  
  if (photoReading <400)    // This is the darknes value that turns the LED on
{    
  digitalWrite(2, HIGH);  
  delay(10000);             // Maintains the time that the LED is on
}
  else
{     
  digitalWrite(2, LOW);   
}  
  Serial.println(photoReading);  
  delay(1000);              // The interval that serialmonitor is updating info, 
}                           // in its window

I hope that you guys that have more experience of this can help me with this.

Best Regards
// Leif

Is there any easy way to get the timer to work in Hour, Minutes and Seconds instead of milliseconds?

No, the timer works in it's own special way.

YOU can convert the number of milliseconds elapsed to hours, minutes, seconds, etc. You know how many milliseconds in a second, so divide by 1000 to get total seconds. Then, divide that by 60 to get total minutes. Then, divide that by 60 to get hours. Subtract number of hours times 60 from total minutes to get minutes. Subtract total minutes times 60 from total seconds to get seconds.

On the other hand, if a pin is supposed to be turned on for some number of hours and minutes, that time can be converted to milliseconds, and used easier than converting milliseconds to hours, minutes, and seconds.

The arithmetic to count mSec is not so bad.

A bigger problem occurs when you decide to have a cancel button that isn't the reset button.

One way around this is to use my Timer library that doesn't hog the CPU while its busy 'delaying'.

#include "Timer.h"

Timer t;
int pin = 13;
long hours = 7;
long mins = 30;

void setup()
{
  pinMode(pin, OUTPUT);
  t.pulse(pin, (hours * 60 +  mins) * 60000, HIGH);
}

void loop()
{
  t.update();
}

Hi all, I'm having a similar problem. I would like to include Si timer library....how do I do this..

I have a proximity switch and used the included code as shown.

I want to have pin 13 time on for about 30 mins etc, similar in ways to original poster, except I know much less.

here is what I'm using...where do I copy and paste Si timer code...tried but get errors.

const int InfraredSensorPin = 4;//Connect the signal pin to the digital pin 4 const int LedDisp = 13;

void setup() { Serial.begin(57600); Serial.println("Start!"); pinMode(InfraredSensorPin,INPUT); pinMode(LedDisp,OUTPUT); digitalWrite(LedDisp,LOW); }

void loop() { if(digitalRead(InfraredSensorPin) == LOW) digitalWrite(LedDisp,HIGH); else digitalWrite(LedDisp,LOW); Serial.print("Infrared Switch Status:"); Serial.println(digitalRead(InfraredSensorPin),BIN); delay(50); }

t.pulse(pin, (hours * 60 + mins) * 60000UL, HIGH);

Literals are interpreted as ints, in the absence of any directives to the contrary. 60000 is an integer value; it is not an int value. The UL tells the compiler to handle 60000 correctly.

where do I copy and paste Si timer code…tried but get errors.

The code you posted does not contain any of Si’s code, so we can’t reproduce the errors you got. Therefore, we can’t tell you how to fix them.

where do I copy and paste Si timer code...tried but get errors. The code you posted does not contain any of Si's code, so we can't reproduce the errors you got. Therefore, we can't tell you how to fix them.

My question was where do I insert Si's code into my existing code.

I have been researching on the web for hours and can't find an answer....there probably out there but my lack of understanding doesn't help.I'm wondering if I should start from scratch.

where do I copy and paste Si timer code...tried but get errors.

You said that. We can't guess where you pasted it, or that you copied and pasted everything you needed to, or that you downloaded the needed library, or what errors you are getting.

You need to answer some questions, and post some code, if you really want help.

This is the code from 2 examples I have put together to make one.

const int InfraredSensorPin = 4;//Connect the signal pin to the digital pin 4
const int timer = 13;

void setup()
{
Serial.begin(57600);
Serial.println(“Start!”);
pinMode(InfraredSensorPin,INPUT);
pinMode(pin 13,OUTPUT);
t.pulse(pin, (mins * 30) *30000,high);
}

void loop()

{
if(digitalRead(InfraredSensorPin) == LOW) digitalWrite(timer,HIGH);
else digitalWrite(timer,LOW);
Serial.print(“Infrared Switch Status:”);
Serial.println(digitalRead(InfraredSensorPin),BIN);
t.update();
}

Basically I have a proximity switch on pin 4.
I need an output with a run on timer for 30 minutes adjustable to suit, after proximity sensor activated…at this moment pin 13 only activated momentary with proximity sensor.

I am using pin 13 for the output.

Do i need to change output pins…or is pin 13 output timer suitable for this application.

This is the code from 2 examples I have put together to make one.

You missed some parts.

#include "Timer.h"

Timer t;

This defines the timer class (you need to download the Timer library) and creates an instance of the class, called t.

I need an output with a run on timer for 30 minutes adjustable to suit, after proximity sensor activated...at this moment pin 13 only activated momentary with proximity sensor.

You should look at the blink without delay example. It defines a state machine. The states are LED on and LED off. The transition occurs automatically after a defined interval.

You have a slightly (only slightly) more complicated situation, in that you have "motion sensed", "no motion sensed", "pin on", and "pin off" states.

The transition from "no motion sensed" to "motion sensed" is easy enough to detect - the pin now reads LOW. The transition from "motion sensed" to "no motion sensed" is also easy to detect - the pin now reads HIGH.

Triggering the "pin on" state is for you to perform, by turning the pin on and noting the time.

Triggering the "pin off" state is for the Arduino to do, when sufficient (and you define what is sufficient) time has passed since the "pin on" state was transitioned to. It is detecting when to perform this transition that the blink without delay example illustrates.

Si's library is a bit overkill for your situation, but you should use this as a learning opportunity. First, learn how to do what you want with Si's code. Then, learn how to do it with Si's code. In the future, then, you'll be able to do it either way, as appropriate.

Tried the blink without delay option...still no result....at this stage the timer option is too complicated.

I'll persevere tomorrow....today now.

thanks for your help...but its not working for me,cant get my head around some of this stuff yet.

Thank you Si and Paul for your answers, i have not tried with the library yet but i should try during this weekend.

// Leif