Created
July 7, 2018 03:44
-
-
Save ayoussef127/6b4168860f3cb70f7b55cff55e3679a7 to your computer and use it in GitHub Desktop.
Working minimum reader + servo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <IntStatistics.h> | |
#include <Statistics.h> | |
Statistics stats (100); | |
#include <FastLED.h> | |
#include <Wire.h> | |
#define NUM_LEDS 200 | |
#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; | |
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); | |
} | |
if (x > 110) { | |
stats.reset(); | |
} | |
if (x < 43) | |
{ | |
lightingVal = minim; | |
} | |
if (lightingVal < 80) { | |
int gHue; | |
EVERY_N_MILLISECONDS (10) { | |
gHue++; | |
} | |
fill_rainbow(leds, NUM_LEDS, gHue, 5); | |
FastLED.show(); | |
} | |
if (lightingVal > 80) { | |
fill_solid(leds, NUM_LEDS, CRGB::Blue); | |
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