Code Monkey home page Code Monkey logo

pseudopwm's Introduction

PseudoPWM for Arduino

Output pseudo PWM wave to arduino digital port.

Build Status

It generates a relatively long cycle pseudo PWM as milliseconds and output to the port.

Features

  • Specify cycle and duty in millisecond unit
  • Change cycle and duty dynamically
  • Suspend output and resume
  • It uses two ticker objects

Works on

It uses two Ticker objects. Useally Ticker class depends on the hardware timers equipped on the various Arduino boards. Hardware timers are implemented as Timer libraries and eventually work as Ticker's reality. Therefore, whether the PseudoPWM works properly depends on the Arduino board. However, ESP8266 works correctly.

Installation

Download this file as a zip, and extract the resulting folder into your Arduino Libraries folder.
See Installing Additional Arduino Libraries.

Example

  • Simple
// This sketch blinks simply the onboard LED of ESP8266.

#include <Arduino.h>
#include "PseudoPWM.h"

PseudoPWM PilotLED(2);

void setup() {
  PilotLED.Start(1000, 500);
}

void loop() {
  delay(100);
}
  • Change duty rate dynamically
// This sketch flashes the onboard LED of ESP8266.
// Also it is blinked 0.2 seconds for one second every 8 seconds.

#include <Arduino.h>
#include "PseudoPWM.h"

PseudoPWM PilotLED(2);              // ESP8266 onboard led is active-low
unsigned  long ts;
boolean   flash;

void setup() {
  PilotLED.Start(2000, 1700);       // Start PWM, lit for 0.3[s]
  flash = false;
  ts = millis(); 
}

void loop() {
  unsigned long ls = millis();
  unsigned long span = ls - ts;

  delay(500);
  if (span > 1000) {
    if (flash) {
      PilotLED.Start(2000, 1700);   // Restore cycle and duty
      digitalWrite(2, HIGH);        // turn off led
      flash = false;
    }
    if (span > 8000) {
      PilotLED.Start(200, 100);     // Change cycle and duty to blinking 0.2[s]
      flash = true;
      ts = ls;
    }
  }
}

Usage

Declare PseudoPWM object

#include "PseudoPWM.h"

PseudoPWM pwmout(uint8_t port);

Specify the port to output PWM. Output mode of the port is set to DIGITAL OUT in the constructor. Incidentally, Ticker.h is also included from PseudoPWM.h.

Methods

Start()

void PseudoPWM::Start(unsigned int cycle, unsigned int duty);

Starts PWM output. Specify cycle for the PWM cycle and duty for duty of PWM by millisecond unit.

Stop()

void PseudoPWM::Stop();

Stop PWM output. It only temporarily stops output and the timer is running. From this state, the output can be resumed by the Start method.

attach()

void PseudoPWM::attach(void(*callback)(void));

The function attached here is called every cycle period.

PseudoPWM pwmout(2);

void callbackOne() {
  // This is called every second. 
  Serial.println("Cycle tick.");
}

void setup() {
  pwmout.attach(callbackOne);
  pwmout.Start(1000, 100);
}

void loop() {
  ...
}

detach()

void PseudoPWM::detach();

Disable callback.

License

The PseudoPWM class is licensed under the MIT License.
Copyright © 2017 [email protected]

pseudopwm's People

Contributors

hieromon avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

sorphin

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.