Go Down

Topic: playing tones based on micros() (Read 13158 times) previous topic - next topic

CrossRoads

Jul 28, 2013, 09:19 am Last Edit: Jul 28, 2013, 09:21 am by CrossRoads Reason: 1
I've got some code running, using a 1284P to allow 13 push buttons and have 13 tones out.
(piezo thread elsewhere got me going on this)
(easily scaled back to 8 & 8 on an UNO).
It's sort of "ultimate blink without delay" if you will.

What I am seeing is long bursts of ~40KHz output for as long as 11 seconds before the output settles down to the intended frequency.
I was thinking that it is taking a long time for the equivalent of "previousMicros" to catch up & get back in sync with the intended frequency.
Any thoughts on how to fix that?

This is the code that oscillates:
Code: [Select]

 /* Check if key for note C4 is pressed, key0  */

 if ((portDkeys & 0x04) == 0){ // key is pressed D2 -> A2
   // see if time to change hi to lo, or lo to hi
   if ( (currentTime - changeTime[0])>=noteArray[48]){    // <<< seems like the value in changeTime[0]
                                                     // <<< needs to catch up when a note starts
     changeTime[0] = changeTime[0] + noteArray[48];
     PINA=0x04;  // toggle output bit
   }
 }


This didn't fix it, just stopped the output altogether
Code: [Select]

 /* Check if key for note C4 is pressed, key0  */

if (keyActive[0] == 0){changeTime[0] = currentTime;}
 if ((portDkeys & 0x04) == 0){ // key is pressed D2 -> A2
   // see if time to change hi to lo, or lo to hi
   if ( (currentTime - changeTime[0])>=noteArray[48]){
     changeTime[0] = changeTime[0] + noteArray[48];
     PINA=0x04;  // toggle output bit
     keyActive[0]=1;
   }
 }
 else{keyActive[0]=0;}


And the entire file is attached.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Osgeld

#1
Jul 28, 2013, 09:26 am Last Edit: Jul 28, 2013, 09:27 am by Osgeld Reason: 1
no thoughts to fix it, but I have noticed it for a very long time trying to modulate wav files using it ... since 0.18

I named it the little squawk, for my playing around I squelched it some using a low pass filter, but its a ghetto way of doing it
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

CrossRoads

Ooh! Think I might have it:
Code: [Select]

 if ((portDkeys & 0x04) == 0){ // key is pressed D2 -> A2
    if (keyActive[0]==0){
      keyActive[0] = 1;
      changeTime[0] = currentTime;
    }
   // see if time to change hi to lo, or lo to hi
   if ( (currentTime - changeTime[0])>=noteArray[48]){ // time for a period toggle?
     changeTime[0] = changeTime[0] + noteArray[48];    // setup time for the next toggle
     PINA=0x04;  // write a 1 to PINx toggles an output bit - NickGammon to the rescue again!
   }
 }
 else{keyActive[0] = 0;}


Adding it in to more keys ...
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

CrossRoads

I think that did it  XD

Suppose I could connect the logic analyzer next and see if multiple outputs will mess anything up ...
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

CrossRoads

#4
Jul 29, 2013, 12:26 am Last Edit: Jul 29, 2013, 12:28 am by CrossRoads Reason: 1
Completed code, pic of prototype, schematic.
(cap on the power supply rail didn't make it onto the schematic tho, nor did the FTDI module off to the side of the 1284 board)
I connected to an amplified computer speaker. The sounds do not interfere with each other at all. I only had 8 switches, so I connected c-d-e-f-g-a-b-c. Tried pressing up to all 8 at once, did not hear any frequency fluctuation at all.  C4 and C5 sound good together, no "beat frequency at all, and also sounds very in tune with wife's baby grand piano.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

CrossRoads

I could see several of these to make up a keyboard with as many octaves as you want.
I used 26 of 32 IO, so add some more notes or use the extra pins for other effects to be written later.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

nickgammon

Sounds pretty cool. Maybe make a YouTube video showing it off?
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

CrossRoads

#7
Jul 30, 2013, 12:27 am Last Edit: Nov 23, 2016, 06:18 pm by CrossRoads
Short video of me sort of hacking out a tune!
https://www.youtube.com/watch?v=4c8idXN4Pg0

[edit 11/23/2016 - link fixed, seems youtube changed cut & paste formats?]
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

nickgammon

Seems to work pretty well. Well done!
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

larryd

No technical PMs.
The last thing you did is where you should start looking.

GoForSmoke

Quote
PINA=0x04;  // write a 1 to PINx toggles an output bit - NickGammon to the rescue again!


What returns that pin to LOW? Toggle would take:

      PINA ^= 0x04; 

Um confused.
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

nickgammon

Datasheet page 77:

Quote

13.2.2 Toggling the Pin

Writing a logic one to PINxn toggles the value of PORTxn, independent on the value of DDRxn. Note that the SBI instruction can be used to toggle one single bit in a port.


Quote

Toggle would take:

      PINA ^= 0x04; 


You are thinking of PORTA.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

GoForSmoke

What a neat little speed trick!
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

CrossRoads

Yep, those guys at Atmel are sharp. Nick Gammon is pretty sharp too for finding that in the datasheet!
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

nickgammon

I can't really take the credit. Someone else put me up to it. But it is a nice trick :)
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

Go Up