So I've got an odd issue, or one that I don't have a clear answer for.
My code works exactly how I think it should on the bread board:
1)Turn On a set of NeoPixels
2) Adjust color using a rotary encoder.
- This is through the "check color state" function below which I think is where the issue is?
3) Change patterns through pressing the button of the encoder, while still allowing the color to change from the rotary encoder.
The general run of the loop is:
- Run Effect - Run one of 12 cases of a switch fuctions
- btn. tick - Check for Button Presses to advance case number
- Every 10 mSec, check for a state change on the rotarty encoder to adjust hue value
- Push the output information to the LEDs
- Repeat
When I got it all to a satisfactory level to test on the final product - a light up dog bone - everything worked except step 2, the color adjustment.
I changed the button input to both read one of the two rotary inputs, and then the other, and both times the pattern did change when the rotary encoder was turned, so the board is reading both pins as far as I can tell.
If anyone has any ideas/input I'd greatly appreciate it as this is my first larger project and I've hit a wall troubleshooting farther.
Code is below, as well as a schematic I drew of the bread board, and then the schematic my friend made for the final product.
This is also a video showing intended function vs what is happening of them side by side. The bread board one goes through red, while the other one doesn't change color.
//Change Max Brightness Here
int Brightness = 30;
//Total Case Total Here
int caseTotal = 13;
//Hue 0-255 at Turn on
//0-Red, 32-Orange, 64-Yellow, 96-Green, 128-Cyan, 160-Blue, 192-Purple, 224-Magenta,
int StartColor = 160;
#include <FastLED.h>
#include <OneButton.h>
//encoder Input
#define SPIN_CCW 0
#define SPIN_CW 1
// LED Outputs
#define NUM_LEDS 32
#define LED_PIN 4
int CHASE_LEDS = 26; // number of LEDs around edges of dog bone
//Button Input
#define BTN_PIN 2
int patternCounter = 0;
OneButton btn = OneButton(BTN_PIN, true, true);
int randomCounter = 0;
//Color Variables
int currentStateCLK;
int lastSpinState;
uint8_t hue = StartColor; //Hue
uint8_t sat = 0; //Saturation
uint8_t val = 0; // Value/Brightness to maximum brightness defined above
uint8_t r = 0; // rainbow chase loop
CRGB leds[NUM_LEDS];
uint8_t hueRainbow = 0;
// fillPixel Variables
int fillPixelLoopCounter = 0;
int fillPixelCounter = 0;
int fillPixelBlackCoutner = NUM_LEDS;
int pixelVal = 255;
////DELETE THIS AT FINAL
//#define ledCW 8
//#define ledCCW 9
//// DELETE THIS AT FINAL ^
void setup() {
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(Brightness); //Sets Maximum Brightness - any in effect brightness @ 255 maxes to this
// Set encoder pins as inputs
pinMode (SPIN_CCW, INPUT);
pinMode (SPIN_CW, INPUT);
// Read the initial state of SPIN_CCW
// Assign to lastSpinState variable
lastSpinState = digitalRead(SPIN_CCW);
btn.attachClick(nextPattern);
}
void loop() {
RunEffect();
btn.tick();
EVERY_N_MILLISECONDS(10) {
checkColorState();
}
FastLED.show();
}
void checkColorState() {
// EVERY_N_MILLISECONDS(20) { // Read the current state of SPIN_CCW
currentStateCLK = digitalRead(SPIN_CCW);
// If the previous and the current state of the SPIN_CCW are different then a pulse has occured
if (currentStateCLK != lastSpinState) {
// If the SPIN_CW state is different than the SPIN_CCW state then
// the encoder is rotating counterclockwise
if (digitalRead(SPIN_CW) != currentStateCLK) {
hue = hue - 2;
// digitalWrite(ledCW, LOW);
// digitalWrite(ledCCW, HIGH);
} else {
// Encoder is rotating clockwise
hue = hue + 2;
// digitalWrite(ledCW, HIGH);
// digitalWrite(ledCCW, LOW);
}
// delay (20);
// Serial.println(hue);
// }
}
// Update lastSpinState with the current state
lastSpinState = currentStateCLK;
}
void nextPattern() {
patternCounter = (patternCounter + 1);
if (patternCounter > caseTotal) {
patternCounter = 0;
}
}
//case 0
void solidFill(){
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(hue, 255, 255);
}
}
//case 1
void Horizontal32BPM() {
//Side Wipe back and Forth 128BPM Quater Speed
uint8_t sinBeat4 = beatsin8(32, 0, CHASE_LEDS - 1, 0, 0);
uint8_t sinBeat5 = beatsin8(32, 0, CHASE_LEDS - 1, 0, 122);
uint8_t sinBeat6 = beatsin8(64, 0, 4, 0, 50);
leds[sinBeat4] = CHSV(hue, 255, 255);
leds[sinBeat5] = CHSV(hue, 255, 255);
leds[26 + sinBeat6] = CHSV(hue, 255, 255);
EVERY_N_MILLISECONDS(40);
fadeToBlackBy(leds, NUM_LEDS, 02);
}
//case 2
void Horizontal64BPM() {
//Side Wipe back and Forth 128BPM Quater Speed
uint8_t sinBeat4 = beatsin8(64, 0, CHASE_LEDS - 1, 0, 0);
uint8_t sinBeat5 = beatsin8(64, 0, CHASE_LEDS - 1, 0, 122);
uint8_t sinBeat6 = beatsin8(128, 0, 4, 0, 50);
leds[sinBeat4] = CHSV(hue, 255, 255);
leds[sinBeat5] = CHSV(hue, 255, 255);
leds[26 + sinBeat6] = CHSV(hue, 255, 255);
EVERY_N_MILLISECONDS(40);
fadeToBlackBy(leds, NUM_LEDS, 05);
}
//case 3
void waterfall() {
// Serial.println("Waterfall start");
uint8_t valBeat = beatsin8(64, 0, 255, 0, 0);
uint8_t valBeat2 = beatsin8(64, 0, 255, 0, 225);
uint8_t valBeat3 = beatsin8(64, 0, 255, 0, 180);
uint8_t valBeat4 = beatsin8(64, 0, 255, 0, 144);
uint8_t valBeat5 = beatsin8(64, 0, 255, 0, 108);
uint8_t valBeat6 = beatsin8(64, 0, 255, 0, 72);
uint8_t valBeat7 = beatsin8(64, 0, 255, 0, 36);
// First Row
// Serial.println("Waterfall - 1st row");
for (int i = 2; i < 4; i++) { //i can only be a single integer, so previous line selects which pixels
leds[i] = CHSV(hue, 255, valBeat);
}
for (int i = 9; i < 11; i++) { //Top right of bone since they're non-contiguous numbers
leds[i] = CHSV(hue, 255, valBeat);
}
//Second Row
// Serial.println("Waterfall - 2nd row");
for (int i = 4; i < 9; i++)
leds[i] = CHSV(hue, 255, valBeat2);
//Third Row
// Serial.println("Waterfall - 3rd row");
for (int i = 0; i < 2; i++) {
leds[i] = CHSV(hue, 255, valBeat3);
}
for (int i = 11; i < 13; i++) {
leds[i] = CHSV(hue, 255, valBeat3);
}
//Fourth Row
// Serial.println("Waterfall - 4th row");
for (int i = 26; i < 31; i++)
leds[i] = CHSV(hue, 255, valBeat4);
//Fifth Row
// Serial.println("Waterfall - 5th row");
for (int i = 24; i < 26; i++) {
leds[i] = CHSV(hue, 255, valBeat5);
}
for (int i = 13; i < 15; i++) {
leds[i] = CHSV(hue, 255, valBeat5);
}
//Sixth Row
// Serial.println("Waterfall - 6th row");
// }
for (int i = 17; i < 22; i++) {
leds[i] = CHSV(hue, 255, valBeat6);
}
//Seventh Row
// Serial.println("Waterfall - 7th row");
for (int i = 15; i < 17; i++) {
leds[i] = CHSV(hue, 255, valBeat7);
}
for (int i = 22; i < 24; i++) { //Top right of bone since they're non-contiguous numbers
leds[i] = CHSV(hue, 255, valBeat7);
}
btn.tick();
}
//caase 4
void flashBone() {
int flashVal = beat8(48, 0);
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(hue, 255, flashVal);
}
}
//case 5 - To be Written
//case 6
void sawtoothChase() {
//Perimeter Chase
int saw1 = beat8(64, 0);
int saw2 = beat8(64, 500);
uint8_t sawi = map(saw1, 0, 255, 0, CHASE_LEDS - 1);
uint8_t sawi2 = map(saw2, 0, 255, 0, CHASE_LEDS - 1);
leds[sawi] = CHSV(hue, 255, 255);
leds[sawi2] = CHSV(hue, 255, 255);
EVERY_N_MILLISECONDS(40);
fadeToBlackBy(leds, NUM_LEDS, 05);
}
//case 7 - Work in progress and not looking for help on this currently
void fillPixels() {
// int pixelVal;
int x;
if (fillPixelLoopCounter = NUM_LEDS) { // If total number of loops through is equal to total num of LEDs, then change the fill Pixel Counter
x = 1;
fillPixelBlackCoutner = 32;
if (pixelVal = 255) {
pixelVal = 0;
hue = 0;
}
else {
pixelVal = 255;
}
}
int saw = beat8(32, 0); // What pixel should be on
uint8_t sawi = map(saw, 0, 255, 0, NUM_LEDS - x); // used with x and
leds[sawi] = CHSV(hue, 255, pixelVal); // Turns on LEDs
if (sawi = fillPixelBlackCoutner) { /// Moves the end of the t
x++;
fillPixelBlackCoutner--;
}
if (pixelVal = 255) { //if Pixel are turning on, then turn on
fadeToBlackBy(leds, fillPixelBlackCoutner, 04);
}
fillPixelLoopCounter++; // Add 1 to loop coutner
}
//Case 8
void randomPixel() {
EVERY_N_MILLISECONDS(30) {
int randomi = random8(0, 30);
leds [randomi] = CHSV(hue, 255, 255);
randomCounter++;
fadeToBlackBy(leds, NUM_LEDS, 10);
}
}
//Case 9
void randomPixelFaster() {
EVERY_N_MILLISECONDS(15) {
int randomi = random8(0, 30);
leds [randomi] = CHSV(hue, 255, 255);
randomCounter++;
fadeToBlackBy(leds, NUM_LEDS, 15);
}
}
//case 10
void rainbowChase() {
uint8_t rainbowHue = beat8(32, 0); //(beat8( BPM, time offeset mSeconds)
for (int i = 26; i < NUM_LEDS; i++) {
leds[i] = CHSV(rainbowHue, 255, 255);
}
EVERY_N_MILLISECONDS(5) {
r++;
fill_rainbow(leds, CHASE_LEDS, r, 255 / CHASE_LEDS);
}
}
//case 11
void rainbowFillChase() {
EVERY_N_MILLISECONDS(14) {
r++;
fill_rainbow(leds, NUM_LEDS, r, 0);
}
}
//case 12
void rainbowWaterfall() {
// Serial.println("Waterfall start");
uint8_t hue2 = beatsin8(32, 0, 255, 0, 0);
uint8_t valBeat = beatsin8(64, 0, 255, 0, 0);
uint8_t valBeat2 = beatsin8(64, 0, 255, 0, 225);
uint8_t valBeat3 = beatsin8(64, 0, 255, 0, 180);
uint8_t valBeat4 = beatsin8(64, 0, 255, 0, 144);
uint8_t valBeat5 = beatsin8(64, 0, 255, 0, 108);
uint8_t valBeat6 = beatsin8(64, 0, 255, 0, 72);
uint8_t valBeat7 = beatsin8(64, 0, 255, 0, 36);
// First Row
// Serial.println("Waterfall - 1st row");
for (int i = 2; i < 4; i++) { //i can only be a single integer, so previous line selects which pixels
leds[i] = CHSV(hue2, 255, valBeat);
}
for (int i = 9; i < 11; i++) { //Top right of bone since they're non-contiguous numbers
leds[i] = CHSV(hue2, 255, valBeat);
}
//Second Row
// Serial.println("Waterfall - 2nd row");
for (int i = 4; i < 9; i++)
leds[i] = CHSV(hue2, 255, valBeat2);
//Third Row
// Serial.println("Waterfall - 3rd row");
for (int i = 0; i < 2; i++) {
leds[i] = CHSV(hue2, 255, valBeat3);
}
for (int i = 11; i < 13; i++) {
leds[i] = CHSV(hue2, 255, valBeat3);
}
//Fourth Row
// Serial.println("Waterfall - 4th row");
for (int i = 26; i < 31; i++)
leds[i] = CHSV(hue2, 255, valBeat4);
//Fifth Row
// Serial.println("Waterfall - 5th row");
for (int i = 24; i < 26; i++) {
leds[i] = CHSV(hue2, 255, valBeat5);
}
for (int i = 13; i < 15; i++) {
leds[i] = CHSV(hue2, 255, valBeat5);
}
//Sixth Row
// Serial.println("Waterfall - 6th row");
// }
for (int i = 17; i < 22; i++) {
leds[i] = CHSV(hue2, 255, valBeat6);
}
//Seventh Row
// Serial.println("Waterfall - 7th row");
for (int i = 15; i < 17; i++) {
leds[i] = CHSV(hue2, 255, valBeat7);
}
for (int i = 22; i < 24; i++) { //Top right of bone since they're non-contiguous numbers
leds[i] = CHSV(hue2, 255, valBeat7);
}
btn.tick();
}
void RunEffect() {
// get current button state
switch (patternCounter) {
case 0:
solidFill();
break;
case 1:
Horizontal32BPM();
break;
case 2:
Horizontal64BPM();
break;
case 3:
waterfall();
break;
case 4:
flashBone();
break;
case 5:
// blank - to be written
patternCounter++;
break;
case 6:
sawtoothChase();
break;
case 7:
fillPixels();
break;
case 8:
randomPixel();
break;
case 9:
randomPixelFaster();
break;
case 10:
rainbowChase();
break;
case 11:
rainbowFillChase();
break;
case 12:
rainbowWaterfall();
break;
}
}