Real-time color adjustment via variable resistor in the loop() function.

Good day,

I was wondering if somebody smarter than me could provide assistance in this issue. I’m working on a scoreboard thing.

Its just four digits controlled by a station w/ a seven-segment display and some buttons. But I also wanted to add some potentiometers so that the teams could pick their color on the big board. Another dude on this forum helped me tons in getting it set up to the point that it functions now.

It reads the color in the setup() function, to get the initial color, but then the same process repeats in the loop() function. Is it possible that the color could change in real-time? As of right now, it only changes when one of the buttons is pressed. That’s when the update occurs. But I thought loop() just kept going and going and going. I’m really knew to this kind of thing, so I appreciate the help.

When I was testing the prototype w/ a for loop that just counted 0-99, it worked properly while excecuting the loop. I just would like to see the change in real-time, rather than whenever a button is pressed.

Read stuff:

void loop() {
  valPotHome = analogRead(A2); //This is the part where most of my question takes place
  valPotAway = analogRead(A4);
  colorHome = map(valPotHome, 0, 1023, 0, 255); //reads value off POT so team can pick their color.  Put val in first value of CHSV
  colorAway = map(valPotAway, 0, 1023, 0, 255); //reads value off POT so team can pick their color.  Put val in first value of CHSV

Full code:

#include "FastLED.h"
#include "LedControl.h"

#define DATA_HomeRight    6
#define DATA_HomeLeft   8
#define DATA_AwayRight    10
#define DATA_AwayLeft   12
#define LED_TYPE    WS2811
#define COLOR_ORDER GRB
#define NUM_LEDS    15
#define NUM_CHAR 4
#define BRIGHTNESS  130

#define switchHomeUp 3
#define switchHomeDown 2
#define switchAwayUp 5
#define switchAwayDown 4
#define switchReset 7

CRGB leds[NUM_CHAR][NUM_LEDS];
LedControl lc = LedControl(9, 13, 11, 1); //  (DIN, CLK, LOAD, number of Max7219 chips)
//-----------------------------------------------------------------------------------
int score[][13] = {
  {0, 1, 2, 3, 5, 6, 8, 9, 11, 12, 13, 14, 14},
  {0, 5, 6, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12},
  {2, 1, 0, 5, 6, 7, 8, 9, 14, 13, 12, 14, 14},
  {2, 1, 0, 5, 6, 7, 11, 8, 12, 13, 14, 14, 14},
  {2, 0, 3, 8, 6, 7, 11, 5, 12, 12, 12, 12, 12},
  {0, 1, 2, 3, 8, 7, 6, 11, 12, 13, 14, 14, 14},
  {0, 1, 2, 3, 8, 9, 14, 13, 12, 11, 6, 7, 14},
  {2, 1, 0, 5, 7, 10, 13, 13, 13, 13, 13, 13, 13},
  {1, 13, 2, 12, 0, 14, 5, 9, 11, 3, 6, 8, 7},
  {0, 1, 5, 2, 3, 6, 8, 7, 11, 12, 12, 12, 12},
};
//-----------------------------------------------------------------------------------
uint8_t homescore = 0;  // keeps track of home team score to display
uint8_t awayscore = 0;  // keeps track of away team score to display
int rightdigit;
int leftdigit;
int colorHome;
int colorAway;
int valPotHome;
int valPotAway;

//---------------------------------------------------------------
void setup() {
  pinMode(switchHomeDown, INPUT_PULLUP);
  pinMode(switchHomeUp, INPUT_PULLUP);
  pinMode(switchAwayDown, INPUT_PULLUP);
  pinMode(switchAwayUp, INPUT_PULLUP);
  pinMode(switchReset, INPUT_PULLUP);

  FastLED.setBrightness(BRIGHTNESS);
  FastLED.clear();

  FastLED.addLeds<LED_TYPE, DATA_AwayRight, COLOR_ORDER>(leds[0], NUM_LEDS);
  FastLED.addLeds<LED_TYPE, DATA_AwayLeft, COLOR_ORDER>(leds[1], NUM_LEDS);
  FastLED.addLeds<LED_TYPE, DATA_HomeRight, COLOR_ORDER>(leds[2], NUM_LEDS);
  FastLED.addLeds<LED_TYPE, DATA_HomeLeft, COLOR_ORDER>(leds[3], NUM_LEDS);
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[0][i] = CRGB::Black;
    leds[1][i] = CRGB::Black;
    leds[2][i] = CRGB::Black;
    leds[3][i] = CRGB::Black;
  }

  valPotHome = analogRead(A2);
  valPotAway = analogRead(A4);

  colorHome = map(valPotHome, 0, 1023, 0, 255); //reads value off POT so team can pick their color.  Put C in first value of CHSV
  colorAway = map(valPotAway, 0, 1023, 0, 255); //reads value off POT so team can pick their color.  Put C in first value of CHSV

  ledcalculateHome();
  ledcalculateAway();

  lc.shutdown(0, false); // Wake up MAX7219
  lc.setIntensity(0, 6); // Set brightness to medium
  lc.clearDisplay(0);  // Clear all displays connected to MAX7219 chip #
  // Put zeros on both displays at startup
  lc.setDigit(0, 0, 0, false); // (Max7219 chip #, Digit, value, DP on or off)
  lc.setDigit(0, 1, 0, false);
  lc.setDigit(0, 4, 0, false);
  lc.setDigit(0, 5, 0, false);
}

