Hi, I have 5 leds and a strip of dotstar and I’m using arduino uno, I’m trying to make one button cycle between 2 modes with a second button for mode 3 only when held down,
mode 1: leds and dotstar doing animations at the same time
mode 2: leds doing an animation while the dotstar is a vu meter
mode 3(while holding down the interrupt button): leds and dotstar will do different animations as mode 1
I first wrote the code for the 2 modes without interrupt, all the animations and vu meter work as expected. With the interrupt code, once first powered on(which is in mode 1), the leds and dotstar animations work, but if I hold the interrupt button, the leds would pause on its mode 1 animation, and the dotstar will pause on its mode 3 animation.
In mode2 the vu meter doesn’t work(the dotstar would not light up), and after that the dotstar will not light up again no matter how many times I press the mode button. Holding the interrupt button still pause the leds in its current mode.
Please let me know what I did wrong and how I can properly do this, thank you.
The code is too long so I have to split it.
//led
//------------------------------------------------------------------------
const byte ledPins[] = {8,9,10,11,12};
const byte number_of_leds = sizeof(ledPins);
unsigned long previousMillis11[number_of_leds] = {};
unsigned long previousMillis21[number_of_leds] = {};
unsigned long previousMillis31[number_of_leds] = {};
byte indexes11[number_of_leds] = {0};
byte indexes21[number_of_leds] = {0};
byte indexes31[number_of_leds] = {0};
const byte max_number_of_onOff11 = 8;
const byte max_number_of_onOff21 = 11;
const byte max_number_of_onOff31 = 11;
int FR11 = 200;
int FR21 = 100;
int FR31 = 500;
byte on = HIGH;
byte off = LOW;
//dotstar
//------------------------------------------------------------------------------
#include <FastLED.h>
#define NUM_LEDS 5
#define DATA_PIN 6
#define CLOCK_PIN 7
#define COLOR_ORDER BGR
CRGB DOT[NUM_LEDS];
int FR12 = 200;
int FR32 = 500;
unsigned long previousMillis12[NUM_LEDS] = {};
unsigned long previousMillis32[NUM_LEDS] = {};
byte indexes12[NUM_LEDS] = {0};
byte indexes32[NUM_LEDS] = {0};
const byte MAX_NUM_OF_PERIODS12 = 7;
const byte MAX_NUM_OF_PERIODS32 = 7;
long RED12[][MAX_NUM_OF_PERIODS12] =
{
{255, 0, 0, 0, 0, -1},
{0, 0, 0, 0, 0, -1},
{0, 0, 0, 0, 0, -1},
{0, 0, 0, 255, 0, -1},
{0, 0, 0, 0, 0, -1},
};
long GREEN12[][MAX_NUM_OF_PERIODS12] =
{
{0, 0, 0, 0, 0, -1},
{0, 255, 0, 0, 0, -1},
{0, 0, 0, 0, 0, -1},
{0, 0, 0, 150, 0, -1},
{0, 0, 0, 0, 150, -1},
};
long BLUE12[][MAX_NUM_OF_PERIODS12] =
{
{0, 0, 0, 0, 0, -1},
{0, 0, 0, 0, 0, -1},
{0, 0, 255, 0, 0, -1},
{0, 0, 0, 0, 0, -1},
{0, 0, 0, 0, 255, -1},
};
long RED32[][MAX_NUM_OF_PERIODS32] =
{
{250, 3, 246, 5, 45, -1},
{44, 53, 209, 53, 5, -1},
{2, 251, 44, 206, 3, -1},
{3, 248, 6, 246, 207, -1},
{53, 209, 4, 209, 246, -1},
};
long GREEN32[][MAX_NUM_OF_PERIODS32] =
{
{96, 159, 19, 235, 252, -1},
{252, 7, 240, 7, 235, -1},
{252, 2, 252, 2, 47, -1},
{160, 96, 234, 18, 2, -1},
{7, 240, 47, 240, 19, -1},
};
long BLUE32[][MAX_NUM_OF_PERIODS32] =
{
{3, 14, 96, 250, 249, -1},
{250, 249, 161, 3, 3, -1},
{18, 3, 14, 234, 248, -1},
{233, 249, 240, 19, 3, -1},
{13, 233, 248, 239, 18, -1},
};
//dotstar vu meter
//---------------------------------------------------------------------------------
#define MIC_PIN A5
#define SAMPLE_WINDOW 2
#define PEAK_HANG 5
#define PEAK_FALL 1
#define INPUT_FLOOR 100
#define INPUT_CEILING 450
byte peak = 16;
unsigned int sample;
byte dotCount = 0;
byte dotHangCount = 0;
//buttons and modes
//------------------------------------------------------------------------------
int buttonPin = 1;
int stopPin = 2;
int val;
int val2;
int buttonState;
int LEDMode;
void setup() {
for(int led = 0; led < number_of_leds; led++) {
pinMode(ledPins[led], OUTPUT);
}
pinMode(buttonPin, INPUT);
buttonState = digitalRead(buttonPin);
pinMode(stopPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(stopPin), MODE3, LOW);
FastLED.addLeds<DOTSTAR, DATA_PIN, CLOCK_PIN, BGR> (DOT, NUM_LEDS);
FastLED.setBrightness(5);
}
void loop() {
val = digitalRead(buttonPin);
delay(10);
val2 = digitalRead(buttonPin);
if (val == val2) {
if(val != buttonState){
if (val == LOW) {
if (LEDMode == 0) {
LEDMode = 1;
} else {
if (LEDMode == 1) {
LEDMode = 0;
}
}
}
}
}
buttonState = val;
if (LEDMode == 0) {
MODE1();
}
if (LEDMode == 1) {
MODE2();
}
}
void MODE1() {
//led mode1
//--------------------------------------------------------------------------------
long onOff11[][max_number_of_onOff11] =
{
{on, off, off, off, off, -1},
{off, on, off, off, off, -1},
{off, off, on, off, off, -1},
{off, off, off, on, off, -1},
{off, off, off, off, on, -1},
};
unsigned long currentMillis = millis();
for(int led = 0; led < number_of_leds; led++) {
if(currentMillis - previousMillis11[led] >= FR11) {
digitalWrite(ledPins[led], onOff11[led][indexes11[led]]);
previousMillis11[led] = currentMillis;
indexes11[led]++;
}
if (onOff11[led][indexes11[led]] == -1) {
indexes11[led] = 0;
}
}
//dot star mode1
//-------------------------------------------------------------------------
for(int LED = 0; LED < NUM_LEDS; LED++) {
if(currentMillis - previousMillis12[LED] >= FR12) {
DOT[LED] = CRGB(RED12[LED][indexes12[LED]], GREEN12[LED][indexes12[LED]],BLUE12[LED][indexes12[LED]]);
FastLED.show();
previousMillis12[LED] = currentMillis;
indexes12[LED]++;
}
if (BLUE12[LED][indexes12[LED]] == -1) {
indexes12 [LED] = 0;
}
if (GREEN12[LED][indexes12[LED]] == -1) {
indexes12 [LED] = 0;
}
if (RED12[LED][indexes12[LED]] == -1) {
indexes12 [LED] = 0;
}
}
}