I'm trying to find a way to slide between two array values, similar to a portamento effect on synthesizers. Say I have a sequence containing two values, 255 and 0; I want to gradually decrease from 255 to 0 at a variable time determined by a potentiometer. Would I do some sort of moving average, or a low pass filter? Any ideas or guidance would be appreciated! Thanks
I want to gradually decrease from 255 to 0 at a variable time determined by a potentiometer.
Are you referring to using analogWrite()? Or are you referring to Serial.print()ing a value that decreases, over time, from 255 to 0?
I am sure you know exactly what you want to do, but some of us older folks need more information.
Do you know what an FSM , finite-state machine, is?
.
Sorry - hard for me to get things out of my head and in words!
I will try to explain it this way - There is a step sequencer that sequences between two values at a predetermined rate of say, 500 ms. The first step outputs a value of 255, and the second step outputs a value of 0 using analog write. like so :
analogWrite(255) --> delay(500) --> analogWrite(0)
what I want to do is this :
analog write(255) -->analogWrite(254)-->analogWrite(253)....-->analogWrite(1) --> analogWrite(0)
| |
|--------------------------> delay(500)--------------------------> |
so during that delay of 500ms, the analog write value goes from 255 to 0 incrementally. Does that make a little more sense?
And not - I am not familiar with an FSM but I will look into it
so during that delay of 500ms, the analog write value goes from 255 to 0 incrementally.
You can't make changes during a delay(). What you can do is change the time you delay() so that all 255 steps cause a 500 ms delay, although that will require steps of 1.96 milliseconds, so that could be a challenge. If you can allow the whole sequence to take 510 (or 512) milliseconds, the programming will be easier.
Of course, you might not want to use delay() at all...
So you need to do an analogWrite every ~2ms.
Starting at 255 then every 2ms the value is to decrement by one.
What happens when you reach zero?
.
larryd:
So you need to do an analogWrite every ~2ms.
Starting at 255 then every 2ms the value is to decrement by one.What happens when you reach zero?
.
when it reaches zero I would like for it to increment upwards back to 255 at the same rate. I know not to use delay, I was just using that as a simplistic way of explaining myself.
Got to get a tooth drilled , try this:
unsigned long myMicros; //use a name that makes sence to you
byte duration = 255;
bool runFlag = true;
int upDown = -1;
void setup()
{
}
void loop()
{
nextWrite();
}
void nextWrite()
{
if (micros() - myMicros < 2000)
{
return;
}
myMicros = micros();
analogWrite(9, duration);
duration = duration + upDown;
if (duration == 0 || duration == 255)
{
upDown = upDown * -1;
}
}
great, thank you I will check that out!