Controlling Sound Effects

I am trying to write code to produce sound effects from a purchased sound module. The purchased sound module uses PWM pulses to trigger the playing of a sound.

I have successfully written code to produce the code for each sound, but now I need to control how may times it plays each sound.

I have played with "while" and "for" statements, but the code gets ugly in a hurry. Is there a simpler approach to being able to control when / how many times / timing of each sound that is played. Specifically, I want to play each sound, just one time when a rc transmitter switch position changes state.

That's the key phrase - state change detection. Look in

IDE-> file/examples/digital/

Hi @ovalracer ,

whether a code gets ugly in a hurry does not necessarily depend on while or for loops ... it depends on how and where they are used.

To avoid for or while loops if they are blocking loop() is usually a good idea because blocking code interferes with reading buttons or sensors or communication with external devices.

One possibility is to use millis() to control functions that shall only be performed in certain time steps/intervals.

A second mechanism is the so-called "state machine". It usually uses a switch/case structure that controls which functions are performed depending on the "state" of the software.

There are lots of examples in the forum and also on the Wokwi website (online Arduino simulator). Just search the net for

  • Arduino state machine
  • Arduino blink without delay

Good luck!

Here a link to a Wokwi state machine I wrote as an example some time ago

https://wokwi.com/projects/322586637175358035

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.