Go Down

Topic: Timing Function (Read 799 times) previous topic - next topic

BlackSnake

Hi All,

I am finding it difficult to find good instructions on writing my own functions. I have had a little success but when i tried to replace delay() with Elapsed timer i can not get it to work.
Any suggestions where i am falling over would be great.
Thanks
Code: [Select]
#include <Elapsed.h>

const int button = 8;
const int led = 13;

int buttonState = 0;
int pressed = false;
int activate = 0;

void setup()
{
  pinMode(button, INPUT);
  pinMode(led, OUTPUT);
}

static Elapsed t1;
void loop()
{
  buttonState = digitalRead(button);
  if (buttonState == LOW)
  {
    int a;
    a = myTimer();
  }
  digitalWrite(led,buttonState);
  //t1.reset();
}
int myTimer()
{
  if(!pressed && activate != HIGH)
  {
    activate = HIGH;
    pressed = true;
    t1.reset();
  }
  if(pressed && t1.intervalMs()>5000)
  {
    pressed = false;
    int b;
    b = buttonState;
    if(b == HIGH && a == HIGH)
      {
        b = LOW;
      }
    else
      {
        b = HIGH;
      }
    return b;
  }}


Nick Gammon

Do you mean this?

Code: [Select]
sketch_may20a.cpp: In function 'int myTimer()':
sketch_may20a:40: error: 'a' was not declared in this scope


What are you trying to do exactly?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

BlackSnake

No, sorry i forgot to delete that, it does compile with that not there. It was left over from experimenting.
I am trying to create a delay before switching on a led, without stopping everything else ( delay () )
At the moment the led just changes state instantly.
I tried putting in the code that you originally gave me into my function.
It works fine inside the main loop but i have to write it many times over and size is becoming an issue as i use an 88a chip
If i can get this to work it will help me to write software the correct way.

Nick Gammon

Well post the code that compiles.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

BlackSnake

Code: [Select]
#include <Elapsed.h>

const int button = 8;
const int led = 13;

int buttonState = 0;
int pressed = false;
int activate = 0;

void setup()
{
  pinMode(button, INPUT);
  pinMode(led, OUTPUT);
}

static Elapsed t1;
void loop()
{
  buttonState = digitalRead(button);
  if (buttonState == LOW)
  {
    int a;
    a = myTimer();
  }
  digitalWrite(led,buttonState);
  //t1.reset();
}
int myTimer()
{
  if(!pressed && activate != HIGH)
  {
    activate = HIGH;
    pressed = true;
    t1.reset();
  }
  if(pressed && t1.intervalMs()>5000)
  {
    pressed = false;
    int b;
    b = buttonState;
    if(b == HIGH )
      {
        b = LOW;
      }
    else
      {
        b = HIGH;
      }
    return b;
  }}

WizenedEE

I don't understand what you want myTimer() to do. It has no side effects and only returns a value that you immediately discard. [edit: nevermind, I see it does have side effects. You're not using the variables you modify within it elsewhere, though]

You can't just have a function that "delays without blocking" (blocking = using delay()). You could do it if you wanted to say
Code: [Select]

if (timeIsUp()) {
 // blink led
}


Then you could define timeIsUp()
Code: [Select]

bool timeIsUp() {
 static unsigned long lastFireTime;
 if (millis() - lastFireTime > 1000) {
   lastFireTime += 1000; // or lastFireTime = millis() This version is better since it doesn't drift
   return true;
 }
 return false;

Nick Gammon

I'm sorry, I'm totally confused by your code.

So confused, that to make it work I would have to just rewrite it.

Try and forget about timers for a minute and think about how you would do this with a watch. Say someone said to you, "10 seconds after you hear a knock on the door, turn on the light". How would you do that?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon


You can't just have a function that "delays without blocking" (blocking = using delay()).


Ah I think I see what you are trying to do, thanks to WizenedEE.

No, you can't take the Elapsed library, put it into a function, and call that when you want a delay. You may as well just use delay() if you are going to do that.

All the Elapsed library lets you do is tell how much time elapsed from a "trigger point". So for example, if you start (reset) the timer when the button is pressed, you can check the timer every time around the loop and see how long ago that happened. But you have to do that yourself, it doesn't magically do it for you.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

BlackSnake

Is this written correctly, keep in mind I am still trying to figure what functions can and can't do.
This code needs the button held down but I am not sure why ?

Code: [Select]
const int button = 8;
const int led = 13;

int buttonState = 0;

void setup()
{
  pinMode(button, INPUT);
  pinMode(led, OUTPUT);
}

void loop()
{
  buttonState = digitalRead(button);
  if (buttonState == LOW)// my button is wired backwards
  {
    int a;
    a = myTimer();
  }
  digitalWrite(led,buttonState);
}
int myTimer()
{
 
  int b; // flip flop for changing states after time has elapsed
  b = buttonState;
  if(b == HIGH)
  {
    b = LOW;
  }
  else
  {
    b = HIGH;
  }
  delay(2000);
  return b; // not sure if i need this line

robtillaart

Quote
keep in mind I am still trying to figure what functions can and can't do.


Please read (or buy) the excellent book - http://letrongngoc.tech.officelive.com/Documents/TheCProgrammingLanguageSecondEdition.pdf -

Although it is about C it is still one of the better books about programming imho.

And I like this one too - http://www.amazon.com/Computer-Programming-Introduction-Scientifically-Inclined/dp/9081278819
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up