If analogue value is constant for Xs then... How do I even start?

Hi all, I would like to create a condition where if an analog value from my sensor is the same for an amount of time, then it can set the brightness of the LED permanently at the analog value. Unfortunately, I do not have much experience in coding/Arduino and have no idea where to start! Could someone please tell me which functions to look into for this? My initial thoughts are somehow to incorporate an if statement and using Millis somehow but I'm not sure.

Thanks :slight_smile:

Each time through loop() read the analogue value. If it has not changed significantly since last time then do nothing. If it has changed significantly, save the value and save the value of millis() at that time

Separately in loop(), if the current value of millis() minus the saved value of millis() is greater than the required period then set the brightness of the LED

Sorry, I've been trying to wrap my head around this for the last hour!

How do I compare the analogue values of the same input? By creating a variable?

How do I save the value of millis() at the time? savedMillis = Current Millis?

const unsigned int significantChange = 10;
unsigned long lastChangeTime;
int oldValue = -100;

...


  int currentValue = analogRead(sensorPin);
  if (abs(currentValue - oldValue) >= significantChange) {
   ... 
 }
...
    lastChangeTime = millis();

(sounds like school work, so show us your attempt)

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!

oldValue was set to an impossible value so that it will trigger a significant change after the expected delay, whatever the current setting.

significantChange was just a random guess. Your analogRead() will be between 0 and 1023. A change of 10 leads to ~100 possible choices and should deal with most small changes that happen frequently when you use analogRead()

for your code, put this aside and write something simple to get the algorithm right. Once you get it, integrate with the rest of your code

1 Like

Thanks for the explanation!

Unfortunately I've still not gotten it to work :confused:
here is the code I tried:

#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.

long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement

const unsigned int significantChange = 10;
unsigned long lastChangeTime;
unsigned long currentMillis;
unsigned long savedMillis;
const unsigned long period = 2000;
int oldValue = -100;
int brightnessSet;

int brightness; //variable for the brightness measurement
int lastBrightness; // variable for the last brightness measurement

//===================================================//

void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
  pixels.begin();//Does the initialisations.

  Serial.begin(9600);
}

//===================================================//

void loop() {
  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(250);

  pixels.clear();   
  
  if (distance <= 50) {
    currentMillis = millis();
    brightness = map(distance, 0, 50, 1, 255);
    brightness = (brightness + lastBrightness) / 2;
    lastBrightness = brightness;

    int currentValue = analogRead(echoPin);
    if (abs(currentValue - oldValue) >= significantChange) {
      savedMillis = millis();
      if (currentMillis - savedMillis >> period){
        brightnessSet = brightness;
        pixels.setBrightness(brightnessSet);
      }
    }
    
  }
  else {
    brightness = (brightness - lastBrightness) / 2;
    if (brightness < 0)
      brightness = 0;
      lastBrightness = brightness;
  }
  pixels.setBrightness(brightness);

  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));

  pixels.show(); //Displays the applied values.

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.print(" cm ");
  Serial.print("Brightness: ");
  Serial.println(brightness);
}

I also tried this code of my own which also didn't work

#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.

long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement

int brightnessSet;
int lastDistance; //variable for the last distance measurement
int currentDistance;
unsigned long currentMillis;
unsigned long savedMillis;
const unsigned long sigChange = 10;
const unsigned long period = 2000;

int brightness; //variable for the brightness measurement
int lastBrightness; // variable for the last brightness measurement

//===================================================//

void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
  pixels.begin();//Does the initialisations.

  Serial.begin(9600);
}

//===================================================//

void loop() {
  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(250);

  pixels.clear();   
  
  if (distance <= 50) {
    currentMillis = millis();
    brightness = map(distance, 0, 50, 1, 255);
    brightness = (brightness + lastBrightness) / 2;
    lastBrightness = brightness;

    lastDistance = currentDistance; //loads last reading
    currentDistance = distance; 
    if (currentDistance - lastDistance <= sigChange){
      savedMillis = millis();
      if (currentMillis - savedMillis >> period){
        brightnessSet = brightness;
        pixels.setBrightness(brightnessSet);
      }
    }
    
  }
  else {
    brightness = (brightness - lastBrightness) / 2;
    if (brightness < 0)
      brightness = 0;
      lastBrightness = brightness;
  }
  pixels.setBrightness(brightness);

  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));

  pixels.show(); //Displays the applied values.

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.print(" cm ");
  Serial.print("Brightness: ");
  Serial.println(brightness);
}
      if (currentMillis - savedMillis >> period)

Is the >> deliberate in this line or should it be >= ?

this test

is done only if the distance is <= 50.

is that intended?

yes, I'd only like the brightness to be affected within 50 cm

It was but I'm wiling to try >=!

What do you think >> does in that line of code ?

from my understanding >> meant 'more than' but after a quick check, I now realise that i'm sorely mistaken! :sweat_smile: - thank you for bringing that up!

There are several "cheat-sheets" with commands and syntax, find one you like and get it printed for quick reference. One is here:
https://forum.arduino.cc/t/arduino-programming-cheat-sheet/67896

1 Like

ah immediately making it my desktop background! Cheers!

Awesome ... and not a bad idea for others :grinning:

Note there is no need to delete posts, you just use the "pencil" icon beneath to edit them.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.