Go Down

Topic: sound+lights not working together? (Read 518 times) previous topic - next topic

artisanoo

Hi, first poster here -

I just got my arduino and am trying to get my feet wet.

Was trying to get something running that would drive some leds as well as synthesize some sound at the same time.

i was able to get my leds working fine, with blinking and pwm fading happening independently (using a counter variable, and using modulo arithmetic to drive a series of if statements that perform logic - i.e. i have one set of logic that pushes the leds up to 255 brightness at certain intervals, and another set that fades them. this lets me set up different patterns very easily with  the fade speed being independent of the blink speed, etc).

i am also able to get sound to sort of work, in a similar way ( each iteration through the 'big' loop, it checks if the counter is the correct time to bring the peizo high or low ).

the problem is when i try to enable all these functions at once.
i assume it is because the timing is thrown off because the loop now takes longer to run ( and the frequency and quality of the sound is different)

sorry for being long winded, but essentially what i want to know, is what is the best way to accomplish what i want (ie blinking/fading of leds with sound at the same time).  am i approaching it with a fundamentally flawed idea?  is there a better way?  is it not doable in software?

thanks for any info you guys can provide...
g

artisanoo

did some more digging - would interrupts be the best way to deal with this kind of functionality?  seems like it would be better at keeping things happening at the right times, instead of relying on a big loop and assuming it happens 'fast enough'

any thoughts?

artisanoo

sorry to bump my own post but can anyone advise?
I am new at this so i am not sure what the best place to look for information would be.  

AWOL

Can we see your sketch?
It would then be much easier to make suggestions.

artisanoo

i dont have access to it at the moment, but its essentially something like this:

Code: [Select]

int counter=0
loop{

if (counter%(note interval)==0)
    {
         pulse piezo up or down etc..
    }

if (counter%(blink interval)==0)
    {
         blink logic
    }

if (counter%(fade interval)==0)
    {
          fade logic
    }

delayMicroseconds(a delay small enough to allow the sounds pulsing to work)
}




that allows me to do different things at different , independent intervals, which works for the most part.  the issue is that when the sound logic is enabled along with other logic, i assume the loop takes longer, and the effective delay between iterations is increased, which mangles the sound.  so i assume this approach is fundamentally flawed in that way.  

i thought interrupts would be a good way to approach it but was hoping to find some good resources about synth sound on the arduino in general so i can see what options have been proven as usable

Go Up