Why are the loops not looping

It has been a while since the last I coded but from my experience and looking at other topics on this forum, thecode should work. However, the functions aren't looping and I can't seem to figure out why. For example, with f2, the lights have to keep blinking until another function gets called by pressing a button. What is the reason why the functions don't loop?

I know there might be better ways to code this. But this was the easiest way to do it, with what I could remember :).

Thank you in advance for helping me!

int i = 0;

int switch1 = 0;
int switch2 = 0;
int switch3 = 0;

int teller1 = 0;
int teller2 = 0;
int teller3 = 0;

void setup() {

  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);

  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);

}

void f1() {

  digitalWrite(5, LOW); 
  digitalWrite(6, LOW);
  delay(1000); 

}

void f2() {

  digitalWrite(5, LOW);
  digitalWrite(6, HIGH);
  delay(500);
  digitalWrite(5, HIGH);
  digitalWrite(6, LOW);
  delay(500);

}

void f3() {

  for (i=0; i<=4; i++) {
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    delay(500);
    digitalWrite(6, LOW);
    delay(500);
  }

}

void f4() {

  digitalWrite(5, LOW);
  digitalWrite(6, HIGH);
  delay(500);
  digitalWrite(6, LOW);
  delay(500);

}

void f5() {

  for (i=0; i<=4; i++) {

    digitalWrite(6, LOW);
    digitalWrite(5, HIGH);
    delay(500);
    digitalWrite(5, LOW);
    delay(500);

  }

}

void f6() {

  digitalWrite(6, LOW);
  digitalWrite(5, HIGH);
  delay(500);
  digitalWrite(5, LOW);
  delay(500);

}

void loop() {

  switch1 = digitalRead(2);
  switch2 = digitalRead(3);
  switch3 = digitalRead(4);

  
    if (switch1 == HIGH) {
      
      teller1++;

      if (teller1 == 1) {

        f1();

      }

      if (teller1 == 2) {

        f2();

      }

      if (teller1 >= 3) {

        teller1 = 0;

      }
  
    }

    if (switch2 == HIGH) {
      
      teller2++;

      if (teller2 == 1) {

        f3();

      }

      if (teller2 == 2) {

        f4();

      }

      if (teller2 >= 3) {

        teller2 = 0;

      }
  
    }

    
    if (switch3 == HIGH) {
      
      teller3++;

      if (teller3 == 1) {
        
        f5();

      }

      if (teller3 == 2) {

        f6();

      }

      if (teller3 >= 3) {

        teller3 = 0;

      }
  
    }

}

How are your switches wired ?


Wire them as S3 is wired, look for a LOW for a closed switch:

In setup use INPUT_PULLUP

I do have them wired as LOW. I must add that the lights do react when a switch (or button) is pressed. The only thing for me that currently isn't working is the code.

void setup() {

  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);

  pinMode(2, INPUT_PULLUP);  //  <———<<<<<
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);

}

When it comes to switches, always look at a switch change in in state not the switch’s current level.

Also you do realize delay( ) is a blocking function.


Are you saying you have pull down resistors from the IO pin to ground?

There the switch state is read, correct? And if the switch is not held down, the loopyLEDthingy stops, correct? And you want to know how to do a momentary press of a button? And to have that momentary press cause a "STATE" change that causes one of several LED functions to loop? And the LED function is to loop till another "STATE" change is detected, correct?

Hello mhelluh

In general, you should not use magic numbers. The I/O pins love to have a functional name.

First of all, unless you hold the button down (assuming your hardware is connected properly) then the functions won't get called over and over again. Secondly, because you have delays, you will have to hold down the button through the delays to make sure the program sees the state of the button.

If you want for the functions to get called over and over but only press the button momentarily you need to completely restructure your code to remove all of the delay() calls and to detect a CHANGE in the state of the button not the STATE of the button. I would recommend a state machine, use millis() for timing (rather than delay()), and state change detection for the buttons. The following tutorials may help:

State Change Detection

State Machine

Example-code for timing based on millis()

Thank you guys for all the anwser, they are really helpfull. My intention seems to be good, however there are some big mistakes. Unfortunately, I won't have time to look into it for the next few days... But once I have made some changes i'll let you know whether it works or not!

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