Skip to content

Instantly share code, notes, and snippets.

@Kodekat84
Created November 29, 2018 20:41
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/e141cbd306b51bf1913d487e10748a5b to your computer and use it in GitHub Desktop.
Save Kodekat84/e141cbd306b51bf1913d487e10748a5b 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 = 150; // DEFAULT STROBE BRITGHTNESS - Adjustable once we start up
int rando = 0; // Random number for arrow direction
// ****************************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(5);
int samples = 0;
/*************Trying to remove the delay() || Now Implemented********************/
bool TRIGGERstate = false;
unsigned long interval = 10000; // the time we need to wait
unsigned long previousMillis = 0; // millis() returns an unsigned long.
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(1000);
FastLED.showColor(CRGB::Red);
Serial.println("RED");
delay(1000);
FastLED.showColor(CRGB::Lime);
Serial.println("GREEN");
delay(1000);
FastLED.showColor(CRGB::Blue);
Serial.println("BLUE");
delay(1000);
Serial.println("ThreeTFMinisWArrowsNoDelayStatemachine");
FastLED.clear(true);
// Step 2: Initialize the data rate for the Serial 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() {
unsigned long currentMillis = millis(); // Grabs the current time
random16_add_entropy( random() ); // Make random a bit more random
FastLED.setBrightness(BRIGHTNESS); // RESTORE BRIGHTNESS
getSensorData(); // Polls the sensors
// printSensorData();
if (distMinInside <= triggerValue)
{
TriggerMe(); // Could elimintae function call but I have no need yet
} else {}
if (TRIGGERstate == true) {
TriggerArrowOn();
if ((unsigned long)(currentMillis - previousMillis) >= interval) {
TRIGGERstate = !TRIGGERstate;
rando = random(1, 3);
previousMillis = millis();
}
} else {
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 = 120;
strobeColor1 = randomColor();
strobeColor2 = randomColor();
strobeColor3 = randomColor();
} else {}
if (distMin <= 700 && distMin >= 501 ) {
strobeIntervalMs = 200;
strobeColor1 = CRGB::Red;
strobeColor2 = CRGB::Black;
strobeColor3 = CRGB::White;
} else {}
if (distMin <= 900 && distMin >= 701) {
strobeIntervalMs = 400;
strobeColor1 = CRGB::White;
strobeColor2 = CRGB::White;
strobeColor3 = CRGB::Yellow;
} else {}
if (distMin <= 1200 && distMin >= 901) {
strobeIntervalMs = 800;
strobeColor1 = CRGB::White;
strobeColor2 = CRGB::White;
strobeColor3 = CRGB::Blue;
} else {}
if (distMin >= 1201) {
strobeIntervalMs = 2000;
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 TriggerMe() {
//************************attempt at removing the delay************************//
TRIGGERstate = true;
//************************attempt at removing the delay************************//
}
void TriggerArrowOn() {
if (rando == 1) {
// SET ARROW ONE (LEFT) CONSTANT ON
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
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 {
// SET ARROW TWO (RIGHT) CONSTANT ON
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
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
}
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
}
void getSensorData( ) {
// Take TF Mini distance measurement 1
distA = tfmini1.getDistance();
distA = constrain(distA, 0, MaxDistance);
// Take TF Mini distance measurement 2
distB = tfmini2.getDistance();
distB = constrain(distB, 0, MaxDistance);
// Take TF Mini distance measurement 3
distC = tfmini3.getDistance();
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();
samples++;
/*******************************CLEAR SAMPLES SO IT DOESN'T BECOME TOO LARGE******************************/
if (samples == 300)
{
samples = 0;
// myRA.clear();
} else {}
}
CRGB randomColor() {
return CRGB(random(256), random(256), random(256));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment