How can I turn on and off staff with an ultasonic sensor.

I want to make a lamp turn on when I put my hand close but when I take it off I don’t want it toturn off I want it to stay on but I can’t find the code on the internet.And when I put my hand close agian it should turn my lamp off.

Sorry for my English.
Thank you.

AndriiL2:
I want to make a lamp turn on when I put my hand close but when I take it off I don't want it toturn off I want it to stay on but I can't find the code on the internet.And when I put my hand close agian it should turn my lamp off.

Sorry for my English.
Thank you.

What code are you using to test the sensor right now? Is it working?

Paul

Yes, all working fine but I don’t know the code for what I said at the beggining.

AndriiL2:
Yes, all working fine but I don't know the code for what I said at the beggining.

Do you use Serial.println() to show the value returned by the sensor? If so, do you see the difference in the value when your hand is close and when your hand is far away?

Paul

Yes, I have it and I won’t it to work like in the code below

int pinButton = 8;
int LED = 2;
int stateLED = LOW;
int stateButton;
int previous = LOW;
long time = 0;
long debounce = 200;

void setup() {
  pinMode(pinButton, INPUT);
  pinMode(LED, OUTPUT);
}

void loop() {
  stateButton = digitalRead(pinButton);  
  if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {
    if(stateLED == HIGH){
      stateLED = LOW; 
    } else {
       stateLED = HIGH; 
    }
    time = millis();
  }
  digitalWrite(LED, stateLED);
  previous == stateButton;
}

But I want it to triger with the sensor not with the button.
This is the code that I have so far. It is just a fraction of all hole project.

if (distance <= 30 && mode1 == 0 && mode2 == 0 && mode3 == 1 && LightMode == 1) {
    digitalWrite(Light, HIGH);
    delay(1000);
    Serial.println("h");
    LightMode = 0;
  }
  if (distance <= 30 && mode1 == 0 && mode2 == 0 && mode3 == 1 && LightMode == 0) {
    LightMode = 1;
    digitalWrite(Light, LOW);
    delay(1000);
    Serial.println("g");

Ok, then what values are you getting for “distance” and are they correct?

Paul

You could refer to this code:

constint lampPin = 2;
const DISTANCE_THRESHOLD = 30; // 30 cm

int currentNearState  = 1; //  0: NEAR, 1: FAR
int previousNearState = 1; //  0: NEAR, 1: FAR
int lampState = LOW;

void setup() {
  // your setup code
}

void loop() {
  int currentDistance = // implement the getting distance from sensor here

  currentNearState = (currentDistance > DISTANCE_THRESHOLD) ? 1 : 0;

  if(previousNearState == 1 && currentNearState == 0) // change from FAR to NEAR
  {
    // toggle lamp state
    lampState = (lampState == HIGH) ? LOW : HIGH;
    digitalWrite(lampPin, lampState);
  }

  previousNearState = currentNearState;
}

For reading distance from ultrasonic sensor, please refer to this tutorial

IoT_hobbyist:

  currentNearState = (currentDistance > DISTANCE_THRESHOLD) ? 1 : 0;

if(previousNearState == 1 && currentNearState == 0) // change from FAR to NEAR
[...]
    lampState = (lampState == HIGH) ? LOW : HIGH;

Those lines can be much simpler, and using the true/false wording makes it more readable. Of course you should use bool rather than int as type for those two variables; if only because it saves you a byte for each in RAM. Especially the second ternary operator is really a seriously overdone not operation.

  currentNearState = currentDistance > DISTANCE_THRESHOLD; // simply returns true or false, 1 or 0.
  if (previousNearState == true && currentNearState == false) // change from FAR to NEAR; the '== true' part may be omitted.
[...]
    lampState = ~lampState; // Toggle using a not operation.

Great! @wvmarle, kama++

You're welcome, and thanks for the karma :slight_smile:

I'm sorry, I'm new to this. I tride everything but it doesn't work.
I don't know how to make it work.