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
1 Answer 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:
- You put in
setup()
the code you want to run when your program starts. - 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
}
loop()
function, and renamevoid startShow(int i)
asvoid loop()
.startShow(showType);
to just above ` oldState = newState;`. That way it get called al the time, and the button still changes the showType.