Code Distance Sensor

#include "FastLED.h"

#include "Adafruit_VL53L1X.h"

 

#define NUM_LEDS 24

#define DATA_PIN 8                 //digital Pin for LED-Data

#define potiPin A0

 

#define MAX_POWER_MILLIAMPS 5000

CRGB leds[NUM_LEDS];

 

// address we will assign in case more than two sensors are present

#define LOX_ADDRESS 0x29

 

//const uint8_t IRQ = 2;       //GPIO Pin on VL53L1X (ClockPin)

//const uint8_t XSHUT = 3;

 

 

const uint8_t readingsAmount = 25;  //for the Array, how many values to store

 

float brightness;

const uint8_t hue = 15;       //Hue-Range: 0=gelbrosa, 31= gelbgrün, 63=grün, 94=blaugrün, 127=(grün)blau, 158=blaublaub, 190=lilablau, 221=lila(blaurosa), 255=gelbrosa

int16_t distance;

int movingAverage;

int farestDistance = 2000;    // Threshold for a reaction to an Object

int closestDistance = 50;    // Threshold for a reaction to an Object

 

 

int readings1[readingsAmount];

 

 

Adafruit_VL53L1X lox = Adafruit_VL53L1X(); //in case needed, add (XSHUT1, IRQ1) to the brackets

 

 

 

 

void setup() {

  Serial.begin(115200);

  pinMode(potiPin, OUTPUT);

 

  // Initialise the LedStrip

  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);

  FastLED.setMaxPowerInVoltsAndMilliamps( 5, MAX_POWER_MILLIAMPS);

  Serial.print("FastLED Stripe initialazed..");

 

  // wait until serial port opens for native USB devices

  while (! Serial) {

    delay(10);

  }

  Serial.println("Booting VL53L1X...");

  if (!lox.begin(LOX_ADDRESS)) {

    Serial.println("Failed to boot VL53L1X_Sensor1");

    while (true) {

      delay(10);

    };

  }

 

  Serial.println("VL53L1X sensor OK!"); //Sensor 1

  Serial.print("Sensor ID: 0x");

  Serial.println(lox.sensorID(), HEX);

 

  if (!lox.startRanging()) {

    Serial.print("Couldn't start ranging: ");

    Serial.println(lox.vl_status);

    while (true) {

      delay(10);

    }

  }

 

 

  Serial.println("Ranging started");

 

  // Valid timing budgets: 15, 20, 33, 50, 100, 200 and 500ms!

  lox.setTimingBudget(100);

  Serial.print("Timing budget lox (ms): ");

  Serial.println(lox.getTimingBudget());

}

 

 

void loop() {

 

  // Sensortesting //

  if (lox.dataReady()) {

    // new measurement for the taking!

    distance = lox.distance();

    if (distance == -1) {

      // something went wrong!

      Serial.print("Couldn't get distance: ");

      Serial.println(lox.vl_status);

      return;

    }

  }

 

  int threshold = analogRead(potiPin);

  int distThreshold = map(threshold, 0, 100, 0, 4000);

 

 

 

  movingAverage = calculateMovingAverage(distance, readings1, readingsAmount);  // berechne einen Durchschnitt aus der jeweils tiefsten Distanz; distanceLowest !

 

  // DISTANZ ZUM SENSOR //

  // If distance to the sensor is more than 1500mm brightness low (50) and if distance to the sensor is less than 500 mm brightness high 255

  float constrDistance = constrain(movingAverage, closestDistance, farestDistance);         

  float mappedIncrements = map(constrDistance, closestDistance, farestDistance, 20, 200);  

  float mappedBrightness = map(mappedIncrements, 20, 200, 200, 20);                        // make sure you never go below 50 in brightness if you have a color. It looks wired if you do so because it is rgb

  Serial.print ("\t Distance: ");

  Serial.print (distance);

  Serial.print ("\t farestDistance: ");

  Serial.print (farestDistance);

  Serial.print ("\t poti: ");

  Serial.print (threshold);

  Serial.print ("\t MovingAverage: ");

  Serial.print (movingAverage);

  Serial.print ("\t MappedBrightness: ");

  Serial.println (mappedBrightness);

 

 

  // smooth transistions in the middle sectoin /////////

  // beeing close enough = high brightness (255)

  if (movingAverage < closestDistance) {

    for (uint16_t i = 0; i < NUM_LEDS; i++) {

      leds[i] = CHSV(hue, 100, 200);

    }

    FastLED.show();

    delay(1);

  }

 

  //Transitions (distance sets the brightnes)

  else if (movingAverage >= closestDistance && movingAverage <= farestDistance) {

    for (uint16_t i = 0; i < NUM_LEDS; i++) { //Eilibeili 1

      leds[i] = CHSV(hue, 100, mappedBrightness);

    }

    FastLED.show();

    delay(1);

  }

 

  //beeing far enough = low brightness (0)

  else if (movingAverage > farestDistance) {

    for (uint16_t i = 0; i < NUM_LEDS; i++) {

      leds[i] = CHSV(hue, 100, 20);

    }

 

    FastLED.show();

    delay(1);

  }

 

}

 

 

int calculateMovingAverage(int reading, int values[], int valuesSize) {

  // put in the new reading & throw out the oldest one

  for (int i = 0; i < valuesSize - 1; i++) {

    values[i] = values[i + 1];

  }

  values[valuesSize - 1] = reading;

 

  // sum up all readings & return the average

  int average = 0;

  for (int i = 0; i < valuesSize; i++) {

    average += values[i] / valuesSize;

  }

  return average;

 

}