Skip to content

Instantly share code, notes, and snippets.

@Kodekat84
Created November 27, 2018 16:14
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 Kodekat84/ecd0cede0b4d6dea519a3fa42c90f7b4 to your computer and use it in GitHub Desktop.
Save Kodekat84/ecd0cede0b4d6dea519a3fa42c90f7b4 to your computer and use it in GitHub Desktop.
/*
NFL ARROWS WIP CODE 2018
Arduino Mega
FastLED 3.1.6
Ardunio 1.8.5
_____ _ _ _
/ ____| | | | | | | _
| | _ __ ___ __ _| |_ ___ __| | | |__ _ _(_)
| | | '__/ _ \/ _` | __/ _ \/ _` | | '_ \| | | |
| |____| | | __/ (_| | || __/ (_| | | |_) | |_| |_
____\_____|_| \___|\__,_|\__\___|\__,_| |_.__/_\__, (_) _
|__ __| / ____| | | | |__/ | | |
| | ___ _ __ ___ | (___ ___| |__ _ _| |___/ ___ _ __| |_
| |/ _ \| '_ ` _ \ \___ \ / __| '_ \| | | | '_ \ / _ \ '__| __|
| | (_) | | | | | | ____) | (__| | | | |_| | |_) | __/ | | |_
|_|\___/|_| |_| |_| |_____/ \___|_| |_|\__,_|_.__/ \___|_| \__|
AND THE HELP OF MANY OTHERS, INCLUDING:
Mark Kriegsman FastLED
Rob Tillaart Running average
Chemdoc77 CRGBSet Examples
Peter Jansen TFMini Examples
*/
//#define FASTLED_ALLOW_INTERRUPTS 0
#include "TFMini.h"
#include "RunningAverage.h"
#define SensorOne Serial1
#define SensorTwo Serial2
#define SensorThree Serial3
#include <FastLED.h>
//FAST LED
#define NUM_LEDS 498 // Total LEDs in EACH Arrow
#define DATA_PIN 4 // Arrow ONE data
#define CLK_PIN 6 // Arrow ONE clock
#define DATA_PINTWO 5 // Arrow Two data
#define CLK_PINTWO 7 // Arrow Two clock
//#define BRIGHTNESS 255
#define LED_TYPE APA102 //Type
#define COLOR_ORDER BGR
#define MaxDistance 1500
int triggerValue = 300; // Distance to trigger the arrow random hold (cm)
uint8_t BRIGHTNESS = 100; // DEFAULT STROBE BRITGHTNESS - Adjustable once we start up
// ****************************LED ARRAY SET UP**************************** //
CRGB rawleds[NUM_LEDS];
CRGB rawledsTWO[NUM_LEDS];
CRGBSet leds(rawleds, NUM_LEDS);
CRGBSet ledsTWO(rawledsTWO, NUM_LEDS);
CRGBSet leds1(leds(0, 126)); //127
CRGBSet leds2(leds(127, 293)); //167
CRGBSet leds3(leds(294, 498)); //204
CRGBSet leds1B(ledsTWO(0, 126)); //127
CRGBSet leds2B(ledsTWO(127, 293)); //167
CRGBSet leds3B(ledsTWO(294, 498)); //204
struct CRGB * ledarray[] = {leds1, leds2, leds3};
struct CRGB * ledarrayTWO[] = {leds1B, leds2B, leds3B};
// ****************************LIDAR SENSOR SET UP**************************** //
TFMini tfmini1;
TFMini tfmini2;
TFMini tfmini3;
uint16_t distA = 1200;
uint16_t distB = 1200;
uint16_t distC = 1200;
uint16_t distMin = 1200;
uint16_t distMinInside = 1200;
/**************FOR STROBE**************/
int strobeIntervalMs = 100; // controls the interval between strobe flashes, CHANGES BASED ON SENSOR DISTANCE
CRGB strobeColor1 = CRGB::White; // the "on" color of the strobe1
CRGB strobeColor2 = CRGB::White; // the "on" color of the strobe2
CRGB strobeColor3 = CRGB::White; // the "on" color of the strobe3
/*************Running average initialization*****************/
RunningAverage myRA(10);
int samples = 0;
/*************Trying to remove the delay() || Have not implemented YET********************/
unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop()
unsigned long previousOnBoardLedMillis = 0; // will store last time the LED was updated
void setup() {
FastLED.addLeds<LED_TYPE, DATA_PIN, CLK_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); //ARROW ONE (LEFT) LEDS
FastLED.addLeds<LED_TYPE, DATA_PINTWO, CLK_PINTWO, COLOR_ORDER>(ledsTWO, NUM_LEDS).setCorrection(TypicalLEDStrip); //ARROW TWO (RIGHT) LEDS
FastLED.setBrightness(BRIGHTNESS);
// Step 1: Initialize hardware serial port (serial debug port)
Serial.begin(115200);
FastLED.clear(true);
delay(500);
FastLED.showColor(CRGB::Red);
Serial.println("RED");
delay(500);
FastLED.showColor(CRGB::Lime);
Serial.println("GREEN");
delay(500);
FastLED.showColor(CRGB::Blue);
Serial.println("BLUE");
delay(500);
Serial.println("ThreeTFMinisWArrows");
FastLED.clear(true);
// Step 2: Initialize the data rate for the SoftwareSerial port
SensorOne.begin(TFMINI_BAUDRATE);
SensorTwo.begin(TFMINI_BAUDRATE);
SensorThree.begin(TFMINI_BAUDRATE);
// Step 3: Initialize the TF Mini sensor
tfmini1.begin(&SensorOne);
tfmini2.begin(&SensorTwo);
tfmini3.begin(&SensorThree);
// Step 4: Set all three sensors to DEFAULT data mode
Serial1.write(0x42);
Serial1.write(0x57);
Serial1.write(0x02);
Serial1.write(0x00);
Serial1.write(0x00);
Serial1.write(0x00);
Serial1.write(0x01);
Serial1.write(0x06);
Serial2.write(0x42);
Serial2.write(0x57);
Serial2.write(0x02);
Serial2.write(0x00);
Serial2.write(0x00);
Serial2.write(0x00);
Serial2.write(0x01);
Serial2.write(0x06);
Serial3.write(0x42);
Serial3.write(0x57);
Serial3.write(0x02);
Serial3.write(0x00);
Serial3.write(0x00);
Serial3.write(0x00);
Serial3.write(0x01);
Serial3.write(0x06);
myRA.clear(); // explicitly start the Average clean
}
void loop() {
FastLED.setBrightness(BRIGHTNESS); // RESTORE BRIGHTNESS
getSensorData();
// printSensorData();
// checkForTrigger();
lightItUpStuff();
strobe();
FastLED.show();
}
void lightItUpStuff() { //Changes the timing and color of the arrow LED arrays based on the distance
if (distMin <= 500 && distMin >= 301) {
strobeIntervalMs = 101;
strobeColor1 = randomColor();
strobeColor2 = randomColor();
strobeColor3 = randomColor();
} else {}
if (distMin <= 700 && distMin >= 501 ) {
strobeIntervalMs = 200;
strobeColor1 = CRGB::Red;
strobeColor2 = CRGB::White;
strobeColor3 = CRGB::White;
} else {}
if (distMin <= 900 && distMin >= 701) {
strobeIntervalMs = 300;
strobeColor1 = CRGB::White;
strobeColor2 = CRGB::White;
strobeColor3 = CRGB::Yellow;
} else {}
if (distMin <= 1200 && distMin >= 901) {
strobeIntervalMs = 400;
strobeColor1 = CRGB::White;
strobeColor2 = CRGB::White;
strobeColor3 = CRGB::Blue;
} else {}
if (distMin >= 1201) {
strobeIntervalMs = 700;
strobeColor1 = CRGB::Black;
strobeColor2 = CRGB::Grey;
strobeColor3 = CRGB::Black;
} else {}
}
void strobe() {
if ((millis() / strobeIntervalMs) % 2) {
fill_solid(ledarray[0], 127, strobeColor1); //Inner loop
fill_solid(ledarray[1], 167, strobeColor2); //Middle loop
fill_solid(ledarray[2], 204, strobeColor3); //Outside loop
fill_solid(ledarrayTWO[0], 127, CRGB::Black); //Inner loop
fill_solid(ledarrayTWO[1], 167, CRGB::Black); //Middle loop
fill_solid(ledarrayTWO[2], 204, CRGB::Black); //Outside loop
} else {
fill_solid(ledarrayTWO[0], 127, strobeColor1); //Inner loop
fill_solid(ledarrayTWO[1], 167, strobeColor2); //Middle loop
fill_solid(ledarrayTWO[2], 204, strobeColor3); //Outside loop
fill_solid(ledarray[0], 127, CRGB::Black); //Inner loop
fill_solid(ledarray[1], 167, CRGB::Black); //Middle loop
fill_solid(ledarray[2], 204, CRGB::Black); //Outside loop
}
}
void printSensorData() {
Serial.print(" distA ");
Serial.print(distA);
Serial.print(" distB ");
Serial.print(distB);
Serial.print(" distC ");
Serial.print(distC);
Serial.print(" distMin ");
Serial.print(distMin);
Serial.print(" Strobe Speed ");
Serial.print(strobeIntervalMs);
Serial.print(" samples ");
Serial.print(samples);
Serial.print("\t Running Average: ");
Serial.println(myRA.getAverage(), 0);
}
void checkForTrigger() {
FastLED.clear(true);
int rando = random(1, 3);
if (rando == 1) {
// SET ARROW ONE (LEFT) CONSTANT ON
//FastLED.clear();
FastLED.setBrightness(254);
fill_solid(ledarray[0], 127, CRGB::Green); //Inner loop
fill_solid(ledarray[1], 167, CRGB::White); //Middle loop
fill_solid(ledarray[2], 204, CRGB::Green); //Outside loop
}
else {
// SET ARROW TWO (RIGHT) CONSTANT ON
//FastLED.clear();
FastLED.setBrightness(254);
fill_solid(ledarrayTWO[0], 127, CRGB::Green); //Inner loop
fill_solid(ledarrayTWO[1], 167, CRGB::White); //Middle loop
fill_solid(ledarrayTWO[2], 204, CRGB::Green); //Outside loop
}
FastLED.show();
myRA.clear();
myRA.fillValue(1250, 10); // Fill with higher value to avoid low loop
delay(10000); // NEED TO FIGURE OUT HOW TO ELIMINATE
FastLED.clear(true);
}
void getSensorData( ) {
// Take TF Mini distance measurement 1
distA = tfmini1.getDistance();
// uint16_t distA1 = tfmini1.getDistance();
// uint16_t distA2 = tfmini1.getDistance();
// uint16_t distA3 = tfmini1.getDistance();
// uint16_t distA4 = tfmini1.getDistance();
// uint16_t distA5 = tfmini1.getDistance();
// distA = ((distA1 + distA2 + distA3 + distA4 + distA5) / 5); //ARCHAIC PRE AVERAGE - MAY REMOVE BECAUSE WE'RE NOW UTILIZING A RUNNING AVERAGE
distA = constrain(distA, 0, MaxDistance);
// Take TF Mini distance measurement 2
distB = tfmini2.getDistance();
// uint16_t distB1 = tfmini2.getDistance();
// uint16_t distB2 = tfmini2.getDistance();
// uint16_t distB3 = tfmini2.getDistance();
// uint16_t distB4 = tfmini2.getDistance();
// uint16_t distB5 = tfmini2.getDistance();
// distB = ((distB1 + distB2 + distB3 + distB4 + distB5) / 5); //ARCHAIC PRE AVERAGE - MAY REMOVE BECAUSE WE'RE NOW UTILIZING A RUNNING AVERAGE
distB = constrain(distB, 0, MaxDistance);
// Take TF Mini distance measurement 3
distC = tfmini3.getDistance();
// uint16_t distC1 = tfmini3.getDistance();
// uint16_t distC2 = tfmini3.getDistance();
// uint16_t distC3 = tfmini3.getDistance();
// uint16_t distC4 = tfmini3.getDistance();
// uint16_t distC5 = tfmini3.getDistance();
// distC = ((distC1 + distC2 + distC3 + distC4 + distC5) / 5); //ARCHAIC PRE AVERAGE - MAY REMOVE BECAUSE WE'RE NOW UTILIZING A RUNNING AVERAGE
distC = constrain(distC, 0, MaxDistance);
distMinInside = min ( distA, min(distB, distC) ) ; //GET MINIMUM VALUE (CLOSEST DISTANCE) OF ALL THREE SENSORS
/*******************************AVERAGE CODE******************************/
myRA.addValue(distMinInside);
distMin = myRA.getAverage();
if (distMinInside <= triggerValue)
{
distMinInside = triggerValue + 1;
checkForTrigger();
} else {}
samples++;
/*******************************CLEAR SAMPLES SO IT DOESN'T BECOME TOO LARGE******************************/
if (samples == 300)
{
samples = 0;
// myRA.clear();
} else {}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
CRGB Wheel(byte WheelPos) {
if (WheelPos < 85) {
return CRGB(WheelPos * 3, 255 - WheelPos * 3, 0);
}
else if (WheelPos < 170) {
WheelPos -= 85;
return CRGB(255 - WheelPos * 3, 0, WheelPos * 3);
}
else {
WheelPos -= 170;
return CRGB(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
CRGB randomColor() {
return Wheel(random(256));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment