Pages: [1]   Go Down
Author Topic: Timing Function  (Read 748 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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;
  }}
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 506
Posts: 19120
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you mean this?

Code:
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?
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 506
Posts: 19120
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Well post the code that compiles.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#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;
  }}
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
if (timeIsUp()) {
  // blink led
}

Then you could define timeIsUp()
Code:
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;
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 506
Posts: 19120
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 506
Posts: 19120
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 224
Posts: 13917
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: