clock /timer ???

Hello to all , I have a project in developement that requires 2 pir sensors. during programme excution the first pir sensor willl be triggered and this should start a clock/ timer from zero. the second pir sensor should stop the same clock / timer . i then want to save this value to my phone. now for the time being I just need advice on how to programme the clock /timer part of this to test it. I have been reading about time delays but i believe they just stop a program excuting until the time has expired. what i would like is to have the clock /timer start and stop( pir dependent) while the programe is running. any info would be appreciated .

Thanks in advance Dermot.

Sure, record the PIR1 time using millis(). Record the PIR2 time using millis(). Subtract.

cheers aarg!!!

This is the great beauty of Arduino-Hidden! Simple solution - just use the millis()! I genuinely appreciate @aarg's approach of delivering hints/solution understanding the requirements level of the help seeker.

But, what about those people who are reading/consulting/analyzing the cryptic 442 pages of the ATmega328 data sheets? They might be proposing/expecting otherwise complicated solutions which the OP will be ignoring; but, these solutions could be matter of pleasure to those who are interested to explore the TCX (X=0, 1, 2) architecture of the ATmega328.

Algorithm to measure elapsed time between the actuations of two sensors through interrupts:

1. Initialize INT0 and INT1 interrupts
2. Initialize TC1 as Timer-1 for normal up-counting with suitable clkTC1
3. while (INT0 is not active)
4.    goto 3
5. Start Timer-1 
6. Keep recording elapse time by poling TOV1 flag
7. while (INT1 is not acive)
8.    goto 6
9. Stop Timer-1 and read the elapsed time.

GolamMostafa:
4. goto 3

Oh dear a goto.

This is an algorithmic pseudo code; in actual coding, it will be replaced by while()-do structure! In many cases, it is really difficult to describe program-branching logic without goto statements!

GolamMostafa:
This an algorithmic pseudo code; in actual coding, it will be replaced by while()-do structure!

Structured languages were designed in part, to support algorithms. Algol-68 even had it in it's name!

needless to say my attempt at this has failed. I don't know if i'm on the right or wrong track with this so rather than mess about with it I would like some guidance to save myself some time. any feedback will be appreciated.

byte switch1 = 3;
byte switch2 = 5;
byte led = 7;

void setup() {
  pinMode (switch1, INPUT);
  pinMode (switch2, INPUT);
  pinMode (led, OUTPUT);

  // put your setup code here, to run once:
  Serial.begin(9600); //Set baud rate.
}

void loop() {
  // put your main code here, to run repeatedly:
  float buttonState_1 =  digitalRead(switch1);
  float buttonState_2 =  digitalRead(switch2);
  float value = buttonState_1 - buttonState_2;

  if (buttonState_1 == 1) {
    digitalWrite(led, HIGH);
    buttonState_1 = millis();
  }
  else {
    digitalWrite(led, LOW);
  }

  if (buttonState_2 == 1) {
    digitalWrite(led, HIGH);
    buttonState_2 = millis();
    Serial.print("TIME = ");
    Serial.println(value);
  }
  else {
    digitalWrite(led, LOW);
  }

  delay(1000);
}

mechup:
needless to say my attempt at this has failed. I don't know if i'm on the right or wrong track with this so rather than mess about with it I would like some guidance to save myself some time. any feedback will be appreciated.

byte switch1 = 3;

byte switch2 = 5;
byte led = 7;

void setup() {
  pinMode (switch1, INPUT);
  pinMode (switch2, INPUT);
  pinMode (led, OUTPUT);

// put your setup code here, to run once:
  Serial.begin(9600); //Set baud rate.
}

void loop() {
  // put your main code here, to run repeatedly:
  float buttonState_1 =  digitalRead(switch1);
  float buttonState_2 =  digitalRead(switch2);
  float value = buttonState_1 - buttonState_2;

if (buttonState_1 == 1) {
    digitalWrite(led, HIGH);
    buttonState_1 = millis();
  }
  else {
    digitalWrite(led, LOW);
  }

if (buttonState_2 == 1) {
    digitalWrite(led, HIGH);
    buttonState_2 = millis();
    Serial.print("TIME = ");
    Serial.println(value);
  }
  else {
    digitalWrite(led, LOW);
  }

delay(1000);
}