//---------------------------------------------------------------
void loop() {
  valPotHome = analogRead(A2); //This is the part where most of my question takes place
  valPotAway = analogRead(A4);
  colorHome = map(valPotHome, 0, 1023, 0, 255); //reads value off POT so team can pick their color.  Put val in first value of CHSV
  colorAway = map(valPotAway, 0, 1023, 0, 255); //reads value off POT so team can pick their color.  Put val in first value of CHSV

  FastLED.show();
  delay(5);

  if (digitalRead(switchReset)) {
    homescore = 0;
    awayscore = 0;
    while (digitalRead(switchReset)) {
      delay(2); //delay to debounce switch
    }
    ledcalculateHome();
    ledcalculateAway();
  }

  if (digitalRead(switchHomeUp)) {
    homescore++;
    while (digitalRead(switchHomeUp)) {
      delay(2); //delay to debounce switch
    }
    ledcalculateHome();
  }

  if (digitalRead(switchHomeDown)) {
    homescore--;
    while (digitalRead(switchHomeDown)) {
      delay(2); //delay to debounce switch
    }
    ledcalculateHome();
  }
  if (digitalRead(switchAwayUp)) {
    awayscore++;
    while (digitalRead(switchAwayUp)) {
      delay(2); //delay to debounce switch
    }
    ledcalculateAway();
  }

  if (digitalRead(switchAwayDown)) {
    awayscore--;
    while (digitalRead(switchAwayDown)) {
      delay(2); //delay to debounce switch
    }
    ledcalculateAway();
  }

}

//-----------------------------------------------------------------ledcalculate Functions();
//HOME
void ledcalculateHome() {
  //valPotHome = analogRead(A2);
  //colorHome = map(valPotHome, 0, 1023, 0, 255); //reads value off POT so team can pick their color.  Put C in first value of CHSV

  rightdigit = homescore % 10;
  leftdigit = homescore % 100 / 10;

  lc.setDigit(0, 1, leftdigit, false);
  lc.setDigit(0, 0, rightdigit, false);

  for (int i = 0; i < NUM_LEDS; i++) {
    leds[0][i] = CRGB::Black;
    leds[1][i] = CRGB::Black;
  }

  for (int x = 0; x < 13; x++) {
    leds[0][score[rightdigit][x]] = CHSV(colorHome, 255, 255);
    leds[1][score[leftdigit][x]] = CHSV(colorHome, 255, 255);
    delay(10);
    FastLED.show();

  }

}
//AWAY
void ledcalculateAway() {
  //valPotAway = analogRead(A4);
  //colorAway = map(valPotAway, 0, 1023, 0, 255); //reads value off POT so team can pick their color.  Put C in first value of CHSV

  rightdigit = awayscore % 10;
  leftdigit = awayscore % 100 / 10;

  lc.setDigit(0, 5, leftdigit, false);
  lc.setDigit(0, 4, rightdigit, false);

  for (int i = 0; i < NUM_LEDS; i++) {
    leds[2][i] = CRGB::Black;
    leds[3][i] = CRGB::Black;
  }

  for (int x = 0; x < 13; x++) {
    leds[2][score[rightdigit][x]] = CHSV(colorAway, 255, 255);
    leds[3][score[leftdigit][x]] = CHSV(colorAway, 255, 255);
    delay(10);
    FastLED.show();
  }

}

