Ah thanks, and it is! I'd be really appreciative if you could explain the values that you chose for the significantChange and the oldValue.
Here is my code so far:
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#endif
#define echoPin 2 // Echo of HC-SR04
#define trigPin 3 //Trig of HC-SR04
#define PIN 6
#define NUMPIXELS 7
#define DELAYVAL 500 // Time (in milliseconds) to pause between pixels
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); //Determines the number of LEDs and Arduino pins.
const unsigned int significantChange = 10;
unsigned long savedMillis;
unsigned long currentMillis;
const unsigned long period = 3000; //period of time for brightness to set
long duration; // variable for the duration of sound wave travel
int oldValue = -100;
int distance; // variable for the distance measurement
int prevDistance; // variable for the previous distance
int brightness; //variable for the brightness measurement
int lastBrightness; // variable for smoother brightness transition
int counter = 0; //counter for the colour modes
const int buttonPin = 5;
//===================================================//
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
pinMode(buttonPin, INPUT);
pixels.begin();//Does the initialisations.
Serial.begin(9600);
}
//===================================================//
void loop() {
int buttonState;
buttonState = digitalRead(buttonPin);
digitalWrite(trigPin, LOW); // Clears the trigPin condition
delayMicroseconds(2);
digitalWrite(trigPin, HIGH); // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
delay(100);
pixels.clear();
//setting the brightness etc
if (distance <= 50) {
brightness = map(distance, 0, 50, 1, 255);
brightness = (brightness + lastBrightness) / 2;
lastBrightness = brightness;
int currentValue = analogRead(2);
if (abs(currentValue - oldValue) >= significantChange) {
savedMillis = millis();
}
currentMillis = millis();
if (currentMillis - savedMillis >> period){
pixels.setBrightness(brightness);
}
}
else {
brightness = (brightness - lastBrightness) / 2;
if (brightness < 0)
brightness = 0;
lastBrightness = brightness;
}
pixels.setBrightness(brightness);
if (buttonState == LOW) {
counter++;
delay(150);
}
else if (counter == 0) {
pixels.setPixelColor(0, pixels.Color(253, 244, 200));//Defines the LEDs color with the RGB system, after specifying the LED number (from 0 to NUMPIXELS-1).
pixels.setPixelColor(1, pixels.Color(253, 244, 200));
pixels.setPixelColor(2, pixels.Color(253, 244, 200));
pixels.setPixelColor(3, pixels.Color(253, 244, 200));
pixels.setPixelColor(4, pixels.Color(253, 244, 200));
pixels.setPixelColor(5, pixels.Color(253, 244, 200));
pixels.setPixelColor(6, pixels.Color(253, 244, 200));
}
else if (counter == 1) {
pixels.setPixelColor(0, pixels.Color(255, 0, 0));
pixels.setPixelColor(1, pixels.Color(255, 0, 0));
pixels.setPixelColor(2, pixels.Color(255, 0, 0));
pixels.setPixelColor(3, pixels.Color(255, 0, 0));
pixels.setPixelColor(4, pixels.Color(255, 0, 0));
pixels.setPixelColor(5, pixels.Color(255, 0, 0));
pixels.setPixelColor(6, pixels.Color(255, 0, 0));
}
else if (counter == 2) {
pixels.setPixelColor(0, pixels.Color(0, 255, 0));
pixels.setPixelColor(1, pixels.Color(0, 255, 0));
pixels.setPixelColor(2, pixels.Color(0, 255, 0));
pixels.setPixelColor(3, pixels.Color(0, 255, 0));
pixels.setPixelColor(4, pixels.Color(0, 255, 0));
pixels.setPixelColor(5, pixels.Color(0, 255, 0));
pixels.setPixelColor(6, pixels.Color(0, 255, 0));
}
else if (counter == 3) {
pixels.setPixelColor(0, pixels.Color(0, 0, 255));
pixels.setPixelColor(1, pixels.Color(0, 0, 255));
pixels.setPixelColor(2, pixels.Color(0, 0, 255));
pixels.setPixelColor(3, pixels.Color(0, 0, 255));
pixels.setPixelColor(4, pixels.Color(0, 0, 255));
pixels.setPixelColor(5, pixels.Color(0, 0, 255));
pixels.setPixelColor(6, pixels.Color(0, 0, 255));
}
else {
counter = 0;
}
pixels.show(); //Displays the applied values.
Serial.print(" Counter: ");
Serial.println(counter);
Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" cm ");
Serial.print("Brightness: ");
Serial.println(brightness);
delay(200);
}
unfortunately I still haven't gotten the set brightness part to work - keen to make it and learn why!