Coder Social home page Coder Social logo

vishnumaiea / ptscheduler Goto Github PK

View Code? Open in Web Editor NEW
22.0 2.0 4.0 427 KB

Pretty tiny Scheduler or ptScheduler is an Arduino library for writing non-blocking periodic tasks easily.

License: MIT License

C++ 100.00%
arduino scheduler library timing delay non-preemptive tasks avr multitasking task-scheduler

ptscheduler's Introduction

ptScheduler

Pretty tiny Scheduler

Pretty tiny Scheduler or ptScheduler is a non-preemptive task scheduler and timing library for Arduino. It helps you write non-blocking, periodic tasks easily without using ordinary delay routines or using millis() or micros() functions. Delay routines can prevent other parts of your code from running until the delay is exhausted. Using millis() function in your code can make it harder to understand and manage. ptScheduler solves these problems.

ptScheduler acts as a wrapper for millis() or micros() based logic used for timing of tasks. Under the hood, ptScheduler uses the native micros() implementation of Arduino. The micros() function is a hardware timer-based ISR that increments a global counter variable (unsigned integer) every microsecond. This function is available on all microcontrollers that support the Arduino framework. ptScheduler was using millis() function previously, but the new micros() provides granular timing in microseconds compared to milliseconds.

When you create a new ptScheduler task, you can specify the time intervals and execution modes. There are two execution modes - ONESHOT and SPANNING. Oneshot task is executed only once every time an interval is elapsed. Spanning task on the other hand remains active during the span of an interval.

All class member variables and functions are public and therefore give you full control over your tasks, allowing dynamically changing the behavior of the task. To run a task, just enclose the call() function inside any conditional statements, either inside your infinite loop or inside a function. Every time you invoke the call() function, it checks if the elapsed time is larger than the preset interval. If yes, it will return true and cause the code under the conditional block to be executed.

ptScheduler is good mainly for control applications that require periodic polling of sensors, GPIOs, and other IO devices. ptScheduler tasks can coexist with preemptive tasks such as FreeRTOS tasks.

Hello World

Here is the basic Hello World example.

#include "ptScheduler.h"

// Create tasks
ptScheduler sayHello = ptScheduler (PT_TIME_1S); //time in microseconds

// Setup function runs once
void setup() {
  Serial.begin (9600);
}

// Infinite loop
void loop() {
  if (sayHello.call()) {  // Executed every second
    Serial.println ("Hello World");
  }

  // Add other tasks and non-blocking code here
}

Tutorial

Complete tutorial can be found at CIRCUITSTATE

Documentation

See the reference of all available functions and variables in the Documentation page.

ptscheduler's People

Contributors

alexmaurer-madis avatar vishnumaiea avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

ptscheduler's Issues

How to implement jitter ?

Firstly, love ptScheduler !!

I'm sending messages over serial between two Arduinos on the same schedule (say 100ms) but would like to ensure they don't all fire at the same time in a 'feast and famine' style which is hard on my receiver device.

How can I introduce a one time offset when my scheduler objects are created or similar so that i spread the load but maintain the timing of tasks ? ie: Two tasks scheduled at 100ms with a 50ms offset would result in alternating tasks triggering every 50ms.

Cheers !!

Arduino resetting - Looking for guidance

I'm working on an automotive project and would like to alter the frequency of my ptScheduler task (ptCalculateRpm) based on engine RPM. When I implement this as shown below the Arduino goes into a reset loop ... something is not happy.

Is this an edge case anti-pattern or am I missing something ?

The definition of the object is:
ptScheduler ptCalculateRpm = ptScheduler(PT_TIME_200MS);

The code that contains my desires is:

  if (ptCalculateRpm.call()) {
    detachInterrupt(digitalPinToInterrupt(rpmSignalPin));
    currentRpm = calculateRpm();
    attachInterrupt(digitalPinToInterrupt(rpmSignalPin), updateRpmPulse, RISING);
    // Change frequency of rpm calculation depending on RPM to avoid high error
    if (currentRpm > 1500) {
      ptCalculateRpm = ptScheduler(PT_TIME_50MS);
    } else {
      ptCalculateRpm = ptScheduler(PT_TIME_200MS);
    }
  }

Task runs indefinitely when sequence repetition is 1

When the sequence repetition is set to 1 with the help of setSequenceRepetition() function, the task run indefinitely. The correct behavior should be the should stop after executing the sequence only once.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.