Need help on code

Hi guys, currently I’m doing a project which involves using an ultrasonic sensor and a LED. Currently, the codes works whereby if my ‘distance’ is less than 10, it would toggle my ‘hold’ value. However, I wish to improve the code whereby my hold value would only toggle only when the person moves away from the sensor (i.e. distance >10)

could anyone help me with my code?
below is my code for reference.

#include <AugikoModulesKit1.h>
#include <boarddefs.h>
int led1 = 3;
long duration; //define duration for ultrasonice refresh
int distance; //define ultrasonic sensor
const int trigPin = 11; //input trig for ultrasonic sensor
const int echoPin = 10; //output pin for ultrasonic sensor
int hold=0;
int i=0;

void setup() 
{
 Serial.begin(9600);
 pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
 pinMode(echoPin, INPUT); // Sets the echoPin as an Input
 pinMode(led1, OUTPUT);
}

void loop() 
{
 distancecal();
 Light();

  Serial.print("Distance: ");
  Serial.println(distance);
  Serial.println(hold);
  delay(500);
}

void distancecal(){
     // Clears the trigPin
  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; 
  if (distance<10 && hold == 0){
        hold = 1;
 } else if (distance<10 && hold ==1){
        hold = 0;
        }     
}

void Light(){
 
   if(hold ==1){
        analogWrite(led1, i);
        delay(500);
        i+=21;
        if(i>=255) i=255;
        } 
   else {
        analogWrite (led1, 0);
        i = 0;
        }
}

thank you so much

Hi guy/gal. Welcome to the Forum. First thing you need to do is to learn how to post your code inside tags like this,

void loop()
{
    distancecal();
    Light();

    Serial.print("Distance: ");
    Serial.println(distance);
    Serial.println(hold);
    delay(500);
}

void distancecal()
{
    // Clears the trigPin
    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;
    if (distance<10 && hold == 0)
    {
        hold = 1;
    } else if (distance<10 && hold ==1)
    {
        hold = 0;
    }     
}

void Light()
{

    if(hold ==1)
    {
        analogWrite(led1, i);
        delay(500);
        i+=21;
        if(i>=255) i=255;
    }
    else if (hold ==0)
    {
        analogWrite (led1, 0);
        i = 0;
    }
}

Secondly, most often it helps to post your entire code as we have no idea as to what declarations are being made, what’s going on in setup, what libraries are attached, etc.

You can learn about this and a whole bunch of other useful stuff by reading the post entitled “Read this before posting…” at the top of page 1. :wink:

 if (distance<10 && hold == 0){
        hold = 1;
 } else if (distance<10 && hold ==1){
        hold = 0;
        }     
}

So you say now it works when distance is less than 10 and you can’t imagine what to change to make it do that instead when distance is greater than 10? You don’t think changing those less than operators to greater than would do it?

Sorry, let me rephrase my question. Currently my program is a night light activated by an ultrasonic sensor. currently, it works if a person walks past the ultrasonic sensor and my LED would turn on/off. however, I need some advise to improve my code whereby if a person were to stand in front of the ultrasonic sensor, my LED would only turn on/off when he leaves the position

So instead of if maybe while?

Pseudocode:

boolean someoneStandingThere = false;

/////   in loop

if(distance <= 10) {
    someoneStandingThere = true;
}

if(distance > 10 && someoneStandingThere) {
    ///  someone just moved away so do your thing
    someoneStandingThere = false;
}