hey.
so i have a led strip running a left to right chase and need a pot to control the rate of the chase and dont know a way to do it. am using neopixle to control the strip
could anybody help i can post my code for strip if needed
thanks
hey.
so i have a led strip running a left to right chase and need a pot to control the rate of the chase and dont know a way to do it. am using neopixle to control the strip
could anybody help i can post my code for strip if needed
thanks
Read the pot (there is at least one example for analogRead in the IDE) and use the value for a delay.
You will probably need to rework your sketch to get rid of delay() in favour of millis() if you want an instant reaction on the changes.
Post your code; just in case you haven't done so yet, please read How to use this forum - please read. - Installation & Troubleshooting - Arduino Forum and pay special attention to point #7 about posting code using code tags.
There is something in your sketch that sets the rate at which the animation moves. That something is probably based on a numeric value. Replace the numeric value with a formula based on the analogRead() result.
I'd have to see your sketch to be more specific.
#include <Adafruit_NeoPixel.h>
#define N_LEDS 44
#define PIN 1
Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_LEDS, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
strip.begin();
}
int pos = 0, dir = 1; // Position, direction of "eye"
void loop() {
int j;
// Draw 5 pixels centered on pos. setPixelColor() will clip any
// pixels off the ends of the strip, we don't need to watch for that.
strip.setPixelColor(pos - 1, 0x2000); // Green
strip.setPixelColor(pos - 2, 0x2000); // Green
strip.show();
delay(20);
// Rather than being sneaky and erasing just the tail pixel,
// it's easier to erase it all and draw a new one next time.
for(j=-2; j<= 2; j++) strip.setPixelColor(pos+j, 0);
// Bounce off ends of strip
pos += dir;
if(pos < 0) {
pos = 1;
dir = -dir;
} else if(pos >= strip.numPixels()) {
pos = strip.numPixels() - 2;
dir = -dir;
}
}
johnwasser:
There is something in your sketch that sets the rate at which the animation moves. That something is probably based on a numeric value. Replace the numeric value with a formula based on the analogRead() result.I'd have to see your sketch to be more specific.
Sketch in Chat
johnwasser:
There is something in your sketch that sets the rate at which the animation moves.
Do you see where your sketch says:
strip.show();
delay(20);
That's the 'something'.
johnwasser:
That something is probably based on a numeric value.
The '20' in 'delay(20);' is that numeric value.
johnwasser:
Replace the numeric value with a formula based on the analogRead() result.
Maybe something like:
strip.show();
int speed = (analogRead(A0) * 50) / 1024; // Value from 0 to 50
delay(speed);
Also worth mentioning, John and joegie, that you need to set your pot up with a pullup resistor to get it to read acceptably.
either external 10-20kOhm pullup resistor or internal pullup. pinMode(A0,INPUT_PULLUP);
Andrew_F_In_Australia:
Also worth mentioning, John and joegie, that you need to set your pot up with a pullup resistor to get it to read acceptably.either external 10-20kOhm pullup resistor or internal pullup. pinMode(A0,INPUT_PULLUP);
Sorry, but no. The potentiometer acts as both sides of a voltage divider. It does not use a pull-up or pull-down resistor. You may be thinking of a resistive sensor like a thermistor or a Light Dependent Resistor (LDR).
You should not use pinMode() on an analog input pin that you are using for analogRead(). Use pinMode() on pins that you are using for digitalRead() {INPUT or INPUT_PULLUP} or digitalWrite() {OUTPUT}.
I was thinking of an LDR and misread.
Thanks John, you are correct.
A
edit: I use pinMode(A?,INPUT_PULLUP) on analog pins with the LDR in preference to an external pullup resistor to create a voltage divider. Not to hijack this, but is there a reason why you can not declare an internal pullup on an analogue pin? Just enquiring.
Andrew_F_In_Australia:
edit: I use pinMode(A?,INPUT_PULLUP) on analog pins with the LDR in preference to an external pullup resistor to create a voltage divider. Not to hijack this, but is there a reason why you can not declare an internal pullup on an analogue pin? Just enquiring.
Just be very careful NOT to try that on Pin A7 of a Nano. The tables that pinMode() uses to look up port numbers and pin masks only go up to Pin 19 (A5). Pins higher than 19 will return data from off the end of the table.
Pin 20 (A6) returns Port 1 (A), pin mask 50. Since the UNO/Nano has no Port A nothing will happen.
Pin 21 (A7) however returns Port 2 (B) and pin mask 3. Since Port B exists you will be setting the pinmode on pins 8 and 9 (PORTB pins 0 and 1), not on pin A7.
so do i power the pot useing the 5v and ground
cheers for all your help
joegie:
so do i power the pot useing the 5v and ground
Yes. Just like it says in File->Examples->03.Analog->AnalogInput:
The circuit:
- potentiometer
center pin of the potentiometer to the analog input 0
one side pin (either one) to ground
the other side pin to +5V
johnwasser:
Just be very careful NOT to try that on Pin A7 of a Nano. The tables that pinMode() uses to look up port numbers and pin masks only go up to Pin 19 (A5). Pins higher than 19 will return data from off the end of the table.
Pin 20 (A6) returns Port 1 (A), pin mask 50. Since the UNO/Nano has no Port A nothing will happen.
Pin 21 (A7) however returns Port 2 (B) and pin mask 3. Since Port B exists you will be setting the pinmode on pins 8 and 9 (PORTB pins 0 and 1), not on pin A7.
Thanks John,
A
johnwasser:
Hey Cheers John for ur help. been greatone thing if i speed it down to 0 it will not speed back up. also it only lets me get control when the pot is set to max fast speed how would i resolve this
thanks again u have been super
joegie:
Hey Cheers John for ur help. been greatone thing if i speed it down to 0 it will not speed back up. also it only lets me get control when the pot is set to max fast speed how would i resolve this
thanks again u have been super
I think the problem is integer overflow when multiplying 1023 * 50 because 51150 won't fit in a signed 'int'. One way to work around the problem is to change "50" to "50ul" which makes the 50 an 'unsigned long'. When the math gets done in 'unsigned long' instead of 'int' you get the right answer.