I/O anti-chattering library

Hello,

This is my first attempt to create a library for Arduino.
I am trying to create a library that provides functions to implement a timer to connection and/or disconnection to be applied for example to set a flag only if a Digital Input has been HIGH for more than 200 CPU cycles, not with just a single one.

The constructor of the class receives a char to select which type of timer is it. Within the class, the constructor sets a class member function pointer to the appropriate timer function.

The problem is that if there is only one instance of the class (one timer), it works fine, but as soon as I include a second one, it starts messing…

Can anyone give me some light on what can it be going wrong??

Thanks in advance!!

Example1.ino (2.79 KB)

PLCTimer.cpp (1.3 KB)

PLCTimer.h (672 Bytes)

Hi mizamae

volatile static int _timer;

Is each instance of PLCTimer meant to have its own member variable _timer? Or are all instances meant to share the same variable?

You have declared _timer as static, so I think the second case is going to be true.

Regards

Ray

Yes, they are supposed to have different _timer variables.

By declaring it static I wanted it to keep its value from call to call, how should I get that?? declaring the whole class as static??

I have seen something similar to what I want. It is to declare the member with the qualifier "static thread_local" but the compiler gives me the following error:

In file included from Example1.ino:6: E:\Datos\Documents\Arduino\libraries\PLCTimer/PLCTimer.h:21: error: ISO C++ forbids declaration of 'thread_local' with no type E:\Datos\Documents\Arduino\libraries\PLCTimer/PLCTimer.h:21: error: expected ';' before 'volatile'

Has anyone required a member within a class to be persistent (retain its value from execution to execution)??

Thanks!!

Member variables exist from when the object instance is created to when it is destroyed. If you assign a value to a member variable, it keeps that value until you assign a new value to it (or the instance is destroyed).

Try removing the static and see what happens.

Static means something a little different for class memebers. Your "global" class members (is that a proper term) will stay around from call to call just like normal global variables will. AS long as they are defined out side of a function they work like they are global variables except they are only available inside the class.

A static member in a class means that all instances of the class share the same instance of that variable. So any change you make to that variable, even from inside another instance of the class, affects it in all instances.

Thanks you all guys!! I will be removing all static identifier and will see if the members keep its value...

Thanks!!