1

In a piece off code I am modifying I have a button connected to start the code and then it resets its self afterwards. Is they a way to make the arduino loop that code without manually pressing the button again?

This is a section off the code:

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif
#define PIN 6
#define BUTTON_PIN 9

Adafruit_NeoPixel strip = Adafruit_NeoPixel(120, PIN, NEO_GRB + NEO_KHZ800);

bool oldState = HIGH;
int showType = 0;

void(* resetFunc) (void) = 0; //declare reset function @ address 0

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  // Get current button state.
  bool newState = digitalRead(BUTTON_PIN);

  // Check if state changed from high to low (button press).
  if (newState == LOW && oldState == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState = digitalRead(BUTTON_PIN);
    if (newState == LOW) {
      showType++;
      if (showType > 9)
        showType=0;
      startShow(showType);
    }
  }

  // Set the last button state to the old state.
  oldState = newState;
}

void startShow(int i) {  
  colorWipe(strip.Color(75, 0, 0), 50); // Red
  colorWipe(strip.Color(0, 75, 0), 50); // Green
  colorWipe(strip.Color(0, 0, 75), 50); // Blue
  theaterChase(strip.Color(75, 75, 75), 50); // White
  theaterChase(strip.Color(75, 0, 0), 50); // Red
  theaterChase(strip.Color(0, 0, 75), 50); // Blue
  rainbow(20);
  rainbowCycle(20);
  theaterChaseRainbow(50);
  resetFunc();  //call reset
}

Thanks Reece

6
  • pastebin.com/BULf5CfQ Commented May 23, 2017 at 19:32
  • Remove the loop() function, and rename void startShow(int i) as void loop(). Commented May 23, 2017 at 20:02
  • Move startShow(showType); to just above ` oldState = newState;`. That way it get called al the time, and the button still changes the showType.
    – Gerben
    Commented May 23, 2017 at 20:07
  • Could you describe more clearly what you want to happen please? Do you want to wait for the button to be pressed then do the LED stuff forever?
    – Mark Smith
    Commented May 23, 2017 at 20:09
  • 1
    If you want the led stuff to loop forever, why are you resetting the board at the end of that function? Why not just let loop call it again?
    – Delta_G
    Commented May 23, 2017 at 21:21

1 Answer 1

1

As pointed out by Delta_G, there is no point in resetting your program. What you should do is think your program in terms of the Arduino logic:

  1. You put in setup() the code you want to run when your program starts.
  2. You put in loop() the code you want to run over and over again.

In this case loop() is fairly simple, as the stuff you want to run over and over again is the animation, thus:

void loop() {
  colorWipe(strip.Color(75, 0, 0), 50); // Red
  colorWipe(strip.Color(0, 75, 0), 50); // Green
  colorWipe(strip.Color(0, 0, 75), 50); // Blue
  theaterChase(strip.Color(75, 75, 75), 50); // White
  theaterChase(strip.Color(75, 0, 0), 50); // Red
  theaterChase(strip.Color(0, 0, 75), 50); // Blue
  rainbow(20);
  rainbowCycle(20);
  theaterChaseRainbow(50);
}

Note that I removed the call to resetFunc().

The stuff you want to do at startup is the initialization. Oh, wait. In a comment you wrote “I want to wait for the button press”. So this also has to be done before the program starts looping. Hence your setup() should initialize stuff, then wait for the button press:

void setup() {
  // Initialize the strip to all piwels off.
  strip.begin();
  strip.show();

  // Wait until the button is pressed.
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  while (digitalRead(BUTTON_PIN) == HIGH)
      ; // do nothing
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.