Please define "failed"? I am starting to experiment/learn this types of timer functions so to know what failed means helps.
Why do you have a delay at the end? Delays stop everything until delay has ended, so your "if" statements will not be tested during the delay period. The "blink without delay" system is easy to implement and doesn't stop the program like a delay does.

@mechup

during programme excution the first pir sensor will be triggered and this should start a clock/ timer from zero.

/--------------------------------------------------------------------------------------------------------------------

You have clearly indicated in your statement that the Timer will begin 'time counting' from zero, and this counting will be initiated only when the 1st pir sensor is activated.

millis() has started counting time long before your 1st pir sensor has activated. Use of millis() is not objectionable, but it does not quite agree with your statement.

If you don't feel comfort of using interrupts of Post #3, you may use polling methodology for sensing the active conditions of your sensors.

/--------------------------------------------------------------------------------------------------------------------

However, use of millis() as suggested by @aarg is a good option for you.

/--------------------------------------------------------------------------------------------------------------------

Draw a simple Flow Chart of the solution that you have created in your mind for your problem, and then convert it into Arduino/C instructions. This approach will inspire you tremendously to think yourself what to try/do next to solve the problem. In the long run, you will discover that you have attained the capability of solving many problems without depending much on others. Learning from yourself is the best way of learning!

/------------------------------------------------------------------------------------------------------------------

If you are measuring times < 100 millis it would be better if you use micros() instead of millis().

The millis() function skips 6 values counting up the low 8 bits to make 250 into 256.
The low 8 bits of millis() will never == 255, for example.

The micros() function does not do that.

As far as making start time zero, there is no need at all.

it seems I need to do some more research regarding my understanding of whats required. I appreciate all the positve feedback and help so far. you have pointed me in a direction I can explore. thanks to all .

Please define "failed"? I am starting to experiment/learn this types of timer functions so to know what failed means helps.
Why do you have a delay at the end? Delays stop everything until delay has ended, so your "if" statements will not be tested during the delay period. The "blink without delay" system is easy to implement and doesn't stop the program like a delay does.

The delay was used to slow down the results on the serial monitor. It is not a requirement for the final code.
this piece of code was written in an attempt to gain an understanding of 2 new concepts(for me!)

1 How to code a simple substraction.
2 How to code the millis() function and have the results show on the serial monitor.

I would consider this a failure because I haven't achieved my goal. But I am further down the road regarding this challenge. I have found a post from "Robin2 " in the" project guidance section " called
"several things at the same time" I have briefly read it and it will occupy me for a while , I would think!!!

thanks once again.

With these 2 tutorials you will learn about timing and how to use it.
The same material is presented in the Many Things At Once thread only in a different way.

  1. Gammon Forum : Electronics : Microprocessors : How to do multiple things at once ... like cook bacon and eggs
  2. Gammon Forum : Electronics : Microprocessors : How to process incoming serial data without blocking

Whatever way you choose, even both, be sure to write or modify code to check your understanding, reinforce your memory and develop habits to make this easier and easier. There is no lesson more fundamental to automation than that one.

It is worth all the time it takes. Not learning it can result in wasting more time and effort than learning it, many times over.

Hi,
How have you got your PIR output wired to the Arduino?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

hi TomGeorge,
hope you can follow this. I have used different pins but the layout is the same. I have only put 1 PIR sensor in the drawing just to make it more legible.

Hi,
What sort of configuration is the PIR output, relay , NPN?

Can you see the input to the Nano going HIGH and LOW on your IDE monitor?

OPs circuit;


Tom.. :slight_smile:

to be honest, No. I wired it up as per diagram and used an LED with some code to verify that it worked. Is there something wrong with the way I configured it . should I have wired and coded it a different way? I very keen to know. any feedback educates me.
here is the PIR sensor https://www.amazon.co.uk/gp/product/B007XQRKD4/ref=oh_aui_detailpage_o03_s00?ie=UTF8&psc=1

The 9V battery through the regulator may not deliver enough current is one possible problem, but I don't know how much the PIR needs so maybe not. Does it run the same when connected to the PC-USB?

Ya , It's fine . It works perfectly. I don't have a problem with the PIR not working. It triggers the led with simple code . I just need to read up on the programing side of things so I have a better understanding of what I'm hping to accomplish.

FYI, my suggestion about using millis() in reply #1 has nothing to do with more-than-one-thing-at-a-time multitasking or anything like that. It's just a simple time interval measurement.