Push button input to start prorgramme

Hi,
I would like to have a momentary push button to start my program off. I'm using an Arduino Micro and simply have one side of the push button on 5V and the other on pin 13 (so I can see onboard LED status).

Below is my code. Without the push push button code, it work fine. There are no compiler errors and the wiring is fine. Thanks !

//Tutorial https://www.youtube.com/watch?v=cAT07gy4DII
//This program lights up led's 1-64 one by one then stops at number 64.
#include <MultiShiftRegister.h>
#include <ShiftIn.h>

ShiftIn<8>shift; // Init ShiftIn instance with one chip. The number in brackets defines the number of daisy-chained 74HC165 chips.So if you are using two chips, you would write: ShiftIn<2> shift;

int latchPin = 10;//74HC565 pin setup
int clockPin = 11;
int dataPin = 12;

const int buttonPin = 13; //The number of the pin the push button is connected to.
int buttonState = 0; //Variable for reading the push button status

MultiShiftRegister msr (8, latchPin, clockPin, dataPin);//Put the total number of latches at the start of the bracket.

void setup() {

  pinMode(buttonPin, INPUT); //Initialise the pushbutton pin as an input
  buttonState = digitalRead(buttonPin); //Read the state of the pushbutton value.

  if (buttonState == HIGH)
  {
    Serial.begin(9600); // open the serial port at 9600 bps:

    // declare pins: pLoadPin, clockEnablePin, dataPin, clockPin
    shift.begin(7, 4, 5, 6);//74HC165 pin setup

    pinMode (latchPin, OUTPUT);//setup hc595 pins
    pinMode (clockPin, OUTPUT);
    pinMode (dataPin, OUTPUT);
    msr.shift();
    msr.set(0);
    msr.shift();
    msr.clear_shift(0);
    displayValues();
    delay(2000);
    shift.update();
    msr.clear_shift(0);

    for (int pin = 0; pin <= 65; pin += 1) //(pin +=1) = (pin = pin+1)
    {
      //msr.set_shift(pin);//To set eg '0 'individually, do msr.set(0); msr.shift(); to clear it: msr.clear_shift(0);
      // msr.set(0);try doing manual inserts here...
      // msr.shift();

      msr.set(pin); //Select LED to turn on
      msr.shift();  //Shift out on register
      //shift.update();
      displayValues();
      delay(150);
      shift.update();
      msr.clear_shift(pin); //Turn the selected LED off
      delay(150);
    }
  }
  else
  {
    ;//Do nothing
  }
}

void displayValues() {
  for (int i = 0; i < shift.getDataWidth(); i++) {
    Serial.print( shift.state(i) ); // get state of button i
    Serial.print(','); // comma
  }
  Serial.println();
}
void loop() {
  // if (shift.update()) // read in all values. returns true if any button has changed
  //  displayValues();
  //  Serial.print("Loop Entered");
  //delay(1000);

}

I would like to have a momentary push button to start my program off

Use a while loop in setup() to read the state of the input pin until it becomes LOW or HIGH depending on how you have it wired. Keep the pin in a known state by using a pullup or pulldown resistor or by using INPUT_PULLUP in pinMode() to activate the built in pullup resistor and eliminate the need for an external resistor

I've added a while loop within setup(). No compiler error but still doesn't seem to work. Added a pull down to the input pin just in case it floats.

If I change the code to while (buttonState=LOW), it goes through the program fine as I would expect. I would have expected to press the button and the program to have stopped but it doesn't.

//Tutorial https://www.youtube.com/watch?v=cAT07gy4DII
//This program lights up led's 1-64 one by one then stops at number 64.
#include <MultiShiftRegister.h>
#include <ShiftIn.h>

ShiftIn<8>shift; // Init ShiftIn instance with one chip. The number in brackets defines the number of daisy-chained 74HC165 chips.So if you are using two chips, you would write: ShiftIn<2> shift;

int latchPin = 10;//74HC565 pin setup
int clockPin = 11;
int dataPin = 12;

const int buttonPin = 13; //The number of the pin the push button is connected to.
int buttonState = 0; //Variable for reading the push button status

MultiShiftRegister msr (8, latchPin, clockPin, dataPin);//Put the total number of latches at the start of the bracket.

void setup() {

  pinMode(buttonPin, INPUT); //Initialise the pushbutton pin as an input
  buttonState = digitalRead(buttonPin); //Read the state of the pushbutton value.

  while (buttonState == HIGH)
  {
    Serial.begin(9600); // open the serial port at 9600 bps:

    // declare pins: pLoadPin, clockEnablePin, dataPin, clockPin
    shift.begin(7, 4, 5, 6);//74HC165 pin setup

    pinMode (latchPin, OUTPUT);//setup hc595 pins
    pinMode (clockPin, OUTPUT);
    pinMode (dataPin, OUTPUT);
    msr.shift();
    msr.set(0);
    msr.shift();
    msr.clear_shift(0);
    displayValues();
    delay(2000);
    shift.update();
    msr.clear_shift(0);

    for (int pin = 0; pin <= 65; pin += 1) //(pin +=1) = (pin = pin+1)
    {
      //msr.set_shift(pin);//To set eg '0 'individually, do msr.set(0); msr.shift(); to clear it: msr.clear_shift(0);
      // msr.set(0);try doing manual inserts here...
      // msr.shift();

      msr.set(pin); //Select LED to turn on
      msr.shift();  //Shift out on register
      //shift.update();
      displayValues();
      delay(150);
      shift.update();
      msr.clear_shift(pin); //Turn the selected LED off
      delay(150);
    }
  }

}

void displayValues() {
  for (int i = 0; i < shift.getDataWidth(); i++) {
    Serial.print( shift.state(i) ); // get state of button i
    Serial.print(','); // comma
  }
  Serial.println();
}
void loop() {
  // if (shift.update()) // read in all values. returns true if any button has changed
  //  displayValues();
  //  Serial.print("Loop Entered");
  //delay(1000);

}
while (buttonState == HIGH)
  {

And if it is LOW?

Nothing is happen when LOW.

I've added a while loop within setup(). No compiler error but still doesn't seem to work.

The idea is to stay in the while loop doing nothing except reading the input until the input changes to your "go" state

balgill021:
Nothing is happen when LOW.

Do you want to bet on that ?

No, the idea is to press a push button (causing a HIGH input to the Arduino) and for the program to run through the setup(). If there is no button press (LOW input to the Arduino), it does not need to go through setup().

the idea is to press a push button (causing a HIGH input to the Arduino) and for the program to run through the setup()

while (digitalRead(buttonPin) == LOW)
{
}  //don't do anything until buttonPin goes LOW

That worked great, thanks for helping me out!

Why did the way I did it not work? Even if I had to keep the button pressed for it to go through the void setup()?

Thanks

Why did the way I did it not work? Even if I had to keep the button pressed for it to go through the void setup()?

Probably because the program ran immediately after upload, reset or power on, read the state of buttonPin immediately, it was LOW because of your pulldown resistor so the whole of setup() was skipped. In any case, as you did not read the state of buttonPin in the while loop it would never have worked

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.