Go Down

Topic: DigitalIO library for sensor/devices using Digital IO pins. (Read 394 times) previous topic - next topic


I am creating a DigitalIO library, the purpose being to simplify using sensors and devices that use the digital IO pins. Maybe it'll be of use to some here.

* For AVR boards like UNO it can generate faster more compact code by avoiding digitalRead/Write methods.
* It has built in debounce for level changes (like push butttons) as well as for brief trigger signal (like knock sensors).

I've added more specific support for:
* ultrasonic sensors (returns distance in mm, cm, inch 1/10th inch, 1/16 inch)
* rotary encoders (range of values can be constrained, supports interrupts on AVR to simplify main loop).

* For AVR, it also has a scheduler class to run callbacks functions asynchronously on a time schedule (millisecond precision). It uses Timer 0 comparator A interrupts (it does not interfere with millis() nor the Timer0 clock interrupt). I hope Comparator A interrupt is usually not used since it's tied to the system clock.

A simple scheduler like that might be a cleaner way for people to implement doing "multiple things at the same time".

Library is here, if you appreciate the effort, please give it a star on github to motivate me:



If you have ideas, opinions, critiques etc. please let me know on github or here.


Code: [Select]

#include <DigitalIO.hpp>

// Encoder is wired to pins 3 4 and 5, with interrupt (on pin 3).
// It returns values [-16,16] only.
// It uses encoderData to store its state (don't read it)
volatile int32_t encoderData = 0;
digitalRotaryEncoder<5,4,3, -16,16, true, encoderData> encoder;

// We have a LED on pin 13, turned off when LOW
digitalIo<13, LOW> led;

void setup() {

void loop() {
  // If the encoder switch is pressed down display the value
  // We also turn on the LED
  case 1:
    // We can read the value and not worry about managing the encoder mechanical movement
    // that's handled by the interrupt handler. Otherwise we'd have to call this function every 5~10ms
    // in the main body of the loop to not skip a beat.
    const int32_t val = encoder.rotaryRead();
    Serial.print("Button pressed, position: ");
  case -1:
  case 0:


With over 200 posts you should know better.

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you take a few moments to Learn How To Use The Forum.
It will help you get the best out of the forum in the future.
Other general help and troubleshooting advice can be found here.
It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google (who would have thunk it ! ) or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

Go Up