Better SINUS as TONE
Posted by kll on June 08 2018 11:57:08
work on the new PMS3 scope ( inside FLASK webserver "myBLOG" ) and
while i was looking for the tone info for arduino i come across the REAL SINUS for arduino thing from Adrian Freed,
- - the text is poor, no info what he is really doing,
+ but inside the code see table-based digital oscillator using "DDS" now can start googling
+ + the code is great ( far above my level )
runs on arduino mega 1280 ( lucky i still have one ( DF ROBOT MEGA 2010 ))
and UNO i am just testing
// following not needed in modern arduino IDE arduino IDE
//#include <'avr/io.h'>
//#include <'avr/interrupt.h'>
//#include <'avr/pgmspace.h'>

NOT WORK with Leonardo ?different timer structure?
and under IDE 1.8.5 with 2 small changes ( add "const" ) could upload.

later i changed it to run for me just one fix tone 440Hz ( for a second test 1500Hz)
add i used a UNO with the arduino tone 440Hz for show at second channel,
and used a Leonardo with PMS3 and also a MAX32 with PMS3 for better resolution and play 440 / 1500Hz signals.
what i see is actually something what i know from theory, it is the way the siemens inverter drive a "better" more sinus current to big drives, just as here with a (powerDC) ON OFF signal.
as he, Adrian Freed not even give a idea how he want to make a better sinus just with a Dout
a picture would have been very helpful ( for me as i am more the visual type )
(a) MEGA 440Hz "good sinus"
(b) UNO 440Hz "tone"
Leonardo PMS3 21kHz sampling

(a) MEGA 440Hz "good sinus"
(b) UNO 440Hz "tone"
MAX32 PMS3 65kHz sampling

(a) MEGA 440Hz "good sinus"
MAX32 PMS3 522kHz sampling
now one sinus does not fit on screen (360 samples)

so change ( MEGA ) to higher signal freq.
(a) MEGA 1500Hz "good sinus"
MAX32 PMS3 522kHz sampling

that was a very interesting usage of my latest play project
and thanks to Adrian for sharing his great work.
he did not mention that on that ?pwm? pin a speaker should be connected?
but say something like we would be able to hear the difference between 16 and 32 bit integers??? for the frequency generation, hmm i never hear any integers??

i now try the piezo disc ( connect between PWM pin and GND / NO transistor) & paper thing, but only hear it if my ear touch the paper,
for the piezo speaker tuna can i also have to work on the acoustic properties of the can LOL thai:555

but isn't there one question open? what/how he do with a different AMP
again, he can not change the ( here 5V ) arduino Dout ON signal, only the time it is ON
i could have just loaded the same program with a different
o1.amplitude = 255*256; // full amplitude
but i know i will play more and put in my serial menu so i can control from PC what the arduino tone generator does,
"n69,4,0,100 ( play: A4, quarter note, not sustain, very Loud) "
arduino IDE monitor at [CR] 115200 type: n77,2,1,80
show: ?n play: 77 phaseinc: 374980 msec: 300 pause: 199 AMP: 80
so for play notes i needed to define some timings:
Beat 1000 and use line note ON to pause OFF
NtoP = 0.4 NtoP_Sus = 0.7
and calc a tempo array
tempo[2][2]={
{(Beat * NtoP)/DurDiv , (Beat * ( 1.0 - NtoP))/DurDiv },
{ (Beat * NtoP_Sus)/DurDiv, (Beat * (1.0 - NtoP_Sus))/DurDiv } }
it says how long is the tone ON and how long OFF in millis
like for a full note
DurDiv = 1 // 2,4,8,16
Sus =0 or Sus = 1
[ { 400,600 },{ 700,300 } ]
I found in some old arduino code the idea that
a pause after a note should be 1.3 * Duration of the note
but he used his Duration = 1000, so the note plus pause == Beat for full note
would be 2.3 secs???
( i could not find any musical definition what could help me there, but i know from working as a kid in a piano company how it mechanically works ( hammer and damper ))
but the thing here now not work like a MIDI piano, because i not play one note, i change the setting!

and the loop plays that note with its timing again and again.
but here now only the AMP test for N 90, F 1480Hz
AMP 100, 50, 10

yes, as expected, reducing AMP makes the sinus "thinner",
but a 255*256; // full amplitude aka 65280 steps for LOUD ?hmm
i just use AMP [ 0 .. 100 ] * 652


this code