Some pictures if it helps : Imgur: The magic of the Internet

Thank you very much for your time.

The color gets set in the ledcalculateHome() and ledcalculateAway() functions. Inside your loop(), you only call these functions if one of the buttons is pressed. If you want it to update "real-time", then you will have to call these functions every time through the loop(), not inside if() statements within the loop().

Doing it as quickly as loop() can loop may be too quick and possible cause flickering or something. You might have to only call them every 100msec or so... Look at the BlinkWithoutDelay example to see how to time things.

I see what you mean as that probably being a contributing factor, but I've got the color portions inside the functions commented out, so that it would only pull the values in the setup and then the loop functions.

Thank you for helping me out.

One change that I've made is taking the ledcalculateHome() and ...Away() functions out of each of the if statements. So that maybe improved the quality of the program, but still, I can only see the color change if a button is pressed. Even if I move the FastLED.show() to the bottom or top of the loop() area.

Meh, I'll just keep playing around. Learning about this kind of thing is a fun way of thinking about stuff. I'll definitely check out the recommended BlinkWithoutDelay example. Thank you, once more.

This code reads the potentiometers every time through the loop, maps the value to 0-255 and then updates the display, regardless of button presses.

#include "FastLED.h"
#include "LedControl.h"

#define DATA_HomeRight    6
#define DATA_HomeLeft   8
#define DATA_AwayRight    10
#define DATA_AwayLeft   12
#define LED_TYPE    WS2811
#define COLOR_ORDER GRB
#define NUM_LEDS    15
#define NUM_CHAR 4
#define BRIGHTNESS  130

#define switchHomeUp 3
#define switchHomeDown 2
#define switchAwayUp 5
#define switchAwayDown 4
#define switchReset 7

CRGB leds[NUM_CHAR][NUM_LEDS];
LedControl lc = LedControl(9, 13, 11, 1); //  (DIN, CLK, LOAD, number of Max7219 chips)
//-----------------------------------------------------------------------------------
int score[][13] = {
  {0, 1, 2, 3, 5, 6, 8, 9, 11, 12, 13, 14, 14},
  {0, 5, 6, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12},
  {2, 1, 0, 5, 6, 7, 8, 9, 14, 13, 12, 14, 14},
  {2, 1, 0, 5, 6, 7, 11, 8, 12, 13, 14, 14, 14},
  {2, 0, 3, 8, 6, 7, 11, 5, 12, 12, 12, 12, 12},
  {0, 1, 2, 3, 8, 7, 6, 11, 12, 13, 14, 14, 14},
  {0, 1, 2, 3, 8, 9, 14, 13, 12, 11, 6, 7, 14},
  {2, 1, 0, 5, 7, 10, 13, 13, 13, 13, 13, 13, 13},
  {1, 13, 2, 12, 0, 14, 5, 9, 11, 3, 6, 8, 7},
  {0, 1, 5, 2, 3, 6, 8, 7, 11, 12, 12, 12, 12},
};
//-----------------------------------------------------------------------------------
uint8_t homescore = 0;  // keeps track of home team score to display
uint8_t awayscore = 0;  // keeps track of away team score to display
int rightdigit;
int leftdigit;
int colorHome;
int colorAway;
int valPotHome;
int valPotAway;

//---------------------------------------------------------------
void setup() {
  pinMode(switchHomeDown, INPUT_PULLUP);
  pinMode(switchHomeUp, INPUT_PULLUP);
  pinMode(switchAwayDown, INPUT_PULLUP);
  pinMode(switchAwayUp, INPUT_PULLUP);
  pinMode(switchReset, INPUT_PULLUP);

  FastLED.setBrightness(BRIGHTNESS);
  FastLED.clear();

  FastLED.addLeds<LED_TYPE, DATA_AwayRight, COLOR_ORDER>(leds[0], NUM_LEDS);
  FastLED.addLeds<LED_TYPE, DATA_AwayLeft, COLOR_ORDER>(leds[1], NUM_LEDS);
  FastLED.addLeds<LED_TYPE, DATA_HomeRight, COLOR_ORDER>(leds[2], NUM_LEDS);
  FastLED.addLeds<LED_TYPE, DATA_HomeLeft, COLOR_ORDER>(leds[3], NUM_LEDS);
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[0][i] = CRGB::Black;
    leds[1][i] = CRGB::Black;
    leds[2][i] = CRGB::Black;
    leds[3][i] = CRGB::Black;
  }

  valPotHome = analogRead(A2);
  valPotAway = analogRead(A4);

  colorHome = map(valPotHome, 0, 1023, 0, 255); //reads value off POT so team can pick their color.  Put C in first value of CHSV
  colorAway = map(valPotAway, 0, 1023, 0, 255); //reads value off POT so team can pick their color.  Put C in first value of CHSV

  ledcalculateHome();
  ledcalculateAway();

  lc.shutdown(0, false); // Wake up MAX7219
  lc.setIntensity(0, 6); // Set brightness to medium
  lc.clearDisplay(0);  // Clear all displays connected to MAX7219 chip #
  // Put zeros on both displays at startup
  lc.setDigit(0, 0, 0, false); // (Max7219 chip #, Digit, value, DP on or off)
  lc.setDigit(0, 1, 0, false);
  lc.setDigit(0, 4, 0, false);
  lc.setDigit(0, 5, 0, false);
}

