Skip to content

Instantly share code, notes, and snippets.

@ayoussef127
Created July 20, 2018 17:16
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 ayoussef127/82e90ab4e424849ea825a8d4d32d8881 to your computer and use it in GitHub Desktop.
Save ayoussef127/82e90ab4e424849ea825a8d4d32d8881 to your computer and use it in GitHub Desktop.
minimum distance reader - glitching
#include <IntStatistics.h>
#include <Statistics.h>
Statistics stats (100);
#include <FastLED.h>
#include <Wire.h>
#define NUM_LEDS 100
#define datapin 6
#define LED_TYPE WS2811
CRGB leds[NUM_LEDS];
const int trigPin = 10;
const int echoPin = 11;
// defines variables
long duration;
int distance;
int oldValue;
int lightingVal;
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600);
FastLED.addLeds<WS2811, datapin>(leds, NUM_LEDS);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
// Serial.begin(9600);
}
int x;
int minim;
uint8_t gHue;
int bpm;
int r;
int g;
void loop() {
//distance code
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
// Prints the distance on the Serial Monitor
//Serial.print("Distance: ");
//Serial.println(distance);
// Minimum distance detection code
// main code
{
stats.addData(distance);
//Serial.print("Minimum: ");
//Serial.println(stats.minVal());
minim = (stats.minVal());
//Serial.println(minim);
}
// COLLECT AND CALCULATE MIN DATA PER SWEEP
if (x > 110) {
stats.reset();
}
if (x < 43)
{
lightingVal = minim;
}
// LIGHTING STATES
if (lightingVal < 50) {
for (int j = 0; j < NUM_LEDS; j++) leds[j].fadeToBlackBy(8);
FastLED.show();
}
if (lightingVal >= 50 && lightingVal < 80) {
r = beatsin8(35, 0, 255);
fill_solid(leds, NUM_LEDS, CRGB(r, 0, 0));
FastLED.show();
}
if (lightingVal >= 80 && lightingVal < 150) {
int sHue = beatsin8(23, 0, 25);
int eHue = beatsin8(25, 0, 25);
fill_gradient(leds, NUM_LEDS, CHSV(sHue, 255, 255), CHSV(eHue, 255, 255), SHORTEST_HUES);
FastLED.show();
}
if (lightingVal > 150) {
EVERY_N_MILLISECONDS (5) {
gHue++;
}
fill_rainbow(leds, NUM_LEDS, gHue, 5);
FastLED.show();
}
Serial.println(lightingVal);
}
void receiveEvent(int howMany) {
while (1 < Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
//Serial.print(c); // print the character
}
x = Wire.read(); // receive byte as an integer
// Serial.println(x); // print the integer
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment