Loop patterns (slow respons

Hello all. iam busy with a project than involves multiple loops that are turned "looped?" at different times using a push button. The problem is that the button is not that responsive. Either I will skip a loop or it dosnt change at all, only if I hold it in for exactly 1 second+- Thanks for al the help

int L5 = 9;
int L6 = 8;
int L7 = 7;  //7 LED pin

int buttonPin = 6;  //the number of the pushbutton pin

int de=50;  // delay time

int p=0;    // variable for pattem

int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  
  pinMode(L5, OUTPUT);
  pinMode(L6, OUTPUT);
  pinMode(L7, OUTPUT);
  
  pinMode(buttonPin, INPUT_PULLUP);


}

void loop() 
{
 buttonState = digitalRead(buttonPin);

 if (buttonState == HIGH)

    {
      p++;
      delay(800);
    } 

  if(p==1)
  {

 digitalWrite(L7,1);  
  delay(500);  
   digitalWrite(L7,0);  
  delay(500);  
   digitalWrite(L7,1);  
  delay(500);  
   digitalWrite(L7,0);  
  delay(500); 
  
  buttonState = digitalRead(buttonPin); 
  
  }
 if(p==2)
 {
 digitalWrite(L7,1);  
  delay(100);  
   digitalWrite(L7,0);  

   buttonState = digitalRead(buttonPin);

 if (buttonState == HIGH)

    {
      p++;
      delay(100);
    } 
   digitalWrite(L7,1);  
  delay(100);  
   digitalWrite(L7,0);  
  delay(100); 

  buttonState = digitalRead(buttonPin);

 if (buttonState == HIGH)

    {
      p++;
      delay(50);
    } 
  
  
 }
 if(p==3)
 {
 digitalWrite(L7,1);  
  delay(1000);  
   digitalWrite(L7,0);  
  delay(1000);
   buttonState = digitalRead(buttonPin);

 if (buttonState == HIGH)

    {
      p++;
      delay(1000);
    }   
   digitalWrite(L7,1);  
  delay(1000);  
   digitalWrite(L7,0);  
  delay(1000); 

  buttonState = digitalRead(buttonPin);

 if (buttonState == HIGH)

    {
      p++;
      delay(50);
    } 
  
 }
if(p==4)
{
  digitalWrite(L7,LOW);
  p=0;
}
}

delay(500) Not responsive you say?

Hmmm.

Usual advice - get rid of all the delays, and use a state machine.

Must I remove all delays because I need the ones between the led,s and iam not sure what a State machines is but googling now. :)

You need a pause between switching the LEDs, but you do not want to use delay.

What should I use ?? a SM

Take a look at the blink without delay example in the IDE.

You’re not the first person to ask this question.

This is literally the most common question asked about arduino programming. There are countless guides on blink without delay techniques, "doing more than one thing at a time" and so on, by using millis() for timing instead of delay.

Got it working :slight_smile: :slight_smile:

const int buzzer = 9; //buzzer to arduino pin 9
const int led=8;
const int button=7;
int buttonstate=0;
int var = 2;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
pinMode (buzzer, OUTPUT);
pinMode (led, OUTPUT);
pinMode (button, INPUT);
}


void loop() {
  // put your main code here, to run repeatedly:
  buttonstate=digitalRead(button);
Serial.print(buttonstate);

if(buttonstate == HIGH){ 

for (int X=0; X < 3; X++)
{

  tone(buzzer, 200); // Send 1KHz sound signal...
  delay(1000);        // ...for 1 sec
  noTone(buzzer);     // Stop sound...
  delay(1000);        // ...for 1sec
  
  
}
  for (int Y=0; Y < 10; Y++)
{
digitalWrite(led, HIGH);
  delay(200);
  digitalWrite(led, LOW);
  delay(500);
}}


}

Still not very responsive, is it?

No is good and all to start it but not sure how to brake out of the loop

Would you know how to brake out of the FOR loop with a button??

Yes, use “break”, but the simple answer is to not use a for loop in the first place.

Or delay().

Iam not sure of any outher way to make this loop. Sorry… Anouther problem now is that it loops 6-8 times and not 2* like a set it for,

#include <RF24.h>
#include <RF24Network.h>
#include <SPI.h>
const int buzzer = 7; //buzzer to arduino pin 9
const int led=2; //led pin

RF24 radio(10, 9);               // nRF24L01 (CE,CSN)
RF24Network network(radio);      // Include the radio in the network
const uint16_t this_node = 01;   // Address of our node in Octal format ( 04,031, etc)


void setup() {
     pinMode (buzzer, OUTPUT);
    Serial.begin(9600);
   pinMode(led, OUTPUT);
  SPI.begin();
  radio.begin();
  network.begin(90, this_node); //(channel, node address)
  
}

void loop() {
  network.update();
  while ( network.available() ) {     // Is there any incoming data?
    RF24NetworkHeader header;
    unsigned long buttonState;
    network.read(header, &buttonState, sizeof(buttonState)); // Read the incoming data
    
Serial.print(buttonState);


if(buttonState == HIGH){ //checking that conection is made
  for (int X=0; X < 2; X++)
{
  tone(buzzer, 200);
  digitalWrite(led,HIGH);  // Send 1KHz sound signal...
  delay(800); 
  tone(buzzer, 300);
    digitalWrite(led,LOW);
  delay(800);
  tone(buzzer, 100);
    digitalWrite(led,HIGH);
  delay(800);
  tone(buzzer, 150);
    digitalWrite(led,LOW);
  delay(800);
  noTone(buzzer); 
  delay(800);   
}}}}

}}}}Don’t you just hate it when that happens?

No I did that on purpose. And that dosnt have anything to do with the qustion