0

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

  • pastebin.com/BULf5CfQ – MaskedCoding May 23 '17 at 19:32
  • Remove the loop() function, and rename void startShow(int i) as void loop(). – Edgar Bonet May 23 '17 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 May 23 '17 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 May 23 '17 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 May 23 '17 at 21:21
2

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
}
-3

Fairly simple. Void loop(void) ( Static int buttonpressed = 0. //button isn't pressed initially

If (button pressed == 0) ( If (isbuttonpressed() == !0) button pressed = !0. ) else ledshow()

)

Basically it polls constantly until the button is pressed. Once the button is pressed, it ignores the button and loops to show the led.

  • 1
    This is a very low quality answer. Code not formatted, wrong capital letters, variables which change name, undeclared functions, wrong parentheses....... – frarugi87 May 24 '17 at 7:24
  • Agreed. Please put some effort into formatting your answers. Using capital letters at the start of sentences is a start, and please format your code by selecting it and pressing Ctrl+K. – Nick Gammon Jul 24 '17 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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