Skip to content

Commit

Permalink
adding files
Browse files Browse the repository at this point in the history
  • Loading branch information
elhayrobotican committed Apr 7, 2018
1 parent 6a23045 commit 37258fc
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 2 deletions.
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,30 @@
# CuckooTimer
Timer library for arduino
# SandTimer

This library allow you to set and use timers easily over Arduino platform. The implementation doesn't use delay function, and it is very fast. This is very important if you want your board to keep doing things in the "background" while using timer.

## Usage

1. Include SandTimer.h in your code

2. Create a new SandTimer instance

```
SandTimer my_timer;
```

2. Start the timer, and do something when it's finished

```
void loop()
{
...
...
my_timer.start(1000);
if (my_timer.finished())
Serial.println("time is up");
...
...
}
```

Although start() is invoked inside loop(), timer will only start once. Only after timer is finished, start() method will cause it to start again. If you wish to start timer again before it's finished, use startOver();
74 changes: 74 additions & 0 deletions SandTimer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@

/*******************************************************************************
* Copyright (c) 2018 Elhay Rauper
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted (subject to the limitations in the disclaimer
* below) provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
* THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/


#include "SandTimer.h"

SandTimer::SandTimer() {started_ = false;}

/* start timer. if already started, do nothing */
void SandTimer::start(unsigned long period)
{
if (!started_)
{
period_ = period;
start_time_ = millis();
started_ = true;
}
}

/* override original start time, and start timer again */
void SandTimer::startOver()
{
start_time_ = millis();
if (!started_)
started_ = true;
}

/* return true if timer has finished */
bool SandTimer::finished()
{
if (started_)
{
end_time_ = millis();
if (end_time_ - start_time_ >= period_)
{
started_ = false;
}
}
return !started_;
}

void SandTimer::reset() {started_ = false;}

54 changes: 54 additions & 0 deletions SandTimer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*******************************************************************************
* Copyright (c) 2018 Elhay Rauper
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted (subject to the limitations in the disclaimer
* below) provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
* THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/

#ifndef SAND_TIMER_H
#define SAND_TIMER_H
#include <Arduino.h>

class SandTimer
{
private:
unsigned long start_time_ = 0;
unsigned long end_time_ = 0;
unsigned long period_ = 0;
bool started_ = false;

public:
SandTimer();
void start(unsigned long period);
void startOver();
bool finished();
void reset();
};

#endif
56 changes: 56 additions & 0 deletions examples/print_timer/print_timer.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*******************************************************************************
* Copyright (c) 2018 Elhay Rauper
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted (subject to the limitations in the disclaimer
* below) provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
* THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/

#include "SandTimer.h"

SandTimer timer;
void setup() {
Serial.begin(9600);
// set timer to 100 ms and start it. you can call
// this function in loop() too, and it will only start
// the timer once (as long as it is not finished yet)
timer.start(1000);
}

void loop() {
if (timer.finished())
{
Serial.println("finished counting 1000 ms");
timer.startOver();
}

// you can still do some work here,
// because Timer functions are very fast, and
// will not block like delay() does

}
5 changes: 5 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SandTimer KEYWORD1
start KEYWORD2
startOver KEYWORD2
finished KEYWORD2
reset KEYWORD2
10 changes: 10 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name=SandTimer
version=1.0.0
author=Elhay Rauper
maintainer=Elhay Rauper
sentence=Easy to use and lightweight timer
paragraph=Easy to use and lightweight timer
category=Timing
url=https://github.com/elhayra/SandTimer
architectures=*
includes=SandTimer.h

0 comments on commit 37258fc

Please sign in to comment.