Control LED with SR04 distance sensor, activate by 2 push button switches

Hi, I am learning how to set up a basic Arduino Circuit, which have an on button and an off button.
When I press the on button the SR04 sensor will calculate the distance and then control the LED to turn on and off base on the distance.
And then when the off button is press, everything would end.

I have only been able to control the LED with the distance sensor, by calculating the distance and then using if statements to control the LED. However, I am still not able to implement the button switches into the program that I am having.

As of right now, the program only works when I hold down the on button.

Please help me with this. My code is down bellow

#define trigPin 12
#define echoPin 11
#define off_button 10
#define on_button 9
#define led 8

int val = 0;
int val_off = 0;

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(off_button, INPUT);
  pinMode(on_button, INPUT);
  pinMode(led, OUTPUT);
}

void loop() {
  long duration, distance;
  val = digitalRead(on_button);
  val_off = digitalRead(off_button);
  
  //calculate the distance:

    digitalWrite(trigPin, LOW);  
    delayMicroseconds(2); 
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10); 
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distance = (duration/2) / 29.1; 
  
  //control the LED
  if(val == HIGH)
  {
    if(distance > 10) digitalWrite(led,HIGH);
    else digitalWrite(led,LOW);
  }
    
  else if (val_off == HIGH)
  {
    digitalWrite(led,LOW);
  }    
  Serial.print(distance);
  Serial.println(" cm");
 
}

Why do you have the second if after the else? If val is not equal to high then it has to be low, there is no need to check.

Also do you not need to check for the transition from LOW to HIGH not on the level itself.

Grumpy_Mike:
Why do you have the second if after the else? If val is not equal to high then it has to be low, there is no need to check.

Also do you not need to check for the transition from LOW to HIGH not on the level itself.

Hi, I am using 2 different buttons, one to turn the program on and one to turn it off. But so far I have to hold the buttons for everything to work.

OK so it sounds like you are using momentary switches. IE. they only close the circuit if you hold them down forcing the input to go high. Assuming that you are connecting them between the input and 5v. This is potential dangerous, as in, if the pinMode is not set to input then you can destroy the input by taking it to +5V if it is set to sink by exceeding the maximum current on the input.

You would be better off using only one button and having it so that the first time you pressed it the system started reading the distance and reporting an it and then the next time you pressed it it went off, toggling between the two states each time you pressed it.

You would need to keep track of the current state with a variable and you would be best advised to use the de-bounce library for the button.

The preferred way to test for button presses is to connect them between input and GND with an inline resister to limit the current that can be passed and use either an external pullup or the internal ones that can be activated by the use of the INPUT_PULLUP options in the pinMode statement.

Cheers Pete.

Thanks for your advice. I have been able to make my program work the way I wanted to.

Assuming that you are connecting them between the input and 5v

Then there is no problem because he has set these pins to be inputs. What would be a problem is that there is no pull down resistor.
This is how you code with inputs.

http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

with an inline resister to limit the current that can be passed

NO!!!
There is no need to have an inline resistor with an input pin. That is a stupid current trend and is totally unnecessary.

@MIke

The only issue i can see with leaving an input open and then pulling it to Vcc without a pull down resister is that it is subject to noise. The inputs are after all quite high impedance and an open pin with a length if wire leading to the switch can chatter unless biased (with a resister) to one state or the other.

I was only making an observation that IF the pins where somehow to get set at OPs then there is a potential problem here.

As a beginner and whilst experimenting with code this can, and I have no doubt has, happened when a person changes the pins they are using or adds functionality to the project.

If you feel that my observation are totally wrong then you have never made a mistake.

These observations and suggestion will save the day if such mistakes happen and whilst they are not a requirement to make the device work they are valid suggestion in an experimental environment

The only issue i can see with leaving an input open and then pulling it to Vcc without a pull down resister is that it is subject to noise. The inputs are after all quite high impedance and an open pin with a length if wire leading to the switch can chatter unless biased (with a resister) to one state or the other.

The point is that you should never leave an input unconnected, that is called floating and will produce false readings.

If you feel that my observation are totally wrong then you have never made a mistake.

Consider what mistakes have to be made, it is not just one.
First the pin has to be defined as an output.
Second the output has to be set high
Third the button has to be pressed
So three mistakes, this is two more than safty requirements ditactate.

Grumpy_Mike:
The point is that you should never leave an input unconnected, that is called floating and will produce false readings.

Mike, I think that you may have misread my original post where this was EXACTLY what i was saying about floating inputs and what I then went on to justify in the follow up post. We are both singing of the same hymn sheet here.

Silly mistakes can happen and do. If he had defined the pins with #define statements and then added a few more I/Os with both inputs and outputs and changed them about this would not be unreasonable. The first thing he is going to do to test it is press the button.

But like you said this is a matter of personal preference. I do not tend to do it myself but i still get caught out sometimes :slight_smile: then wish that I had.