Input needed for personal sound/toy project using sensors! Help!

Hi, I've been going around in circles for some time now for the best hardware/software combination on a budget to make what Should be a simple project work, without bugs.
The idea is simple: I'm working on a toy that when the lights are on, is a cute and cuddly stuffed animal; when the lights are off, it becomes demonic and plays scary files. You would think this would be simple, but Arduino's single thread programming keeps causing it to hang up. I am using an Arduino Uno controlling a Pemenol sound board combined with simple If statements in the coding that guide the Arduino program to activate certain sound files in the light, or in the dark. The problem is the program keeps getting hung up when the lights switch on or off: the happy sounds keep playing to the end when the lights go out, and the scary sounds keep playing to the end when the lights come up. Worse, half the time the program seems to get stuck and nothing plays when the lights go on/off, forcing me to hit the reset button on the Uno for the program to work again. I have been plagued with this problem for a long time now, no matter how I change the coding. I am thinking of resorting to buying two Nanos, each controlling its own sound board, where one activates only when the light sensor is activated, and the other activates only when it is dark. It is an expensive solution, so I'm reaching out for any ideas. Would an Adafruit board be better to go with? Is it better at multithreading? Is that what I need for this program? I appreciate any input. Thank you.

  • Study how to make and use State Machines.

  • Here is a simple Task Scheduler tutorial:

  • Share your Sketch and the schematic of your project.

My input is this: show your code. Use the <CODE/> tool to enclose it in a code block so that it's readable.

Nothing better than frightening small children in the dark…

That may be the problem but the cause is definitely wrong programming ... :- :wink:

There is nothing mentioned in your requirements that could not be done with an UNO or a similar board. No need for multithreading just for non-blocking code.

Most of the supportive forum members are quite exhausted from using their magic crystal ball to find a flaw in invisible code (although there seems to be relationship between magic balls and what may be considered a cute and cuddly stuffed toy animal :smiling_face:)

So if you really want a more specific and quick support :

  • Post a drawing of your wiring with all components (can be a manual sketch)
  • Post your code (as already mentioned above inside code tags <CODE>, which you get using the appropriate entry in the menu of the post editor)

Use the magic key of giving appropriate information ...

Then the force of the forum members will be with you!
Good luck!
ec2021

No, your single thread programming keeps causing it to hang up!

Show us the code and we can suggest how to fix.

Read the forum guide to find out how to post code here correctly.

If you cut a circle, it becomes a line segment... where you have a start, a linear progression, and an end.

Occam's razor. Simple is best (@xfpd corollary: be accurate).

  if (LDR == 1) {
    // do thing one
}
  if (LDR == 0) {
    // do thing two
}

What if the child (as I once was), holds the toy tightly, or sits on the toy?

Blaming a device that does exactly what it is told is not how to solve the problem.
And... 100% of failures are human caused.

Therefore, check your work, starting with a physical inspection:

  • Did you "simply" push jumper pins into the vias of the boards like the demonstration videos (frustratingly) show, or do you solder all connections?
  • Did you provide all devices a path to common ground?
  • Is your code as frustrating as your request for help sounds?

No.

No.

At no point in your code do you control the Pemenol Sound Board using multithreading.

Because your code is an endless loop. You need to control when a sound plays, which sound plays, and duration of the sound.

We'll see.

Here is the datasheet (more like web page, but thorough): https://manuals.plus/asin/B0CGRN3JKF

A pseudo code (I resisted my urge to code-dump):

- load eight sounds (read the datasheet)
- set dip switches for Mode 0 (LOW = play, HIGH = stop)
- configure "dark" pins
- configure "light" pins
- configure LDR pin
- configure state, timer for next state, duration of current state

void setup() {
  - set pin direction for dark pins
  - set pin direction for light pins
  - set pin state for dark pins
  - set pin state for light pins
  - set pin state for LDR
}

void loop() {
  - check timer (controlling WHEN a sound can play)
  - flip play/stop state
  - if "play" state, call "play" function
  - if "stop" state, call "stop" function
}

void play() {
  - check flag to play only one sound
  - check LDR for light/dark
  - if light, play (set pin HIGH) light sound
  - if dark, play (set pin HIGH) dark sound
  - clear "play one time" flag to block more than one sound
}

void stop() {
  - set all sound pins LOW
  - set "play one time" flag to allow any sound to play
}

Believe it or not, teenage girls fell in love with my Hellpuppy and all wanted one. It's always the bad boys.......

I appreciate all of the input, thanks to everyone! I'll try to post my code soon, as well as the hardware setup.

Would you describe how often and how long the sounds would play?