AnswerBun.com

Is it possible to exit a loop on the change of an input state?

Arduino Asked by JabawokJayUK on September 15, 2020

I would like to exit a particular loop if the state of an input changes from LOW to HIGH. This is the current loop:

void brakeFade() {
pixels.clear();
pixels.setBrightness(255);
pixels.fill(16711680, 0, 0);

for(int i=255; i>=10; i-=1) {
  pixels.setBrightness(i);
  pixels.show();
  delay(5);
  }
for(int j=10; j<=255; j+=1) {
  pixels.setBrightness(j);
  pixels.show();
  delay(5);

}
}

I need that loop to exit if

(digitalRead(brakeFeed) == LOW)

Changes to HIGH during the loop.

One Answer

figured it out:

void brakeFade2() {
pixels.clear();
pixels.setBrightness(255);
pixels.fill(16711680, 0, 0);

for(int i=255; i>=10; i-=1) {
  if (digitalRead(brakeFeed) == LOW){
  pixels.setBrightness(i);
  pixels.show();
  delay(5);
  }
  else {
    break;
  }
}
for(int j=10; j<=255; j+=1) {
  if (digitalRead(brakeFeed) == LOW){
  pixels.setBrightness(j);
  pixels.show();
  delay(5);
  }
  else {
    break;
  }
  }

}

Answered by JabawokJayUK on September 15, 2020

Add your own answers!

Related Questions

AsyncWebServer giving wdt reset

1  Asked on September 14, 2020 by java-bee

         

serial 1 was not declared in the scope

1  Asked on July 30, 2020 by mian-yasir-shah

 

How can I set a timer?

2  Asked on July 24, 2020 by damon-swart

 

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir