Skip to content

MrDev-5000/PhotonCore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

23 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ”… PhotonCore πŸ”†

PhotonCore is an powerful, non-blocking LED control library for Arduino.


πŸš€ Features

  • Non-Blocking, No delay(), for best code responsiveness
  • Simple and intuitive API
  • Supports Floating Point durations for ease of use and percise timing
  • Support for multiple time units (MINUTE, SECOND, MILLISECOND, MICROSECOND)

πŸ“¦ Dependencies

  • TimerKernel - Make sure to install this library before using PhotonCore.

βš™οΈ Installation

To install this library:

  1. Download ZIP
    Click the green Code button on this repository page, then select "Download ZIP".

  2. Open Arduino IDE
    Launch the Arduino IDE on your computer.

  3. Install ZIP Library

    • Go to Sketch > Include Library > Add .ZIP Library...
    • Navigate to where you downloaded the ZIP file.
    • Select the ZIP file and click Open.
  4. Done!
    The library is now installed and ready to use. You can find examples for this library in File > Examples > TimerKernel.

Note:

  • Do not unzip the downloaded file before installing.
  • If you are updating an existing library, delete the old version from your libraries folder first.

πŸ› οΈ Usage

Traditionally, to blink an LED we use delay() and digitalWrite():

#define myLed 9

void setup() {
  pinMode(myLed, OUTPUT);
}

void loop() {
  digitalWrite(myLed, HIGH);
  delay(1000);
  digitalWrite(myled, LOW);
  delay(1000)
}

However, this approach is not optimal, especially if you want the code to perform other tasks while blinking the LED. To fix this, you would typically usemillis() and digitalWrite() Instead:

#define myLed 9

unsigned long previousMillis = 0;
unsigned long interval = 1000;
bool state = false;

void setup() {
  pinMode(myLed, OUTPUT);
}

void loop() {
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    previousMillis = millis();

    state = !state;
  }
  digitalWrite(myLed, state);
}

However, this approach can be confusing and cumbersome to manage β€” especially for beginners. With PhotonCore the same functionality can be achieved in a cleaner and more intuitive way:

#include <PhotonCore.h>

constexpr myLedPin = 9;

PhotonCore myLed;

void setup() {
  myLed.setPin(myLedPin);
}

void loop() {
  myled.blink(1000);
}

πŸ“– Examples

  • simple_blink - Demonstrates how to blink an LED with regular time intervals.
  • advanced_blink - Demonstrates how to blink an LED with different on/off intervals.

πŸ“š API Reference

PhotonCore()

PhotonCore(int resolution = 255);

Creates a new PhotonCore object and initializes it.

Parameter Type Description
resolution int PWM resolution (default 255)

Returns:

  • Nothing.

setPin()

  void setPin();

Sets the LED pin.

Parameter Type Description
led int Pin number to set

Returns:

  • Nothing.

turnOn()

  void turnOn(int brightness = maxBrightness);

Turns LED on at full brightness by default. if a brightness value is provided, the LED will be set to that brightness level

Parameter Type Description
brightness int brightness of led (0 to maxResolution)

Returns:

  • Nothing.

turnOff()

  void turnOff();

Turns the LED off.

Returns:

  • Nothing.

blink()

  void blink(double duration, TimeUnit unit = MILLISECOND);

Blinks the Led ON and OFF with regular ON/OFF intervals.

Parameter Type Description
duration double Time interval in desired unit
unit enum Time unit (MINUTE, SECOND, MILLISECOND, MICROSECOND)

Returns:

  • Nothing.

blink() - Overload

  void blink(double onDuration, double offDuration, TimeUnit onUnit = MILLISECOND, TimeUnit offUnit = MILLISECOND);

Blinks the led with different ON/OFF intervals.

Parameter Type Description
onDuration double Duration the led stays ON in desired unit
offDuration double Duration the led stays OFF in desired unit
onUnit enum Time unit for onDuration (MINUTE, SECOND, MILLISECOND, MICROSECOND)
offUnit enum Time unit for offDuration (MINUTE, SECOND, MILLISECOND, MICROSECOND)

Returns:

  • Nothing.

fadeOn()

  void fadeOn(double duration, TimeUnit unit = MILLISECOND);

Fades the LED ON over the given duration.

Parameter Type Description
duration double Fade ON Duration in desired unit
unit enum Time unit (MINUTE, SECOND, MILLISECOND, MICROSECOND)

Returns:

  • Nothing.

fadeOff()

  void fadeOff(double duration, TimeUnit unit = MILLISECOND);

Fades the LED OFF over the given duration.

Parameter Type Description
duration double Fade OFF Duration in desired unit
unit enum Time unit (MINUTE, SECOND, MILLISECOND, MICROSECOND)

Returns:

  • Nothing.

fade()

  void fade(double duration, TimeUnit unit = MILLISECOND);

Fades the led ON and OFF with regular durations.

Parameter Type Description
duration double Duration for each fade ON/OFF cycle in desired unit
unit enum Time unit (MINUTE, SECOND, MILLISECOND, MICROSECOND)

Returns:

  • Nothing.

fade() - Overload

  void fade(double onDuration, double offDuration, TimeUnit onUnit = MILLISECOND, TimeUnit offUnit = MILLISECOND);

fades led ON and OFF with different durations.

Parameter Type Description
onDuration double Duration to fade ON led in desired unit
offDuration double Duration to fade OFF led in desired unit
onUnit enum Time unit for onDuration (MINUTE, SECOND, MILLISECOND, MICROSECOND)
offUnit enum Time unit for offDuration (MINUTE, SECOND, MILLISECOND, MICROSECOND)

Returns:

  • Nothing.

isOn()

  bool isOn(int brightness = 1);

Checks whether the LED is currently OFF. By default, it returns true if the LED brightness is greater than 0. If a brightness value is provided, it returns true if the LED’s brightness is greater than or equal to the specified value.

Parameter Type Description
brightness int brightness threshold

Returns:

  • true if LED is ON (depending on the condition), else false

isOff()

  bool isOff();

checks if the LED is currently OFF.

Returns:

  • true if OFF, otherwise false

Note:

  • By Default, all durations are interpreted in millisecond

πŸ“œ License

About

This is a powerful Arduino library to create smooth, non-blocking LED effects

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages