Tonight I decided I would try to make a very simple library for Arduino. I call it FlashingLED and it works with one function, flash(int on, int off), where on and off are microsecond values fed into a more or less BlinkWithoutDelay style millis() timer. It works fine for one LED, but when try to use it on more then one, the global variable "timer" is not unique to each LED so they screw with each other's timings and get all wonky.
Here is the header file:
#ifndef FlashingLED_h
#define FlashingLED_h
#include "Arduino.h"
class FlashingLED
{
public:
FlashingLED(int pin);
void flash(int on, int off);
private:
int _pin;
};
#endif
And the source file:
#include "Arduino.h"
#include "FlashingLED.h"
FlashingLED::FlashingLED(int pin)
{
pinMode(pin, OUTPUT);
_pin = pin;
}
void FlashingLED::flash(int on, int off)
{
millis();
unsigned long timer = millis();
while (millis() - timer < on)
{
digitalWrite(_pin, HIGH);
}
timer = millis();
while (millis() - timer < off)
{
digitalWrite(_pin, LOW);
}
timer = millis();
}
As well as my example for one LED:
#include <FlashingLED.h>
FlashingLED myLED(13);
void setup()
{
}
void loop()
{
myLED.flash(1000, 1000);
}
As far as "what have you tried," I have played with various values for the "on" and "off" values to see if it just looked odd because of my original times, and that wasn't the case. I then thought that maybe the pins were cross-talking due to "_pin = pin" in the source file, but decided that this wasn't necessarily the problem (but I have no real clue). I then decided that my problem was probably the fact that my source file code uses a global variable "timer" for all pins included.
Any ideas on how to restrict the timer variable to each pin?
Is there any way to give each pin its own timer based on something like timer(x) where x could just be a variable based on the pin number to keep the timers separate from pin to pin?
I would like to add that I am very new to coding and Arduino in general, I just like to learn by doing if possible. With that in mind, please don't start slinging techy programmer lingo at me (I assume this exists).