I've followed http://arduino.cc/en/Hacking/LibraryTutorial, and was able to successfully create, compile, and run a very simple stopwatch-like Time class. I'm encountering trouble when using that library to create multiple objects or instances.
Here's my .h
#ifndef Timer_h
#define Timer_h
#include "Arduino.h"
class Timer
{
public:
Timer();
void start();
void stop();
void reset();
unsigned long int getTime();
bool getState();
private:
bool _start;
bool _stop;
unsigned long int _current_time;
unsigned long int _start_time;
unsigned long int _stop_time;
unsigned long int _time;
};
#endif // Timer_h
and my .cpp
#include "Arduino.h"
#include "Timer.h"
Timer::Timer()
{
_start = false;
_stop = false;
_current_time=0;
_start_time = 0;
_stop_time = 0;
_time=0;
}
void Timer::start()
{
_current_time = millis();
if(_start == false)
{
_start_time = _current_time;
}
_start = true;
_stop = false;
}
void Timer::stop()
{
_current_time = millis();
if(_stop==false)
{
_stop_time = _current_time;
_time += _stop_time - _start_time;
}
_start = false;
_stop = true;
}
void Timer::reset()
{
_current_time = millis();
_start_time = _current_time;
_stop_time = _current_time;
_stop = false;
_time = 0;
}
unsigned long int Timer::getTime()
{
_current_time = millis();
if (_start == true)
{
return _time + _current_time - _start_time;
}
else
{
return _time;
}
}
bool Timer::getState()
{
return _start;
}
NOTE: I'm aware that there is already a Timer library out there. I've not installed it so there is no conflict here.
This works fine:
#include <Timer.h>
Timer t1;
boolean start = false;
boolean stop = false;
boolean reset = false;
const int button1 = 2;
const int button2 = 3;
const int button3 = 4;
volatile int triggerCount1 = 0;
volatile boolean interrupt1= false;
volatile int triggerCount2 = 0;
volatile boolean interrupt2= false;
void setup(){
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
Serial.begin(9600);
attachInterrupt(0, trigger1, FALLING);
attachInterrupt(1, trigger2, FALLING);
}
void trigger1(){
static unsigned long int _last_pulse_interrupt_time = 0;
unsigned long int pulse_interrupt_time = micros();
// If interrupts come faster than 250us, assume it's a bounce and ignore
if (pulse_interrupt_time - _last_pulse_interrupt_time > 250)
{
triggerCount1++;
interrupt1 = true;
}
_last_pulse_interrupt_time = pulse_interrupt_time;
}
void trigger2(){
static unsigned long int _last_pulse_interrupt_time = 0;
unsigned long int pulse_interrupt_time = micros();
// If interrupts come faster than 250us, assume it's a bounce and ignore
if (pulse_interrupt_time - _last_pulse_interrupt_time > 250)
{
triggerCount2++;
interrupt2 = true;
}
_last_pulse_interrupt_time = pulse_interrupt_time;
}
void loop(){
reset = !digitalRead(button3);
if (interrupt1 == true){
start = true;
interrupt1 = false;
}
if (interrupt2 == true){
stop = true;
interrupt2 = false;
}
if(start == true){
t1.start();
outputTimer();
outputTrigger1Count();
outputTrigger2Count();
start = false;
}
if(stop == true){
t1.stop();
outputTimer();
outputTrigger1Count();
outputTrigger2Count();
stop = false;
}
if(reset == true){
t1.reset();
outputTimer();
reset = false;
}
}
void outputTimer(){
Serial.print("Timer 1 State: ");
Serial.print(t1.getState());
Serial.print(", Time: ");
Serial.println(t1.getTime());
}
void outputTrigger1Count(){
Serial.print("Interrupt 1 Triggers: ");
Serial.println(triggerCount1);
triggerCount1=0;
}
void outputTrigger2Count(){
Serial.print("Interrupt 2 Triggers: ");
Serial.println(triggerCount2);
triggerCount2=0;
}
but if I do something like this, I run into problems with the timer values setting themselves to strange values:
#include <Timer.h>
Timer t1;
Timer t2;
Timer t3;
...
What do I need to do to get this working for multiple instances? Have been trying to understand how to approach this by going through C++ tutorials but haven't a clue what the problem is or where to begin.