Every time loop() is being called it is doing all the work so it is a very busy sketch. The delay time being set by the pot doesn't matter in this situation because the effective delay is almost zero anyway (on a human scale).
You have to wrap the entire loop code in your delay code like this:
if ((millis() - changeTime) > ledSpeed) {
changeTime = millis();
It looks OK to me now as long as ledSpeed is some reasonable value. Any chance you can take a voltage reading on pin A0 and verify that it is not zero or near zero? A zero or near zero reading would cause this loop to run very fast also. Have you ever used Serial Monitor to debug your code? You may want to have the Arduino send the value of ledSpeed back to your computer to verify that it is reasonable and Serial Monitor is all sorts of help in other ways when you want to know the state of your running code. Read this:
The bit that does the LED changing is still inside the loop, you need to seprate out a change in lights from the loop.
Try playing being the processor, read through the code and remember that everything in the loop happens very fast.
You will see you are doing your sequence very fast, then all the pot is doing is delaying slightly the time before that whole sequence happens again.
Your change lights function goes through ALL the sequence in one go.
As I keep saying you need to separate out just one step and do that each time your timer times out.
It is your 'for' statement that is doing this. Remove it and replace it with a structure that only changes one of the light states any time it is entered. To do this you need a 'state variable' one that shows where you are up to in your sequence. You used the loop variable i for this but now you no longer have a for loop you will have to replace it with a variable you declare as static in the loop function.
A static variable is one that is initialised only once the first time it is encountered and after that it's value remains the same.
Grumpy_Mike:
It is your 'for' statement that is doing this. Remove it and replace it with a structure that only changes one of the light states any time it is entered. To do this you need a 'state variable' one that shows where you are up to in your sequence. You used the loop variable i for this but now you no longer have a for loop you will have to replace it with a variable you declare as static in the loop function.
A static variable is one that is initialised only once the first time it is encountered and after that it's value remains the same.
Is there an example of this? I searched but didn't see anything that might help. I may have missed something though.
Thanks Mike, it's working now. There is one minor issue though. When I turn the pot, most of the speed changes happen at the end of the pot that makes it go fastest rather than gradually and evenly change speed. How can I fix this? I am using a B100K pot.
It sounds like you are using a log pot and not a linear one.
Try swapping over the 5V and ground connections on the pot to move the log section to the other end.
If you do have a log pot and do not have a linear one, you can put resistors across it to linearise it.
See:- The Secret Life of Pots
Also that function will take a certain amount of time to run, if your delay is shorter than this then you won't see any change in speed. To gt round this add a fixed number to your analogue input reading.
Grumpy_Mike:
Also that function will take a certain amount of time to run, if your delay is shorter than this then you won't see any change in speed. To gt round this add a fixed number to your analogue input reading.
I tried this but I'm guessing it's not what you meant, because it didn't seem to be any different:
I'm working on adding additional patterns to this and changing them by pushing a button, and still keep the pot, but one thing that I'll need to change is this:
The changeLights() part in the above, how would I change this and where would I put it so that turning the pot changes speed on all patterns? In other words, if my first pattern looks like this:
void changeLights(){
static int dir = 1;
static int channel = 1;
Tlc.clear();
if (channel == 1) dir = 1;
if (channel == 14) dir = -1;
Tlc.set(channel - 1, 1000);
Tlc.set(channel, 4095);
Tlc.set(channel + 1, 1000);
Tlc.update();
channel += dir;
}
Would that part to change speed still be in the loop section, or would it be in the void changeLights() section?