Push button inclusion

Hi All,

Im trying to figure out how i would include a simple push button code into the below to run the loop, i.e. turning the distance measuring program on and off. I have 2 examples of push button use one storing the state i.e. push once "on" push twice "off" or push and hold to turn on release to turn off, but cant see how to incorporate either into the below.

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);
  //LCD.setCursor(0,1);
  //LCD.print("sonar-low");
  //Serial.println("");
  //Serial.println("sonar-low");
  delayMicroseconds(2);
  //LCD.setCursor(0,1);  
  //LCD.print("sonar-high");
  //Serial.println("sonar-high");
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  //LCD.setCursor(0,1);  
  //LCD.print("sonar-low");
  //Serial.println("sonar-low");
  //delayMicroseconds(10000000);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  

  //LCD.setCursor(0,1);  //Set cursor to first column of second row
  Serial.print("");
  //LCD.println("                "); //Print blanks to clear the row
  //LCD.setCursor(0,1);   //Set Cursor again to first column of second row
  //LCD.print(distance); //Print measured distance
  Serial.println(distance);
  //LCD.print(" cm");  //Print your units.
  Serial.print(" cm ");
  delay(2500); //pause to let things settle
}
void setup()
  pinMode(pushButtonPin, INPUT_PULLUP); //wire a button between ground and pin of your choice

void loop() {
if (digitalRead(pushButtonPin)==LOW) { //push and hold to start measuring
  long duration, distance;
  digitalWrite(trigPin, LOW);
  //LCD.setCursor(0,1);
  //LCD.print("sonar-low");
  //Serial.println("");
  //Serial.println("sonar-low");
  delayMicroseconds(2);
  //LCD.setCursor(0,1);  
  //LCD.print("sonar-high");
  //Serial.println("sonar-high");
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  //LCD.setCursor(0,1);  
  //LCD.print("sonar-low");
  //Serial.println("sonar-low");
  //delayMicroseconds(10000000);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  

  //LCD.setCursor(0,1);  //Set cursor to first column of second row
  Serial.print("");
  //LCD.println("                "); //Print blanks to clear the row
  //LCD.setCursor(0,1);   //Set Cursor again to first column of second row
  //LCD.print(distance); //Print measured distance
  Serial.println(distance);
  //LCD.print(" cm");  //Print your units.
  Serial.print(" cm ");
  delay(2500); //pause to let things settle
} 
}

I gave that a go but the loop just runs irrespective of grounding pin 2

#define trigPin 11 //Sensor Echo pin connected to Arduino pin 13
#define echoPin 12 //Sensor Trip pin connected to Arduino pin 12
int buttoninput = 2;
int buttonstate = 0;

//Simple program just for testing the HC-SR04 Ultrasonic Sensor with LCD dispaly 
//URL:

void setup() 
{  
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buttoninput, INPUT_PULLUP);
  digitalWrite(buttoninput, HIGH);
 
  
  LCD.begin(16,2); //Tell Arduino to start your 16 column 2 row LCD
  LCD.setCursor(0,0);  //Set LCD cursor to upper left corner, column 0, row 0
  LCD.print("Target Distance:");  //Print Message on First Row
  Serial.begin(9600);
  
}

void loop() {
  if (digitalRead(buttoninput)  == LOW) {
  long duration, distance;
  digitalWrite(trigPin, LOW);
  //LCD.setCursor(0,1);
  //LCD.print("sonar-low");
  //Serial.println("");
  //Serial.println("sonar-low");
  delayMicroseconds(2);
  //LCD.setCursor(0,1);  
  //LCD.print("sonar-high");
  //Serial.println("sonar-high");
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  //LCD.setCursor(0,1);  
  //LCD.print("sonar-low");
  //Serial.println("sonar-low");
  //delayMicroseconds(10000000);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  

  //LCD.setCursor(0,1);  //Set cursor to first column of second row
  Serial.print("");
  //LCD.println("                "); //Print blanks to clear the row
  //LCD.setCursor(0,1);   //Set Cursor again to first column of second row
  //LCD.print(distance); //Print measured distance
  //buttonstate = digitalRead(buttoninput);
  //if(buttonstate == LOW){
  Serial.println(distance);
  Serial.print(" cm ");
  delay(2500); //pause to let things settle

 // else{
   Serial.println(  "output  off");
   delay(2500); 
  
  
  
} }

Here's the code with all the commented out stuff removed. In case someone wants to have a little easier time trying to read it.

#define trigPin 11 //Sensor Echo pin connected to Arduino pin 13
#define echoPin 12 //Sensor Trip pin connected to Arduino pin 12
int buttoninput = 2;
int buttonstate = 0;

//Simple program just for testing the HC-SR04 Ultrasonic Sensor with LCD dispaly 
//URL:

void setup() 
{  
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buttoninput, INPUT_PULLUP);
  digitalWrite(buttoninput, HIGH);
 
  
  LCD.begin(16,2); //Tell Arduino to start your 16 column 2 row LCD
  LCD.setCursor(0,0);  //Set LCD cursor to upper left corner, column 0, row 0
  LCD.print("Target Distance:");  //Print Message on First Row
  Serial.begin(9600);
  
}

void loop() {
  if (digitalRead(buttoninput)  == LOW) {
  long duration, distance;
  digitalWrite(trigPin, LOW);

  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  

  Serial.print("");

  Serial.println(distance);
  Serial.print(" cm ");
  delay(2500); //pause to let things settle


   Serial.println(  "output  off");
   delay(2500); 
  
  
  
} }

but the loop just runs irrespective of grounding pin 2

It appears that your button is not wired correctly, and is reading LOW whether or not it is pressed.

If this is one of the standard square tactile buttons, wire diagonally across the button. One lead to the input pin and the other to ground.

Thats what i thought so i simply used a jumper on the board from GND to pin "2" directly even with no switch or direct jumper the program runs i.e. its impossible for pin 2 to be grounded but it runs.

Does it run if you leave pin 2 unconnected?

Yes it still runs with no connection at all on PIN 2

I was wondering if the UNO board has the ability to have an internal PullUp resister on PIN 2 ?

As fas as I know, every digital pin has internal pullup.
Pin13 with onboard led might not work as expected as the led and resistor might pull it down too much for a high reading.

Your pin 2 might be damaged if you have drawn too much current from it, for example configured it as output and set high when it was grounded.

Try another pin.

Re-tried tonight and its now working with no changes, only thing thats different is that power has been removed from the board and re-applied so it looks like a board power off/on has sorted it.