Feedback on my first attempt to write a library for Arduino + troubleshooting


okay, decided try learning write libraries writing library "blink without delay" - routine, set pins use , on/off-times. work degree, when looking @ oscilloscope there timings off approx +10% , of course have no idea why. when attempting run several leds simultaneously on times seem shorter (maybe half) occationally, still seems work.

maybe of guys able see things i've overlooked or done wrong?

the header-file:

code: [select]

// blinks led without use of delay().

#ifndef blinkwithoutdelay_h
#define blinkwithoutdelay_h

#include "arduino.h"

class blinkwithoutdelay
{
public:
blinkwithoutdelay(int blinkpin);
void blink(int _offtime, int _ontime);

private:
int _blinkpin;
int _currenttime;
int _previoustime;
int _pinstate;
int _offtime;
int _ontime;
};
#endif


the source file:

code: [select]

// blinks led without use of delay().

#include "arduino.h"
#include "blinkwithoutdelay.h"


blinkwithoutdelay::blinkwithoutdelay(int blinkpin)
{
pinmode(blinkpin, output);
_blinkpin = blinkpin;
}

void blinkwithoutdelay::blink(int _offtime, int _ontime){
   _currenttime = millis();               //
 int  _pinstate = digitalread(_blinkpin);              // check state of pin, on or off
  if ((_pinstate == 0) && (_currenttime - _offtime > _previoustime)){ // if it's "off" check if has reached set "offtime", otherwise ignore.
    _previoustime = _currenttime; // reset timer
    _pinstate = 1; // change pinstate "on".
  }
  if ((_pinstate == 1) && (_currenttime - _ontime > _previoustime)){ // check if pin "on", check if has reached set "ontime", otherwise ignore
    _previoustime = _currenttime; // reset timer
    _pinstate = 0; // change pinstate "off".
  }
  digitalwrite(_blinkpin, _pinstate); // set pin according "pinstate".
}


a simple sketch blink 1 or more leds:

code: [select]

void setup(){
}

void loop(){
  blink(5,10, 11); // offtime, ontime, pin
}

unsigned int previoustime = 0;                       // use timer.

void blink(int offtime, int ontime, int blinkpin){
  pinmode(blinkpin, output);                         // set blinkpin output (should perhaps set outside of function?
  unsigned int currenttime = millis();               //
  int pinstate = digitalread(blinkpin);              // check state of pin, on or off
  if ((pinstate == 0) && (currenttime - offtime > previoustime)){ // if it's "off" check if has reached set "offtime", otherwise ignore.
    previoustime = currenttime; // reset timer
    pinstate = 1; // change pinstate "on".
  }
  if ((pinstate == 1) && (currenttime - ontime > previoustime)){ // check if pin "on", check if has reached set "ontime", otherwise ignore
    previoustime = currenttime; // reset timer
    pinstate = 0; // change pinstate "off".
  }
  digitalwrite(blinkpin, pinstate); // set pin according "pinstate".
}

code: [select]
(_currenttime - _offtime >= _previoustime)
you need pay more attention datatypes:
code: [select]
int _blinkpin;
int _currenttime;
int _previoustime;
int _pinstate;
int _offtime;
int _ontime;


Arduino Forum > Using Arduino > Programming Questions > Feedback on my first attempt to write a library for Arduino + troubleshooting


arduino

Comments

Popular posts from this blog

invalid use of void expresion in FlexiTimer2 library

error: a function-definition is not allowed here before '{' token

LED Strip Code