I'm using UNO R3 & WS2812B ledstrip.
I want to control LED's hue by an external potentiometer.
So I wrote a following code. But it doesn't work.
If "USE_POT" was commentouted, it works fine and I could check hue value from serial monitor.
What am I doing wrong?
#include "FastLED.h"
// if defined, hue is got from POT by using analogRead().
#define USE_POT
#define DATA_PIN 11
#define NUM_LEDS 60
CRGB leds[NUM_LEDS];
int potPin = 0; // Pin for potentiometer
int potVal;
uint8_t hue;
//---------------------------------------------------------------
void setup() {
delay(200);
pinMode(potPin, INPUT); // Set pin as an input.
// for debug
Serial.begin(9600); // Allows serial monitor output (check baud rate)
hue = 128;
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
FastLED.clear();
FastLED.show();
delay(500);
}
//---------------------------------------------------------------
void loop() {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(hue, 255, 64); // hue comes from pot A, and brightness value is scaled based on pot B.
}
FastLED.show();
Serial.println("FastLED has called");
delay(500);
#ifdef POT_USE
//-- use analogread, NOT OWRK
potVal = analogRead(potPin);
#else
//-- WORK FINE
potVal = (int)random(0, 1023);
#endif
hue = map(potVal, 0, 1023, 0, 255);
Serial.println("chekKnobs has called");
Serial.print("pot: "); Serial.print(potVal); Serial.print(" hue: "); Serial.println(hue);
delay(500);
}
As already stated, be sure you have the correct input pin.
The pot should be connected as below:
One end of the pot => 5V
the wiper (center) => analog input pin
Other end of the pot => gnd
Be sure you have this correct!! If you accidentally reverse the wiper and ground you will damage the pot.
Do you have a multimeter? If not I would suggest you look into getting one. Even a cheap one would help in this instance.
If you do, verify the analog input pin has a voltage you can set with the pot.
#ifdef USE_POT
//-- use analogread, NOT WORK
potVal = analogRead(potPin);
potVal = 42;
#else
//-- WORK FINE
potVal = (int)random(0, 1023);
#endif
And it worked fine at a steady red.
I haven't got a pot wired up at the moment, but as you can see the analogRead function is being called. Therefore I suggest your pot has not been wired up correctly.
Yes, it did not produce a steady red.
And when I commented out analogRead(), it produced a steady red!
It's strange that calling analogRead() affects FastLED's functions.
Grumpy_Mike:
Well strange is not the word for it. I think you are missing something but I don't know what. Can you post a picture of your wiring please.
Those links do not work when you click on them, they are just blank but the label is valid. What ever you do when you post them you are doing it incorrectly.
Yes we need to see the wiring. In particular the common ground between the LED's power supply and the Arduino.
Just did a video of what I get when I use your code on my system.
See comments in the video.
If you set the hue first, then set and show the LEDs, then delay, you will not get a delay between turning the knob and seeing the colours change.