Speaker aliasing low freqs?

I’ve been experimenting with the ultrasonic sensor and have been using the speaker to provide an indication of distance by walking down the musical scale as the robot gets closer to an object (and up as it gets further away). Basically, I have an if/else if structure:

  if (sonarValue < 5) {
    PlayImmediateTone(tones[A5], TONE_TIME);
    wait10Msec(TONE_TIME);
  } else if (sonarValue < 10) {
    PlayImmediateTone(tones[B5], TONE_TIME);
    wait10Msec(TONE_TIME);
  } else if (sonarValue < 15) { ...

in which sonarValue is the value read from the ultrasonic sensor, the tones] array is the frequency to play and TONE_TIME is a constant that causes the tone to play for 100 ms.

However, what I’ve noticed is that as I get down to around middle C (262 Hz) the tone jumps up. I’m not sure by how much the freq increases, but it is at least a few hundred Hz. I have noticed that the intended note also plays, but is much weaker than the higher tone. It’s a gradual switch. That is, when playing a tone using progressively higher integer values, the higher pitch tone fades as I advance past about middle C so that within a note or two you cannot really hear it. Note that I am not talking about the lower (and correct) tone, which also increases with progressively higher integer values.

Conversely, when playing progressively lower integer values, the higher pitch becomes noticeable a little bit above middle C. As I continue to walk down the note scale the higher pitch increases in volume while the correct tone fades away.

Has anyone had a similar experience? It sounds like an aliasing problem. I’m not sure that it matters, but I am using RobotC (the current version).

I don’t understand the word ‘aliasing’ in this usage.

In general, small speakers dont reproduce low frequency sounds well, because they have higher resonate frequencies.
C4 is middle C is 256…280 Hz, depending on the scale or reference.

If the speaker is being driven by a square wave, the effect could be worse, because the a square wave is composed of the sum of higher odd harmonics: eg 256 + 2563 + 2565 + 256*7 Hz; when one of these higher frequencies more closely matches the resonant frequency of the speaker, the higher frequency will be louder.

This reminds me of the old days of 1bit PC speakers, where someone discovered you could play chords. Some searches on
chords 1 bit pc speaker
may provide enough detail to reproduce, if that sort of thing interests you.