1

I'm using my Arduino to make a speaker-like device. Instead of sound, it receives the beat of the song, thus the data comes in an array of floats. Each item in the array is a timestamp, an LED is turned on based on that value.

For example, [0.01,0.53] means sleep 0.01s, turn on LED, pop off 0.01, sleep 0.53 seconds, turn on LED.

I have two questions:

1)How can I receive data and pop off data at the same time? I'm thinking I can send maybe 10 items for the Arduino to buffer at one time, and repeat that until the whole action is done. The popping off action is time sensitive, so I can't do it sequentially. How can I run both functions at once? Are arduinos capable of multithreading?

2)I'm using an arduino with BLE and sending over the data using serial.read() from an iPhone. and the device only allows 18 bytes to be sent over at one time and it is very slow. What other methods can I use? I need to send 18 bytes from my iPhone, wait 2 seconds, then send it again otherwise the arduino treats it as one single send and the other values are not sent over.

3)Is there a better way of doing all of this that I'm missing?

Thank you very much.

  • The arduino keeps time in microseconds. You can't have fractional microseconds. It only lets you have integer microseconds. floats are inherently inaccurate. They simply cannot hold many values exactly, they're just approximations. Using floats here is not good. You should be using unsigned long variables instead and keeping time in milliseconds instead of seconds. That will greatly improve performance once you get it done and also make your work easier. – Delta_G Apr 28 '20 at 04:01
  • Arduino can't multithread, but you can certainly have code that is constantly switching back and forth checking each thing once every few microseconds. You just have to write your code to do things in short chunks. It's called "non-blocking" code. Look that up. – Delta_G Apr 28 '20 at 04:02
  • Answer to #2: It really depends on what exactly you're using. That doesn't sound like a normal limitation. It has to be an issue with either the module you have or the software you're using somewhere. You have to share all those kinds of details if you really want help. – Delta_G Apr 28 '20 at 04:04
  • Answer #3. You haven't really explained the end goal or what you want to actually accomplish. Are you blinking lights to the beat or something? Either way, there is probably a better way to do it. – Delta_G Apr 28 '20 at 04:04
  • 1
    What exactly do you mean when you say "pop off"? – Delta_G Apr 28 '20 at 04:06
  • why do you need to transmit a float? ... a beat is more along the lines of a single bit data – jsotola Apr 28 '20 at 05:01
  • @Delta_G Didn't know that about floats. Thanks! I will change it. 2) I'm using Beetle BLE. I've read from multiple threads that BLE has a packet limit. Am I wrong? How do I go about increasing this value? 3) Yeah that is what I'm trying to do. The timestamps of the beats are stored in an array on the iPhone and it is sent over to the Arduino over BLE, which is then supposed to light up LEDs based on that time. By pop off I mean removing an element from the array. – yeahyongying Apr 29 '20 at 00:04
  • @jsotola I'm transmitting timestamps of every beat of a song in an array. How would a beat be transmitted as a single bit? – yeahyongying Apr 29 '20 at 00:05
  • a pulse in realtime – jsotola Apr 29 '20 at 00:34
  • Why would you send timestamps of every single beat? They're beats. They're rhythmic. You don't need that much information to convey what to do. A start time, a rate, and a number of beats or a stop time. – Delta_G Apr 29 '20 at 03:24
  • @Delta_G I'm looking for precision. Some songs have different bpm in different parts of the song. – yeahyongying Apr 29 '20 at 16:52
  • Yes, so you send a new BPM marker and change the beat. You still don't have to send a timestamp for every beat. Unless the drummer really really sucks you only need to send something at most every few measures. – Delta_G Apr 29 '20 at 17:06
  • @Delta_G That's a solution I guess. It would certainly make my job a lot easier. However, the device is meant to be a learning tool for beginner dancers/ musicians so if possible I'd still like to make it as precise as possible. I'll look up non blocking functions, and if that doesn't work use the bpm marker method. Thanks for your help! – yeahyongying Apr 29 '20 at 18:47
  • This is a trap nobies fall into a lot. You say, "as precise as possible". So in computers you have to put a number to everything. There's no such thing as absolute precision here. At the very least there will be some small time difference between the audio player and the clock on the Arduino. In reality it takes 200ms for a human to blink their eye. Think about how fast that is. So when we talk about something being off by 20 or 50 milliseconds, that is an amount of time that is completely imperceptible to humans in most situations. You and I are talking microseconds. – Delta_G Apr 29 '20 at 19:01
  • So many people fall into the trap of trying to get something to be absolutely perfectly at the same time when such a thing is a physical impossibility. If a professional drummer were behind the scenes hitting buttons to blink the lights with the beat you'd have timing discrepancies on the order of tens or even 100 milliseconds and everyone in the room would call it exact. When you are dealing with timing and measurement in computers, you have to start thinking about what precision is really necessary. – Delta_G Apr 29 '20 at 19:03
  • @Delta_G Huh, I've never thought of it that way before. Makes a lot of sense. – yeahyongying Apr 29 '20 at 19:14

0 Answers0