need 1s high every 20 m from arduino

Discussion in 'Embedded Systems and Microcontrollers' started by fredric58, Sep 17, 2015.

  1. fredric58

    Thread Starter Member

    Nov 28, 2014
    143
    0
    hi all, new to arduino, did the sparkfun SKI and it was fun but didn't cover timing. is there a way to program the uno to send a HIGH signal to an outout for 1-2s every 20-30 minutes? the delay() doesn't work because it shuts other tasks down.

    thanks
     
  2. MikeML

    AAC Fanatic!

    Oct 2, 2009
    5,450
    1,066
    Use the Reference. Follow the "Blink without Delay()" example...
     
    fredric58 likes this.
  3. fredric58

    Thread Starter Member

    Nov 28, 2014
    143
    0
    thanks mike, I looked over the code, so....1,200,000 millis for 20 minutes? sure I am missing something? like I said , it's ALL to me.
    let me ask this. there are a half a dozen things I want to accomplish with an arduino. I can do most of them "individually" right now after 2 days of getting a starter kit. any advice on merging theses sketches.

    thanks,
    oh...that's a fun plane fly!172?
     
  4. MikeML

    AAC Fanatic!

    Oct 2, 2009
    5,450
    1,066
    20min * 60sec/min * 1000ms/sec = 1200000ms

    Code (Text):
    1.  
    2. ...
    3. unsigned long EndTime;
    4. ...
    5. EndTime = millis() +1200000;  //20min from now
    6. ...
    7. do
    8. {
    9. // The stuff you want to do before 20min elapses goes here
    10. } while (millis()<EndtTime);
    11.  
    12. //The stuff you want to do after 20min elapses goes here
    13.  
    Avatar is the wife's Piper PA20 Pacer. We also have a Cessna 182...
     
  5. dannyf

    Well-Known Member

    Sep 13, 2015
    1,810
    362
    Easy:

    Code (Text):
    1.  
    2.   if (time_now() - start_time < 2) set_output();
    3.   else clear_output();
    4.   if (time_now() - start_time > 30*60) start_time+=30*60;
    5.  
    just insert it in the loop and you will be fine.
     
  6. fredric58

    Thread Starter Member

    Nov 28, 2014
    143
    0
    thanks guys. I will experiment with it as an individual project, then try to merge the other sketch I already have. thanks again, have a great weekend.
     
  7. fredric58

    Thread Starter Member

    Nov 28, 2014
    143
    0
    hi, oh my goodness. there are SO MANY WAYS to accomplish the same thing. some are simple and some seem quite complex. i am going to back up a little bit from the timer and start a little simpler.

    i have a sketch for a LDR circuit, and it's SIMPLE. all it does is send power to an output pin when you cover up the LDR. when you take your finger off the LDR the output pin turns off. it powers a PIR. i would like to learn how to add something simple. how/where to place/define it in the header and how/where to place it in the sketch. i would like to turn another pin on for 10s at the same time. i want to use a LED so i have a visual indicator of where my hysteresis is set. after this i will attempt on my own to add a push button and another LED to the sketch.

    another, maybe most important thing i would like to learn from this is the "WHY" is it in this location/position in the sketch?

    thanks!

    int input = 0; //analog input 0 is connected to the sensor
    int output = 2; //digital pin 2 is connected to the transistor base
    int threshold = 812; // nominal threshold
    int hyst = 50; // on when input above threshold + hyst, off when below threshold - hyst
    int samplingDelay = 800; // higher values for slow changing signals, lower values for faster response

    void setup() {
    pinMode(output, OUTPUT); //initializes pin 2 as an output
    }

    void loop() {
    int v;

    // read the value
    v = analogRead(input);

    // if it's higher than the on-threshold
    if (v > (threshold + hyst)) {
    // turn the output high
    digitalWrite(output, HIGH);
    }
    // if is't lower than the off-threshold
    else if (v < (threshold - hyst)) {
    // turn the output low
    digitalWrite(output, LOW);
    }
    // else don't change the output state

    // wait before taking another sample
    delay(samplingDelay);
    }
     
  8. dannyf

    Well-Known Member

    Sep 13, 2015
    1,810
    362
    People's responses to your question are directly related to how well you ask your questions. Help others help you.

    (I'm guessing here) you are trying to implement the following:
    1) once activity is detected, light up the PIR and start a timer;
    2) if 10 minutes passed, turn off the PIR.

    So something like this would be incremented this way:

    Code (Text):
    1.  
    2.   if (activity_detected()) {
    3.     start_time = time_now();
    4.     turn_on_PIR();
    5.   }
    6.   if ((PIR_on()) && (start_time - time_now() > 10 minutes) {
    7.     turn_off_PIR();
    8.   }
    9.  
    You just need to then implement those little blocks.

    You will find that programming top down, like the example above, is much easier. Once you get the logic right, the rest is a walk in the park.
     
    Last edited: Sep 21, 2015
  9. fredric58

    Thread Starter Member

    Nov 28, 2014
    143
    0
    yes I understand the question is as important to the answer. i'll get there. just new to the concept. found a really good video tutorial yesterday that's very easy to understand. i'll be doing that for the rest of the week. LOTS to learn!
     
  10. sirch2

    Well-Known Member

    Jan 21, 2013
    1,008
    351
    Just something a couple of things to consider if using long delays:

    a) I guess you are not looking for a high degree of accuracy in the timing but using things like "delay" and loop counting are not going to give accurate timings (for that you would be better off with a real time clock chip, there is lots of info on using the Arduino with a DS1307.

    b) IIRC "delay" wraps around at some point (65000-odd milliseconds possibly) so trying to use 1200000 may not work
     
  11. MikeML

    AAC Fanatic!

    Oct 2, 2009
    5,450
    1,066
    Here is how the Arduino millis() function works:

    millis.gif
     
    Last edited: Sep 22, 2015
  12. fredric58

    Thread Starter Member

    Nov 28, 2014
    143
    0
    hi, hope everybody had a great weekend! one you'll never forget! ok, here's the deal...I have been taking/subscribing to a guy named mcWhorter who has a, what I think a pretty good you tube video class for beginners. I have actually learned quite a bit since my last question.

    but I have run into a problem. and I need your help. I can count LED blinks, make one blink, then another. I can analog write different voltages. I can do everything in his tutorials up to lesson 15. however I can't figure this one out. "delay" screws everything up, time sensitive "while LOOP" doesn't quite get it. counting....like in a "for LOOP" doesn't work like (int j=1; j<=number; j=j+1), so I am kinda stuck.

    here is the challenge, my PIR doesn't have a fixed OUTPUT time. it "could be" 5s, or 7s, or 4s... it drifts. and there isn't anything I, you or God can do about it. so here is the question. code is attached.

    is there anyway I can say: ok, the PIR has been triggered, it has run/output for Xs. subtract THAT from the 10s I want and continue on till 10s is reached WITHOUT a break in its OUTPUT? in other words for those who complain about my questions, LOL "X" is a variable, it is never the same. ok? if it runs for 4s, I want to add 6s, if it runs for 7s, I want to add 3s to it. I must have 10s, I could even go 11s but no more. I thank ALL OF YOU who put up with the lesser learned like me.

    void setup() {

    int sensor=8;
    pinMode(sensor,INPUT); //from PIR (unstable)
    pinMode(3,OUTPUT); //light an LED for 10s
    pinMode(10,OUTPUT); //powers the PIR

    }

    void loop() {
    digitalWrite(10,HIGH); //powers PIR
    int sensor=8; // goes HIGH when something sensed

    if(digitalRead(sensor)==HIGH) //if PIR sees something, 3 goes HIGH
    digitalWrite(3,HIGH); //turns on LED for the length of time the PIR sends HIGH signal
    //WHICH IS THE PROBLEM!!!

    else digitalWrite(3,LOW); //TURNS OFF led




    }
     
  13. dannyf

    Well-Known Member

    Sep 13, 2015
    1,810
    362
    I think quite a few answers have been proposed so far. You are better off understanding how those approaches work and then figure out a way to implement them in your application.
     
  14. MikeML

    AAC Fanatic!

    Oct 2, 2009
    5,450
    1,066
    Do an up-edge-detection on when the PIR first turns on, and then generate your own delay.
     
Loading...