Skip to content

Commit

Permalink
Added another example sketch and updated the readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
EasyG0ing1 committed Jun 25, 2024
1 parent aa93f32 commit 14a8219
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 9 deletions.
8 changes: 1 addition & 7 deletions .idea/BlockNot.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,11 @@ There are more methods that allow you to affect change on your timers after inst
# Examples
There are currently eight examples in the library.
There are currently nine examples in the library.
### Advanced Auto Flashers
This sketch was one I wrote recently that was for a friend who wanted to put large LEDs on the back of his 5th wheel so that people behind him received much better feedback depending on whether he hits his brakes, uses his turn signals or uses the hazard lights. It's a fairly good example of using BlockNot timers to achieve compartmentalized functions in code that runs continuously and never stops. The code was written for a Raspberry Pi Pico.
### BlockNot Blink
Expand Down Expand Up @@ -836,6 +840,9 @@ simply make sure that only one thread will ever be causing changes to happen in
# Version Update Notes
### 2.1.5
- Added the Advanced Auto Flashers example and updated the README.
### 2.1.4
- Minor code changes per PR #19.
Expand Down
210 changes: 210 additions & 0 deletions examples/AdvancedAutoFlashers/AdvancedAutoFlashers.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
#include <Arduino.h>
#include <BlockNot.h>

/*
* This sketch was created for an LED layout that was installed onto the back of a 5th wheel.
* It was written for a Raspberry Pi Pico
*
* It looks for four different states that a driver could be doing with the automobile blinkers.
* It looks for:
* 1) Left flasher flashing
* 2) Right flasher flashing
* 3) Both flashers flashing at the same time (hazard lights)
* 4) Brakes are being applied
*
* When the flashers are engaged, it lights up LEDs that are arranged in a pattern. See the GIF images to see
* how it lights up the various LEDs on the back of the 5th wheel.
*
* BlockNot has been used as the only means of getting the timing right for the various LED animations.
*
* This code shows you how to write a sketch where the execution of the code never stops, yet it provides
* distinct functionality depending on the statae of the flashers.
*
* The input voltage from the blinkers will flash at some unknown frequency. We simply set a boolean high
* when the pin reads at a positive voltage. When it sees that positive voltage, it resets the timeDelay
* timer so that if there are no high values read on those pins for the duration of that timer, it
* then sets the state that the turn signals are not being used, and we use a firstTrigger() for that
* because we only need it to hit one time after a reset.
*
*/

enum State {
LEFT_BLINK,
RIGHT_BLINK,
ALL_OFF,
HAZARD
};

State state = ALL_OFF;

u_int leftBlinkIn = 2;
u_int rightBlinkIn = 3;
u_int brakeIn = 4;

u_int right1 = 9;
u_int right2 = 10;
u_int right3 = 11;
u_int right4 = 12;
u_int right5 = 13;
u_int cLight = 14;

u_int left1 = 17;
u_int left2 = 18;
u_int left3 = 19;
u_int left4 = 20;
u_int left5 = 21;

u_int brakeHazard = 16;

bool brakeOn = false;

u_int rightArrow[7] = {0, right1, right2, right3, right4, right5, cLight};
u_int leftArrow[7] = {0, left1, left2, left3, left4, left5, cLight};


void getState() {
static bool rightOn = false;
static bool leftOn = false;
static BlockNot timeDelay(3, SECONDS);

if (gpio_get(rightBlinkIn) == HIGH) {
sleep_ms(100);
rightOn = gpio_get(rightBlinkIn) == HIGH;
timeDelay.RESET;
}
if (gpio_get(leftBlinkIn) == HIGH) {
sleep_ms(100);
leftOn = gpio_get(leftBlinkIn) == HIGH;
timeDelay.RESET;
}
if (timeDelay.firstTrigger()) {
rightOn = false;
leftOn = false;
}
if (rightOn && leftOn) {
state = HAZARD;
}
else if (rightOn && !leftOn) {
state = RIGHT_BLINK;
}
else if (leftOn && !rightOn) {
state = LEFT_BLINK;
}
else {
state = ALL_OFF;
}
}


void arrowsOff() {
for (int x = 1; x <= 6; x++) {
gpio_put(rightArrow[x], LOW);
gpio_put(leftArrow[x], LOW);
}
if (!brakeOn) {
gpio_put(brakeHazard, LOW);
}
}

void arrowBlink() {
static BlockNot transition(40);
static BlockNot start(650);
static int index = 0;

if (state == HAZARD) {
index = 0;
return;
}
if ((state != LEFT_BLINK && state != RIGHT_BLINK) || state == ALL_OFF) {
arrowsOff();
index = 0;
return;
}
if (index == 0) {
if (start.TRIGGERED) {
index = 1;
transition.RESET;
}
}
if (index > 0) {
if (transition.TRIGGERED) {
if (index > 6) {
arrowsOff();
start.RESET;
index = 0;
return;
}

if (state == RIGHT_BLINK)
gpio_put(rightArrow[index], HIGH);

if (state == LEFT_BLINK)
gpio_put(leftArrow[index], HIGH);

index++;
}
}
}

void hazard() {
static BlockNot transition(40);
static BlockNot start(650);
static int index = 0;
if (state != HAZARD) {
return;
}

if (index == 0) {
if(start.TRIGGERED) {
index = 1;
}
}

if(index == 1) {
gpio_put(brakeHazard, HIGH);
transition.RESET;
}

if(index > 0) {
if(transition.TRIGGERED) {
if(index > 6) {
arrowsOff();
start.RESET;
index = 0;
return;
}
gpio_put(rightArrow[index], HIGH);
gpio_put(leftArrow[index], HIGH);
index++;
}
}
}

void brake() {
gpio_put(brakeHazard, gpio_get(brakeIn) == LOW ? HIGH : LOW);
brakeOn = gpio_get(brakeIn) == LOW;
}

void setup() {
for (int x = 9 ; x <= 14 ; x++) {
pinMode(x, OUTPUT);
}
for (int x = 16 ; x <= 21 ; x++) {
pinMode(x, OUTPUT);
}
pinMode(brakeHazard, OUTPUT);
pinMode(rightBlinkIn, INPUT);
pinMode(leftBlinkIn, INPUT);
pinMode(brakeIn, INPUT_PULLUP);
}

void loop() {
getState();
brake();
switch (state) {
case HAZARD: hazard();
case ALL_OFF: arrowsOff();
default: arrowBlink();
}
sleep_ms(1); // For good measure
}
Binary file added examples/AdvancedAutoFlashers/Brakes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/AdvancedAutoFlashers/Hazard.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/AdvancedAutoFlashers/Left.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/AdvancedAutoFlashers/Right.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=BlockNot
version=2.1.4
version=2.1.5
author=Michael Sims <[email protected]>
maintainer=Michael Sims <[email protected]>
sentence=BlockNot gives you non-blocking timers with simplicity.
Expand Down

0 comments on commit 14a8219

Please sign in to comment.