diff --git a/Libraries/Metro/Metro.cpp b/Libraries/Metro/Metro.cpp new file mode 100755 index 0000000..3d13747 --- /dev/null +++ b/Libraries/Metro/Metro.cpp @@ -0,0 +1,73 @@ + +#if defined(ARDUINO) && ARDUINO >= 100 +#include "Arduino.h" +#else +#include "WProgram.h" +#endif +#include "Metro.h" + + +Metro::Metro(unsigned long interval_millis) +{ + this->autoreset = 0; + interval(interval_millis); + reset(); +} + +// New creator so I can use either the original check behavior or benjamin.soelberg's +// suggested one (see below). +// autoreset = 0 is benjamin.soelberg's check behavior +// autoreset != 0 is the original behavior + +Metro::Metro(unsigned long interval_millis, uint8_t autoreset) +{ + this->autoreset = autoreset; // Fix by Paul Bouchier + interval(interval_millis); + reset(); +} + +void Metro::interval(unsigned long interval_millis) +{ + this->interval_millis = interval_millis; +} + +// Benjamin.soelberg's check behavior: +// When a check is true, add the interval to the internal counter. +// This should guarantee a better overall stability. + +// Original check behavior: +// When a check is true, add the interval to the current millis() counter. +// This method can add a certain offset over time. + +char Metro::check() +{ + if (millis() - this->previous_millis >= this->interval_millis) { + // As suggested by benjamin.soelberg@gmail.com, the following line + // this->previous_millis = millis(); + // was changed to + // this->previous_millis += this->interval_millis; + + // If the interval is set to 0 we revert to the original behavior + if (this->interval_millis <= 0 || this->autoreset ) { + this->previous_millis = millis(); + } else { + this->previous_millis += this->interval_millis; + } + + return 1; + } + + + + return 0; + +} + +void Metro::reset() +{ + + this->previous_millis = millis(); + +} + + diff --git a/Libraries/Metro/Metro.h b/Libraries/Metro/Metro.h new file mode 100755 index 0000000..cb21776 --- /dev/null +++ b/Libraries/Metro/Metro.h @@ -0,0 +1,26 @@ + + +#ifndef Metro_h +#define Metro_h + +#include + +class Metro +{ + +public: + Metro(unsigned long interval_millis); + Metro(unsigned long interval_millis, uint8_t autoreset); + void interval(unsigned long interval_millis); + char check(); + void reset(); + +private: + uint8_t autoreset; + unsigned long previous_millis, interval_millis; + +}; + +#endif + + diff --git a/Libraries/Metro/Metro.pdf b/Libraries/Metro/Metro.pdf new file mode 100755 index 0000000..f42f502 Binary files /dev/null and b/Libraries/Metro/Metro.pdf differ diff --git a/Libraries/Metro/about.txt b/Libraries/Metro/about.txt new file mode 100755 index 0000000..f075c6b --- /dev/null +++ b/Libraries/Metro/about.txt @@ -0,0 +1,7 @@ +VERSION 2.3.2 + +¥ Added this->autoreset = 0; to Metro(unsigned long interval_millis) as suggested by Paul Bouchier + +VERSION 2.3.1 + +¥ Updated for Arduino 1.0 \ No newline at end of file diff --git a/Libraries/Metro/examples/blinking/blinking.pde b/Libraries/Metro/examples/blinking/blinking.pde new file mode 100755 index 0000000..d6f9a70 --- /dev/null +++ b/Libraries/Metro/examples/blinking/blinking.pde @@ -0,0 +1,36 @@ +/* + This code will blink an LED attached to pin 13 on and off. + It will stay on for 0.25 seconds. + It will stay off for 1 second. + */ +#include //Include Metro library +#define LED 13 // Define the led's pin + +//Create a variable to hold theled's current state +int state = HIGH; + +// Instanciate a metro object and set the interval to 250 milliseconds (0.25 seconds). +Metro ledMetro = Metro(250); + +void setup() +{ + pinMode(LED,OUTPUT); + digitalWrite(LED,state); +} + +void loop() +{ + + if (ledMetro.check() == 1) { // check if the metro has passed its interval . + if (state==HIGH) { + state=LOW; + ledMetro.interval(250); // if the pin is HIGH, set the interval to 0.25 seconds. + } + else { + ledMetro.interval(1000); // if the pin is LOW, set the interval to 1 second. + state=HIGH; + } + digitalWrite(LED,state); + } +} + diff --git a/Libraries/Metro/examples/blinking_2_instances.zip b/Libraries/Metro/examples/blinking_2_instances.zip new file mode 100644 index 0000000..0ce0285 Binary files /dev/null and b/Libraries/Metro/examples/blinking_2_instances.zip differ diff --git a/Libraries/Metro/examples/blinking_2_instances/blinking_2_instances.pde b/Libraries/Metro/examples/blinking_2_instances/blinking_2_instances.pde new file mode 100644 index 0000000..5747c82 --- /dev/null +++ b/Libraries/Metro/examples/blinking_2_instances/blinking_2_instances.pde @@ -0,0 +1,51 @@ +// This code will blink output 13 every 250 ms +// abd will blink output 9 every 125 ms + + +#include // Include Metro library +#define LED0 13 // Define a LED pin +#define LED1 9 // Define another LED pin + +// Create variables to hold the LED states +int state0 = HIGH; +int state1 = HIGH; + +// Instantiate a metro object and set the interval to 250 milliseconds (0.25 seconds). +Metro metro0 = Metro(500); + +// Instantiate another metro object and set the interval to 125 milliseconds (0.125 seconds). +Metro metro1 = Metro(125); + +void setup() +{ + pinMode(LED0,OUTPUT); + digitalWrite(LED0,state0); + + pinMode(LED1,OUTPUT); + digitalWrite(LED1,state1); + +} + +void loop() +{ + + if (metro0.check() == 1) { // check if the metro has passed its interval . + if (state0==HIGH) { + state0=LOW; + } else { + state0=HIGH; + } + digitalWrite(LED0,state0); + } + + if (metro1.check() == 1) { // check if the metro has passed its interval . + if (state1==HIGH) { + state1=LOW; + } else { + state1=HIGH; + } + digitalWrite(LED1,state1); + } + + +} diff --git a/Libraries/Metro/examples/serialInterval/serialInterval.pde b/Libraries/Metro/examples/serialInterval/serialInterval.pde new file mode 100755 index 0000000..68a9d41 --- /dev/null +++ b/Libraries/Metro/examples/serialInterval/serialInterval.pde @@ -0,0 +1,24 @@ +// This example sends a Serial message every 250 milliseconds + +#include // Include the Metro library + +Metro serialMetro = Metro(250); // Instantiate an instance + +void setup() { + Serial.begin(115200); // Start the Serial communication +} + +void loop() { + + if (serialMetro.check() == 1) { // check if the metro has passed it's interval . + // Output all the analog readings seperated by a space character + for (int i = 0; i < 6; i++ ) { + Serial.print (analogRead( i) ); + Serial.print(32,BYTE); + } + // Terminate message with a linefeed and a carriage return + Serial.print(13,BYTE); + Serial.print(10,BYTE); + } +} + diff --git a/Libraries/Metro/keywords.txt b/Libraries/Metro/keywords.txt new file mode 100755 index 0000000..62ca8a2 --- /dev/null +++ b/Libraries/Metro/keywords.txt @@ -0,0 +1,25 @@ +####################################### +# Syntax Coloring Map For Test +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +Metro KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +check KEYWORD2 +interval KEYWORD2 + +####################################### +# Instances (KEYWORD2) +####################################### + +####################################### +# Constants (LITERAL1) +####################################### + diff --git a/Universal_Serial_Adapter/Libraries/Metro/Metro.cpp b/Universal_Serial_Adapter/Libraries/Metro/Metro.cpp new file mode 100755 index 0000000..3d13747 --- /dev/null +++ b/Universal_Serial_Adapter/Libraries/Metro/Metro.cpp @@ -0,0 +1,73 @@ + +#if defined(ARDUINO) && ARDUINO >= 100 +#include "Arduino.h" +#else +#include "WProgram.h" +#endif +#include "Metro.h" + + +Metro::Metro(unsigned long interval_millis) +{ + this->autoreset = 0; + interval(interval_millis); + reset(); +} + +// New creator so I can use either the original check behavior or benjamin.soelberg's +// suggested one (see below). +// autoreset = 0 is benjamin.soelberg's check behavior +// autoreset != 0 is the original behavior + +Metro::Metro(unsigned long interval_millis, uint8_t autoreset) +{ + this->autoreset = autoreset; // Fix by Paul Bouchier + interval(interval_millis); + reset(); +} + +void Metro::interval(unsigned long interval_millis) +{ + this->interval_millis = interval_millis; +} + +// Benjamin.soelberg's check behavior: +// When a check is true, add the interval to the internal counter. +// This should guarantee a better overall stability. + +// Original check behavior: +// When a check is true, add the interval to the current millis() counter. +// This method can add a certain offset over time. + +char Metro::check() +{ + if (millis() - this->previous_millis >= this->interval_millis) { + // As suggested by benjamin.soelberg@gmail.com, the following line + // this->previous_millis = millis(); + // was changed to + // this->previous_millis += this->interval_millis; + + // If the interval is set to 0 we revert to the original behavior + if (this->interval_millis <= 0 || this->autoreset ) { + this->previous_millis = millis(); + } else { + this->previous_millis += this->interval_millis; + } + + return 1; + } + + + + return 0; + +} + +void Metro::reset() +{ + + this->previous_millis = millis(); + +} + + diff --git a/Universal_Serial_Adapter/Libraries/Metro/Metro.h b/Universal_Serial_Adapter/Libraries/Metro/Metro.h new file mode 100755 index 0000000..cb21776 --- /dev/null +++ b/Universal_Serial_Adapter/Libraries/Metro/Metro.h @@ -0,0 +1,26 @@ + + +#ifndef Metro_h +#define Metro_h + +#include + +class Metro +{ + +public: + Metro(unsigned long interval_millis); + Metro(unsigned long interval_millis, uint8_t autoreset); + void interval(unsigned long interval_millis); + char check(); + void reset(); + +private: + uint8_t autoreset; + unsigned long previous_millis, interval_millis; + +}; + +#endif + + diff --git a/Universal_Serial_Adapter/Libraries/Metro/Metro.pdf b/Universal_Serial_Adapter/Libraries/Metro/Metro.pdf new file mode 100755 index 0000000..f42f502 Binary files /dev/null and b/Universal_Serial_Adapter/Libraries/Metro/Metro.pdf differ