Button Not working

I have a button hooked up to the Arduino and it will not read the button state.
In the case the code is faulty I have included it
I have the button wired up exactly like the diagram on https://www.arduino.cc/en/Tutorial/Button
I am doing this for an assignment at my school and have no time left.

//Ultrasonic Transducer
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distanceCm, distanceInch;

#include <SimpleDHT.h>

// for DHT11, 
//      VCC: 5V or 3V
//      GND: GND
//      DATA: 2
int pinDHT11 = 8;
SimpleDHT11 dht11;

String dots;
int dotAmmount = 0;

bool testFin = true;
bool firstTest = false;
const int startButton = 6;
int startButtonState = 0;
  byte temperature = 0;
  byte humidity = 0;
  byte data[40] = {0};
  int led = 13;

void setup() {
  pinMode(startButton, INPUT);
  //Ultrasonic Transducer
  lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

void loop() {
  startButtonState = digitalRead(startButton);
   if(startButtonState == HIGH)
     Serial.print("Button On");
   } else
     Serial.print("Button Off");
    if(startButtonState == HIGH)
  //Ultrasonic Transducer
digitalWrite(trigPin, LOW);
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm= duration*0.034/2;
distanceInch = duration*0.0133/2;

//DHT11 & Data Displaying

//For Screen
lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Distance: "); // Prints string "Distance" on the LCD
lcd.print(distanceCm); // Prints the distance value from the sensor
lcd.print(" cm.");
lcd.print((int)temperature); lcd.print(" *C, ");
lcd.print((int)humidity); lcd.print(" %");

//For Serial
Serial.print(" Distance: ");
Serial.print(" cm.");

  // read with raw sample data.
  if (dht11.read(pinDHT11, &temperature, &humidity, data)) {
    //Loading animation
      if(dotAmmount == 0){dots = "";}
      if(dotAmmount == 1){dots = ".";}
      if(dotAmmount == 2){dots = "..";}
      if(dotAmmount == 3){dots = "..."; dotAmmount = -1;}
    Serial.print("Loading" + dots);
    dotAmmount += 1;
  for (int i = 0; i < 40; i++) {
    if (i > 0 && ((i + 1) % 4) == 0) {
      Serial.print(' ');
  Serial.print("Sample OK: ");
  Serial.print((int)temperature); Serial.print(" *C, ");
  Serial.print((int)humidity); Serial.println(" %");
  // DHT11 sampling rate is 1HZ.
  firstTest = false;
  testFin = true;
   } else

Science_Testing_Prog.zip (4.98 KB)

If you are using the button shown in the tutorial then chances are 50-50 that you may have wired the button wrong. Those buttons have 4 pins, 2 of which are connected internally to each side of the switch. To insure that you connect the switch properly make your connections to diagonal pins, NOT horizontal or vertical.

People here are reluctant to download and open zip files. See the how to use this forum sticky to see how to properly post code.

How to post images.

Thanks for the tip groundFungus.

An illustration of what Due_unto said.

dig sw button.jpg

The button tutorial has simple sample code on it. Have you tried this code? What result did you get?

Also, you have a "return" inside your "loop" function. Where do you expect this to return to?

JackFlynn: Also, you have a "return" inside your "loop" function. Where do you expect this to return to?

It ends loop and returns control to main which calls loop again. All perfectly normal.

It ends loop and returns control to main which calls loop again. All perfectly normal.

Delta_G I understand what the return does. I’m asking if it’s an intentional return and if it’s understood why it’s there. I would personally use a break.

break would not end the loop function. Return does. Break is only useful in while loops, for loops, and switch case statements.

In these cases the best is to exclude possible issues, so to confirm that the hardware is working properly i would make a sketch like this

const int led = 13;

const int startButton = 6;
int startButtonState = 0;

void setup() {                
  pinMode(startButton, INPUT);
  pinMode(led, OUTPUT);     

void loop() {
  digitalWrite(led, startButtonState); 
  delay(100); // (or less, but some for stability)

If that works the issue is in the software. I had some difficulty reading your code since the indentations were not consistent also in the code, every time the loop passes the digitalRead() something is being written to the Serial port, it would be more elegant using an oldButtonState variable, like this you will have some de-bounce functionality, maybe only the LCD is not displaying what you want, and the Serial monitor is showing loads of "button=Off"

Thinking about your code, is all of the code beyond the second check on startButtonState all part of that if statement ? if so, the compiler might see it all as 1 statement.

It was a very simple problem that was hiding in plain sight. On the code one of the display pins was also the pin used for the button, and when I was wiring it I accidentally removed pin 6 which made me think the pin was free.