//---------------------------------------------------------------
void loop() {
  valPotHome = analogRead(A2); //This is the part where most of my question takes place
  valPotAway = analogRead(A4);
  colorHome = map(valPotHome, 0, 1023, 0, 255); //reads value off POT so team can pick their color.  Put val in first value of CHSV
  colorAway = map(valPotAway, 0, 1023, 0, 255); //reads value off POT so team can pick their color.  Put val in first value of CHSV

  ledcalculateHome();
  ledcalculateAway();
  FastLED.show();
  delay(5);

  if (digitalRead(switchReset)) {
    homescore = 0;
    awayscore = 0;
    while (digitalRead(switchReset)) {
      delay(2); //delay to debounce switch
    }
    ledcalculateHome();
    ledcalculateAway();
  }

  if (digitalRead(switchHomeUp)) {
    homescore++;
    while (digitalRead(switchHomeUp)) {
      delay(2); //delay to debounce switch
    }
    ledcalculateHome();
  }

  if (digitalRead(switchHomeDown)) {
    homescore--;
    while (digitalRead(switchHomeDown)) {
      delay(2); //delay to debounce switch
    }
    ledcalculateHome();
  }
  if (digitalRead(switchAwayUp)) {
    awayscore++;
    while (digitalRead(switchAwayUp)) {
      delay(2); //delay to debounce switch
    }
    ledcalculateAway();
  }

  if (digitalRead(switchAwayDown)) {
    awayscore--;
    while (digitalRead(switchAwayDown)) {
      delay(2); //delay to debounce switch
    }
    ledcalculateAway();
  }

}

//-----------------------------------------------------------------ledcalculate Functions();
//HOME
void ledcalculateHome() {
  //valPotHome = analogRead(A2);
  //colorHome = map(valPotHome, 0, 1023, 0, 255); //reads value off POT so team can pick their color.  Put C in first value of CHSV

  rightdigit = homescore % 10;
  leftdigit = homescore % 100 / 10;

  lc.setDigit(0, 1, leftdigit, false);
  lc.setDigit(0, 0, rightdigit, false);

  for (int i = 0; i < NUM_LEDS; i++) {
    leds[0][i] = CRGB::Black;
    leds[1][i] = CRGB::Black;
  }

  for (int x = 0; x < 13; x++) {
    leds[0][score[rightdigit][x]] = CHSV(colorHome, 255, 255);
    leds[1][score[leftdigit][x]] = CHSV(colorHome, 255, 255);
    delay(10);
    FastLED.show();

  }

}
//AWAY
void ledcalculateAway() {
  //valPotAway = analogRead(A4);
  //colorAway = map(valPotAway, 0, 1023, 0, 255); //reads value off POT so team can pick their color.  Put C in first value of CHSV

  rightdigit = awayscore % 10;
  leftdigit = awayscore % 100 / 10;

  lc.setDigit(0, 5, leftdigit, false);
  lc.setDigit(0, 4, rightdigit, false);

  for (int i = 0; i < NUM_LEDS; i++) {
    leds[2][i] = CRGB::Black;
    leds[3][i] = CRGB::Black;
  }

  for (int x = 0; x < 13; x++) {
    leds[2][score[rightdigit][x]] = CHSV(colorAway, 255, 255);
    leds[3][score[leftdigit][x]] = CHSV(colorAway, 255, 255);
    delay(10);
    FastLED.show();
  }

}

That makes perfect sense. Thank you very much for your help.