Button press only working at end of loop?

Hello and thanks your time..

I have a problem where I have a simple program using an Arduino to light 16 LED's via a shift repeater.
I use a push button to move onto the next pattern of lights but the push button is only working at the end of my patterns loop if I hold the button down.

I have been stuck on it now for over a week, I've Googled and Googled and tried everything I can think of and I don't think my head can handle anymore bangs against my desk for inspiration.

Please could somebody help me?

Thank you

Here is the code:

    const int latchPin = 11;
    const int clockPin = 10;
    const int dataPin = 12;
    const int buttonPin = 5;

    int cycle = 0;

    int buttonState = 0;
    int lastbuttonstate = 0;
    int index = 0;

    void setup() {

    Serial.begin(9600);

    pinMode(latchPin, OUTPUT);
    pinMode(clockPin, OUTPUT);
    pinMode(dataPin, OUTPUT);
    pinMode(buttonPin, INPUT);
    wipe();
    }

    void loop() {

    buttonState = digitalRead(buttonPin);
    if (buttonState != lastbuttonstate) {
    if (buttonState == HIGH) {
    cycle = cycle + 1;
    } else {
    delay(50);
    }}
    lastbuttonstate = buttonState;

    if (cycle == 0) {
    wipe();
    }

    if (cycle == 1) {
    pattern1();  
    }
    
    if (cycle == 2) {
    pattern2();
    }
    
    if (cycle == 3) {
    pattern3();
    }
    
    if (cycle > 3) {
    cycle = 0;
    }
    
    }

    void pattern1() {

    byte seq1[16] = {B10001000,B01001000,B00101000,B00011000,
                    B00010100,B00100100,B01000100,B10000100,
                    B10000010,B01000010,B00100010,B00010010,
                    B00010001,B00100001,B01000001,B10000001};
    for (int n = 0; n < 16; n++)
    {
        digitalWrite(latchPin, LOW);
        shiftOut(dataPin, clockPin, MSBFIRST, seq1[n]);
        digitalWrite(latchPin, HIGH);
        delay(150);
    }
    
    }

Use code tags when you post code on the forum.

And to solve the problem that you describe, get rid of delay and use a millis() based appriach.

MikePL:
I use a push button to move onto the next pattern of lights but the push button is only working at the end of my patterns loop if I hold the button down.

That's because of the way you have written your code. Everything has to complete before the code gets back to the line

buttonState = digitalRead(buttonPin);

which is actually at the start of loop() and not at the end of it.

You need to re-write your code with without using delay() and without using FOR. Allow loop() to do the iterations and use millis() to manage the timing without blocking as illustrated in Several Things at a Time. Then your program will be able to check the button hundreds or thousands of times per second.

...R

You need to re-write your code with using delay()

I am sure that Robin meant

You need to re-write your code without using delay()

Thank you all for the replies.

Sorry for not including the code tags when I originally left the post, I will make sure I do for any future posts.

I will do some more research on the topics you have covered and see where I go from there.

Thank you again :slight_smile:

UKHeliBob:
I am sure that Robin meant

You need to re-write your code without using delay()

Thanks for spotting that. I have corrected it.

...R