Break Function Question

I am trying to figure out what I am doing incorrectly. So far the program work without the break function, but when I put in the code shown below it requires me to hold the switch down for the LED to function.

I have tried writing it numerous way and either it doesn’t function at all or the button needs to stay pressed. I am trying to interrupt the LED during the cycle with the press of the switch.

I am not asking you to fix it but provide some guidance as to what I am missing in fixing the break function.

Thank you!

const int LED=9; //The LED is connected to pin 9 
const int switchPin=2; //The Switch is connected to pin 2
boolean lastButton = LOW; //Variable containing the previous button state
boolean currentButton = LOW; //Variable containing the current button state
boolean ledLevel = 0; //The present state of the LED (on/off)


void setup() {
  // put your setup code here, to run once:
pinMode (LED, OUTPUT); //Set the LED Pin as an output
pinMode (switchPin, INPUT); //Set the button as input if required
}

//Debounce for the switch
boolean debounce(boolean last)
{
  boolean current = digitalRead(switchPin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;
}

void loop()   //code to make the LED move through all 255 stages of brightness:
{
  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)

  for (int ledLevel=0; ledLevel<256; ledLevel++)
  {

    analogWrite(LED, ledLevel);
    digitalRead(switchPin);
    if (currentButton != debounce(lastButton))
    {
      break;
           
    }
    delay(10);
      
   }
}

I am sure you have studied what "break" actually does, so I am surprised you don't know that the logic just returns to the start of "loop" and goes through loop again and again.

Paul

Why do you think you need break there at all?

You don't, but if we knew why you think you do then we might be able to explain better.

Paul_KD7HB:
I am sure you have studied what “break” actually does, so I am surprised you don’t know that the logic just returns to the start of “loop” and goes through loop again and again.

Paul

That could be a confusing way to state what a break statement does. The actual code being run is found in main.cpp:

int main(void)
{
 init();

 initVariant();

#if defined(USBCON)
 USBDevice.attach();
#endif
 
 setup();
    
 for (;;) {
 loop();
 if (serialEventRun) serialEventRun();
 }
        
 return 0;
}

A break statement in the OP’s code actually transfer program control to the first statement following the current program structure (e.g., for, while, switch) which encompasses it. In this case, his break statement actually transfers control to the if (serialEventRun) test found in main(). Because the second expression in main()'s for loop is null, the program then performs another call to loop().

The Arduino IDE supports other processors (e.g., STM32, ESP32, Teensy, etc.) and the “hidden” main() could be important if that other processor is behaving differently (e.g., threading) than a standard Arduino processor.

PerryBebbington:
Why do you think you need break there at all?

You don't, but if we knew why you think you do then we might be able to explain better.

I did read the break statement. I was trying to end the loop to restart.

If break is not what I need, how do I end the loop by pressing the button, rather what would be the best way to go about it? I was reading about interrupt, but the goal is to cause the ledLevel = 0 when the switch is pressed in the middle of the loop, followed by restarting the loop when pressed again.

I guess in reading it I thought that would be a good way to tackle that.

Paul_KD7HB:
I am sure you have studied what "break" actually does, so I am surprised you don't know that the logic just returns to the start of "loop" and goes through loop again and again.

Paul

Thank you Paul. It was not clear when I read it on the arduino posting that break actually returned to the start of the loop, as opposed to breaking the loop and restarting. But I did as much reading as I could before posting it here, which is why I was not certain why I couldn't get it to break the loop.

Arduino - Break The reference there doesn't exactly say it returns to the start of the loop.

I believe I have figured out how to do it. I appreciate the candid answers of that break was not what was needed, forcing me to look elsewhere. I ended up using the return function

Please let me know if this is how you would have done it.

Thank you.

const int LED=9; //The LED is connected to pin 9 
const int switchPin=2; //The Switch is connected to pin 2
boolean lastButton = LOW; //Variable containing the previous button state
boolean currentButton = LOW; //Variable containing the current button state
boolean ledLevel = 0; //The present state of the LED (on/off)


void setup() {
  // put your setup code here, to run once:
pinMode (LED, OUTPUT); //Set the LED Pin as an output
pinMode (switchPin, INPUT); //Set the button as input if required
}

//Debounce for the switch
boolean debounce(boolean last)
{
  boolean current = digitalRead(switchPin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;
}

void loop()   //code to make the LED move through all 255 stages of brightness:
{
  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)

  for (int ledLevel=0; ledLevel<256; ledLevel++)
  {
    analogWrite(LED, ledLevel);
    delay(10);
    if (debounce(lastButton) != LOW) return;
  }

  lastButton = currentButton; 
  if (ledLevel > 255) ledLevel =0;
  analogWrite(LED, ledLevel);
}

It's obvious that you don't yet know the basics. How much of the tutorials have you done? If none or not many please work through them.

Things you obviously don't understand:

  • What setup(); is for
  • What loop(); is for or how it works
  • What break; does and when to use it (clue, not for what you are doing)
  • What return; does and when to use it (clue, not for what you are doing)

I'm not going to give you a personalised tutorial, there are perfectly good tutorials in the IDE, on this web site and elsewhere, and they are probably better written than anything I could write in 5 minutes even if I wanted to.

When I was learning C I found this web site very helpful C Tutorial

Please study the tutorials and the sample code, please don't try writing new code until you have learned enough to realise without us telling you what is wrong with the code you have.

Good luck and enjoy