Skip to content

Commit

Permalink
Lots of changes
Browse files Browse the repository at this point in the history
Added header file. Renamed to .ino because PlatformIO will then work
with multiple files
(platformio/platformio-core#130)
  • Loading branch information
costyn committed Jun 15, 2017
1 parent e6c59b0 commit fdfa495
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 57 deletions.
Empty file removed src/LEDswarm-helper-routines.cpp
Empty file.
48 changes: 48 additions & 0 deletions src/LEDswarm-helper-routines.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <arduino.h>

void fadeall(uint8_t fade_all_speed) {
for (uint8_t i = 0; i < NUM_LEDS; i++) {
leds[i].nscale8(fade_all_speed);
}
}

void brightall(uint8_t bright_all_speed) {
for (uint8_t i = 0; i < NUM_LEDS; i++) {
leds[i] += leds[i].scale8(bright_all_speed) ;
}
}


void addGlitter( fract8 chanceOfGlitter)
{
for ( uint8_t i = 0 ; i < 5 ; i++ ) {
if ( random8() < chanceOfGlitter) {
leds[ random16(NUM_LEDS) ] += CRGB::White;
}
}
}



// Custom mod which always returns a positive number
int mod(int x, int m) {
return (x % m + m) % m;
}



uint32_t getMasterNodeId() {
uint32_t lowestId = UINT32_MAX ;
SimpleList<uint32_t>::iterator node = nodes.begin();
while ( node != nodes.end() ) {
if ( *node < lowestId ) {
lowestId = *node ;
}
node++;
}
return lowestId ;
}

bool thisNodeMaster() {
return mesh.getNodeId() == getMasterNodeId() ;
}
36 changes: 36 additions & 0 deletions src/LEDswarm-routines.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <arduino.h>

#ifdef RT_FADE_GLITTER
void fadeGlitter() {
addGlitter(90);
FastLED.setBrightness( maxBright ) ;
FastLED.show();
fadeToBlackBy(leds, NUM_LEDS, 200);
}
#endif


#ifdef RT_DISCO_GLITTER
void discoGlitter() {
fill_solid(leds, NUM_LEDS, CRGB::Black);
addGlitter(map( constrain( activityLevel(), 0, 3000), 0, 3000, 100, 255 ));
FastLED.setBrightness( maxBright ) ;
FastLED.show();
}
#endif


#ifdef RT_STROBE1
#define FLASHLENGTH 20
void strobe1() {
if ( tapTempo.beatProgress() > 0.95 ) {
fill_solid(leds, NUM_LEDS, CHSV( map( yprX, 0, 360, 0, 255 ), 255, 255)); // yaw for color
} else if ( tapTempo.beatProgress() > 0.80 and tapTempo.beatProgress() < 0.85 ) {
fill_solid(leds, NUM_LEDS, CRGB::White );
} else {
fill_solid(leds, NUM_LEDS, CRGB::Black); // black
}
FastLED.setBrightness( maxBright ) ;
FastLED.show();
}
#endif
10 changes: 10 additions & 0 deletions src/LEDswarm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// prototype methods:
void receivedCallback( uint32_t from, String &msg );
void newConnectionCallback(uint32_t nodeId) ;
void changedConnectionCallback() ;
void nodeTimeAdjustedCallback(int32_t offset) ;
void delayReceivedCallback(uint32_t from, int32_t delay) ;
void setTapTempo() ;
uint32_t getMasterNodeId() ;
bool thisNodeMaster() ;
void checkButtonPress() ;
92 changes: 35 additions & 57 deletions src/LEDswarm.cpp → src/LEDswarm.ino
Original file line number Diff line number Diff line change
@@ -1,41 +1,31 @@

// #define TIME_SYNC_INTERVAL 6000000 // Time resync period, in us. 600 sec = 10 min
#include "LEDswarm.h"
#include "painlessMesh.h"
#include "ArduinoTapTempo.h" // pio lib [--global] install https://github.com/dxinteractive/ArduinoTapTempo.git
#include "FastLED.h"


#ifdef INCLUDE_LEDS
#include "FastLED.h"
#endif
#define BUTTON_PIN 0

#define MESH_PREFIX "LEDforge.com LEDswarm"
#define MESH_PASSWORD "somethingSneaky"
#define MESH_PORT 5555
#define MESH_PREFIX "LEDforge.com LEDswarm"
#define MESH_PASSWORD "somethingSneaky"
#define MESH_PORT 5555

// #define TIME_SYNC_INTERVAL 6000000 // Time resync period, in us. 600 sec = 10 min

#define DEFAULT_BPM 120
#define DEFAULT_PATTERN 0

#ifdef INCLUDE_LEDS
#define NUM_LEDS 5
#define DATA_PIN 2
#define NUM_LEDS 30
#define DATA_PIN 5
CRGB leds[NUM_LEDS];
#endif

painlessMesh mesh;
SimpleList<uint32_t> nodes;
uint32_t sendMessageTime = 0; // how often to send broadcast messages
String role ;

#define BUTTON_PIN 0

// prototype methods:
void receivedCallback( uint32_t from, String &msg );
void newConnectionCallback(uint32_t nodeId) ;
void changedConnectionCallback() ;
void nodeTimeAdjustedCallback(int32_t offset) ;
void delayReceivedCallback(uint32_t from, int32_t delay) ;
void setTapTempo() ;


uint8_t currentPatternId = DEFAULT_PATTERN ;
uint32_t currentBeatLength = 60000 / DEFAULT_BPM ;
Expand All @@ -44,8 +34,7 @@ ArduinoTapTempo tapTempo;
uint32_t tapTimer = 0 ;
#define HOLD_TIME 1000000 // wait time before tapping in new BPM
uint32_t holdTimer = 0 ;
unsigned long buttonTimer = 0;
bool buttonActive = false;


void setup() {
Serial.begin(115200);
Expand All @@ -62,9 +51,7 @@ void setup() {

nodes.push_back( mesh.getNodeId() ) ;

#ifdef INCLUDE_LEDS
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
#endif

Serial.print("Starting up... I am: ");
Serial.println(mesh.getNodeId()) ;
Expand All @@ -73,44 +60,12 @@ void setup() {
}


uint32_t getMasterNodeId() {
uint32_t lowestId = UINT32_MAX ;
SimpleList<uint32_t>::iterator node = nodes.begin();
while ( node != nodes.end() ) {
if ( *node < lowestId ) {
lowestId = *node ;
}
node++;
}
return lowestId ;
}

bool thisNodeMaster() {
return mesh.getNodeId() == getMasterNodeId() ;
}

#define SHORT_PRESS_MIN_TIME 50 // minimum time for a short press - debounce

void loop() {
mesh.update();

if( digitalRead(BUTTON_PIN) == LOW ) {
if (buttonActive == false) {
buttonActive = true;
buttonTimer = millis();
}
} else {
if (buttonActive == true) {
buttonActive = false; // reset
if ( millis() - buttonTimer > SHORT_PRESS_MIN_TIME ) { // test if debounce is reached
tapTempo.update(true); // update ArduinoTapTempo
currentBeatLength = tapTempo.getBeatLength();
Serial.printf("%s %u: Button TAP. %u. BPM: ", role.c_str(), mesh.getNodeTime(), currentBeatLength );
Serial.println(tapTempo.getBPM() );
holdTimer = mesh.getNodeTime() + HOLD_TIME ;
}
}
}
checkButtonPress();

if( tapTimer < mesh.getNodeTime() and holdTimer < mesh.getNodeTime() ) {
tapTempo.update(true);
Expand Down Expand Up @@ -204,3 +159,26 @@ void nodeTimeAdjustedCallback(int32_t offset) {
void delayReceivedCallback(uint32_t from, int32_t delay) {
Serial.printf("Delay to node %u is %d us\n", from, delay);
}

void checkButtonPress() {
static unsigned long buttonTimer = 0;
static bool buttonActive = false;

if( digitalRead(BUTTON_PIN) == LOW ) {
if (buttonActive == false) {
buttonActive = true;
buttonTimer = millis();
}
} else {
if (buttonActive == true) {
buttonActive = false; // reset
if ( millis() - buttonTimer > SHORT_PRESS_MIN_TIME ) { // test if debounce is reached
tapTempo.update(true); // update ArduinoTapTempo
currentBeatLength = tapTempo.getBeatLength();
Serial.printf("%s %u: Button TAP. %u. BPM: ", role.c_str(), mesh.getNodeTime(), currentBeatLength );
Serial.println(tapTempo.getBPM() );
holdTimer = mesh.getNodeTime() + HOLD_TIME ;
}
}
}
}
26 changes: 26 additions & 0 deletions src/Notes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
== Startup Procedure ==
start up rainbow routine
connect to mesh
blink LED to indicate connection
sync time and BPM
elect MASTER with lowest ID
receive pattern from MASTER

== Patterns ==
=== All Nodes Same Pattern ===
* synchronized heartbeat,
* sync twirl/cylon
* disco glitter and fade glitter
* palette patterns

=== Multi-Node Patterns ===
* Strobe
** split mesh into 2, 3 or 4 groups;
* Alternate between nodes with cylon/VU meter
* Twirl by group standing in circle and each strand lights up sequentially
* Combine all LEDs strands into 1 long LED array for patterns like RT_THREE_SIN_PAL, RT_JUGGLE_PAL and RT_BOUNCEBLEND


== UI/UX ==
* Set BPM: short click
* Switch pattern: long click

0 comments on commit fdfa495

Please sign in to comment.