I've been running in circles with this one all day...
I'm using a digital potentiometer (AD5207), which seems to work quite nicely when entering the entering the registers by hand.
What I want to do is to increase the volume automatically until an analog input reaches a certain level. If the volume is < than desired, I want to turn the digital pot up. If the volume is > than the desired amount I want to turn it down.
I am monitoring the sensor level on a LCD display, so I can see the sensor output in real time.
I have tried these inside the void loop():
The below simplified step (just to see if the level will increase) will turn the volume up by one step only - from 0 to 1.
if (sensor < 100)
{
L++
volume(0,L); // this is tied to the void volume, where (channel,level) is written to the pot
}
So I've tried a for loop, which works to step the level up automatically.
for (L=0; L<50; L ++)
{
volume(0,L);
delay (5000); // strange, but I have to delay by at least 5 seconds in order for the level to change
}
Then I tried this (to control the volume). My hope in the code below was once the sensor read 100, the for loop would terminate. It doesn't though, it'll continue until '50' then start again at 0.
for (L=0; L<50; L ++)
{
volume(0,L);
delay (5000);
if (sensor >=100)
{
break;
}
}
I have also tried putting a for loop inside an if statement, which acted similarly to the first example, giving me one step only.
if (sensor <100)
{
for (L=0; L<50; L ++)
{
volume(0,L);
delay (5000); // strange, but I have to delay by at least 5 seconds in order for the level to change
}
}
Perhaps I am missing something, or is there a better way to do this?
I like the for loop, as I can set the maximum window to 50 - and not worry about the volume accidentally going higher than that in case of a sensor fail.
I am not quite sure why the first if statement only increases the level one time. I thought it would increase the level each time the void loop() went by if the level was less than 100.
Any comments or help are greatly appreciated.
Scott