I want to have a button switch the animation that is running. The button works but it doesn't update value of the button when the animation is running. How can I have the button value constantly update while the animation is running? here is my code:
#include <FastLED.h>
#include "Plasma.cpp"
#include "TestPattern.cpp"
#include "Snake.cpp"
#include "Twinkle.cpp"
#include "DeadChannel.cpp"
#include "Bouncy.cpp"
#include "HiRez.cpp"
#include "Boxes.cpp"
#include "Life.cpp"
#include "Sprite.cpp"
#define WIDTH 14
#define HEIGHT 14
#define NUM_LEDS WIDTH * HEIGHT
#define DATA_PIN 3
CRGB leds[NUM_LEDS];
const int buttonPin = 5;
// Variables will change:
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState=0;
void setup() {
// put your setup code here, to run once:
pinMode(buttonPin, INPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(8, OUTPUT);
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
// TestPattern testPattern(leds, WIDTH, HEIGHT);
// testPatt.start();
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
}
}
lastButtonState = buttonState;
if (buttonPushCounter == 1) {
digitalWrite(8,HIGH);
doTwinkle();
} else if (buttonPushCounter == 2){
digitalWrite(9,HIGH);
doSnake();
} else if (buttonPushCounter == 3){
digitalWrite(10,HIGH);
doPlasma();
buttonPushCounter = 1;
}
}
void doDeadChannel() {
DeadChannel deadChannel(leds, WIDTH, HEIGHT);
deadChannel.start();
}
void doPlasma() {
Plasma plasma(leds, WIDTH, HEIGHT);
plasma.start();
}
void doTwinkle() {
Twinkle twinkle(leds, WIDTH, HEIGHT, true, true);
twinkle.start();
}
void doSnake() {
Snake snake(leds, WIDTH, HEIGHT);
snake.start();
}
void doLife() {
Life life(leds, WIDTH, HEIGHT, 56);
life.start();
}
void doSprite() {
Sprite sprite(leds, WIDTH, HEIGHT);
sprite.start();
}
Thats the main part here is the parts that it refers to
Twinkle:
/*
* Effect implementation for the GauntletII project.
*/
#include "Effect.h"
#define MAX_TWINKS 25
#define OFFSET 0xB000
typedef struct Twink {
short x;
short y;
} Twink;
class Twinkle : public Effect {
private:
short numTwinks;
bool colour;
bool cycleSaturation;
public:
Twinkle(CRGB *leds, int width, int height, bool colour, bool cycleSaturation) :
Effect(leds, width, height),
colour(colour),
cycleSaturation(cycleSaturation),
numTwinks(0) {
}
void start() {
for (uint16_t frame = 0x0000, iterations = 0; iterations < 2250; frame += 0x20, iterations++) {
for (int i = 0; i < width * height; i++) {
if (leds[i]) {
leds[i].fadeToBlackBy(50);
if (!leds[i]) {
numTwinks--;
}
} else {
if (random(56) == 0) {
numTwinks++;
if (colour) {
if (cycleSaturation) {
uint8_t saturation = (sin16(frame + OFFSET) >> 8) + 128;
leds[i] = CHSV(random(255), saturation, 255);
} else {
leds[i] = CHSV(random(255), random(128, 255), 255);
}
} else {
leds[i] = CRGB::White;
}
}
}
}
LEDS.show();
}
}
};
Snake:
/*
* Effect implementation for the GauntletII project.
*/
#include "Effect.h"
class Snake : public Effect {
private:
static const byte SNAKE_LENGTH = 16;
enum Direction {
UP, DOWN, LEFT, RIGHT
};
struct Pixel {
uint8_t x;
uint8_t y;
};
CRGB colours[SNAKE_LENGTH];
uint8_t initialHue;
Pixel pixels[SNAKE_LENGTH];
Direction direction;
void newDirection() {
switch (direction) {
case UP:
case DOWN:
direction = random(0, 2) == 1 ? RIGHT : LEFT;
break;
case LEFT:
case RIGHT:
direction = random(0, 2) == 1 ? DOWN : UP;
default:
break;
}
}
void shuffleDown() {
for (byte i = SNAKE_LENGTH - 1; i > 0; i--) {
pixels[i] = pixels[i - 1];
}
}
public:
Snake(CRGB *leds, int width, int height) : Effect(leds, width, height), initialHue(0) {
direction = UP;
for (int i = 0; i < SNAKE_LENGTH; i++) {
pixels[i].x = 0;
pixels[i].y = 0;
}
}
void start() {
clearLeds();
for (int frameNo = 0; frameNo < 1000; frameNo++) {
shuffleDown();
if (random(10) > 6) {
newDirection();
}
switch (direction) {
case UP:
pixels[0].y = (pixels[0].y + 1) % height;
break;
case LEFT:
pixels[0].x = (pixels[0].x + 1) % width;
break;
case DOWN:
pixels[0].y = pixels[0].y == 0 ? height - 1 : pixels[0].y - 1;
break;
case RIGHT:
pixels[0].x = pixels[0].x == 0 ? width - 1 : pixels[0].x - 1;
break;
}
fill_rainbow(colours, SNAKE_LENGTH, initialHue++);
for (byte i = 0; i < SNAKE_LENGTH; i++) {
pixel(pixels[i].x, pixels[i].y) = colours[i] %= (255 - i * (255 / SNAKE_LENGTH));
}
LEDS.show();
delay(30);
clearLeds();
}
}
};