Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chemdoc77/d52550a01c5fd9aeb0dae2bc2bb493a2 to your computer and use it in GitHub Desktop.
Save chemdoc77/d52550a01c5fd9aeb0dae2bc2bb493a2 to your computer and use it in GitHub Desktop.
The following sketch contains various functions based on the CD77_Police_Lights_One functions, from my original sketch in GitHub, CD77_police_lights. It includes color change by button.
// Police_Lights_One with Button Color Control by Chemdoc77
// Includes new functions for various number of pixels that are on in a group and for forward and reverse directions along with the ability to change colors with a button.
// ******** Version 20190130 *************
/* This code is based on the police_lightsONE and police_lightsALL code:
in Funkboxing by Teldredge at:
http://funkboxing.com/wordpress/wp-content/_postfiles/sk_qLEDFX_POST.ino
This sketch is one function from the following sketch in GitHub at:
https://github.com/chemdoc77/CD77_FastLED/tree/master/CD77_police_lights
It uses the out of bounds concept and code from the FastLED XYMatrix Example at:
https://github.com/FastLED/FastLED/blob/master/examples/XYMatrix/XYMatrix.ino
It uses Erin St Blaine's button code from:
https://learn.adafruit.com/dotstar-belly-dance-fans/overview
*/
#include "FastLED.h"
#define NUM_LEDS 24
#define Data_Pin 6
#define chipset NEOPIXEL
#define BRIGHTNESS 40
int x = 0;
int xx = NUM_LEDS-1;
//BUTTON SETUP STUFF
#define BUTTON_PIN 14 // this is an analog pin for a Teensy 3.1. Use the appropiate analog pin for your MCU.
byte prevKeyState = HIGH;
#define NUM_MODES 1 // note: modes start at 0.
int landingMode = 0; //FIRST ACTIVE MODE
// From the FastLED XYMatrix Example out of bounds code:
CRGB leds_plus_safety_pixel[ NUM_LEDS + 4];
CRGB* const leds( leds_plus_safety_pixel + 4);
//===============================
void setup() {
delay(1000); // power-up safety delay
FastLED.addLeds<chipset, Data_Pin>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
FastLED.setMaxPowerInVoltsAndMilliamps(5,1500); // sets max amperage to 1500 milliamps (1.5 amps)
set_max_power_indicator_LED(13);
// button setup stuff
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop(){
switch (landingMode) {
case 0: cd77_police_lights_one_modified_n_dots(2, 4, 60, CRGB::Red); break;
case 1: cd77_police_lights_one_modified_n_dots(2, 4, 60, CRGB::Blue); break;
}
// button management section
byte currKeyState = digitalRead(BUTTON_PIN);
if ((prevKeyState == LOW) && (currKeyState == HIGH)) {
keyRelease();
}
prevKeyState = currKeyState;
}
//Sketch Functions ================================
void cd77_police_lights_one_modified( int segments, int wait,CRGB color){
EVERY_N_MILLIS(wait){
x++;
if (x >= NUM_LEDS) {x = 0;}
fill_solid(leds,NUM_LEDS, CRGB::Black);
for (int z=0; z< segments;z++){
int y = x + (z*NUM_LEDS)/segments;
if ((x + (z*NUM_LEDS)/segments) > NUM_LEDS-1) {y = (x + (z*NUM_LEDS)/segments)%NUM_LEDS; }
leds[y]= color;
}
FastLED.show();
}
}
//=======================
void cd77_police_lights_one_modified_2_dots( int segments, int wait,CRGB color){
EVERY_N_MILLIS(wait){
x++;
if (x >= NUM_LEDS) {x = 0;}
fill_solid(leds,NUM_LEDS, CRGB::Black);
for (int z=0; z< segments;z++){
int y = x + (z*NUM_LEDS)/segments;
if ((x + (z*NUM_LEDS)/segments) > NUM_LEDS-1) {y = (x + (z*NUM_LEDS)/segments)%NUM_LEDS; }
leds[y]= color;
leds[y-1]= color;
}
FastLED.show();
}
}
//======================
void cd77_police_lights_one_modified_revese( int segments, int wait,CRGB color){
EVERY_N_MILLIS(wait){
xx--;
if (xx <=0) {xx = NUM_LEDS-1;}
fill_solid(leds,NUM_LEDS, CRGB::Black);
for (int z=0; z< segments;z++){
int y = xx + (z*NUM_LEDS)/segments;
if ((xx + (z*NUM_LEDS)/segments) > NUM_LEDS-1) {y = (xx + (z*NUM_LEDS)/segments)%NUM_LEDS; }
leds[y]= color;
}
FastLED.show();
}
}
//==========================
void cd77_police_lights_one_modified_n_dots(int numb_dots, int segments, int wait,CRGB color){
EVERY_N_MILLIS(wait){
x++;
if (x >= NUM_LEDS) {x = 0;}
fill_solid(leds,NUM_LEDS, CRGB::Black);
for (int z=0; z< segments;z++){
int y = x + (z*NUM_LEDS)/segments;
if ((x + (z*NUM_LEDS)/segments) > NUM_LEDS-1) {y = (x + (z*NUM_LEDS)/segments)%NUM_LEDS; }
for (int dots=0; dots<numb_dots; dots++){
//leds[y]= color;
leds[y-dots]= color;
}
}
FastLED.show();
}
}
//====================
void cd77_police_lights_one_modified_n_dots_reverse(int numb_dots, int segments, int wait,CRGB color){
EVERY_N_MILLIS(wait){
xx--;
if (xx <=0) {xx = NUM_LEDS-1;}
fill_solid(leds,NUM_LEDS, CRGB::Black);
for (int z=0; z< segments;z++){
int y = xx + (z*NUM_LEDS)/segments;
if ((xx + (z*NUM_LEDS)/segments) > NUM_LEDS-1) {y = (xx + (z*NUM_LEDS)/segments)%NUM_LEDS; }
for (int dots=0; dots<numb_dots; dots++){
//leds[y]= color;
leds[y-dots]= color;
}
}
FastLED.show();
}
}
//BUTTON CONTROL STUFF
// called when button is pressed
void shortKeyPress() {
Serial.println("short");
landingMode++;
if (landingMode > NUM_MODES){
landingMode=0; }
}
// called when key goes from pressed to not pressed
void keyRelease() {
Serial.println("key release");
shortKeyPress();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment