ToneAC library manipulation

Hi all!

I'm working on a project, where i'm using the toneAC.h library to drive a piezo. It works perfectly when i use the pins 9 and 10 as described in the library. However since we in the project need to use pin 10 for another component i'm trying to use other pins. To do so i opened the .h file in the library and tried to get an understanding of what's going on. As far as i understand currently it says:

  #if defined (__AVR_ATmega32U4__) || defined(__AVR_ATmega640__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__)
    #define PWMT1AMASK DDB5
    #define PWMT1BMASK DDB6
    #define PWMT1DREG DDRB
    #define PWMT1PORT PORTB
  #elif defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__)
    #define PWMT1AMASK DDD4
    #define PWMT1BMASK DDD5
    #define PWMT1DREG DDRD
    #define PWMT1PORT PORTD
  #else
    #define PWMT1AMASK DDB1
    #define PWMT1BMASK DDB2
    #define PWMT1DREG DDRB
    #define PWMT1PORT PORTB
  #endif

Where the DDB1 and DDB2 in the final 'else' statement refers to pin 9 and 10 based on this picture:

My intuition then tells me, that based on


i should change

    #define PWMT1AMASK DDB1
    #define PWMT1BMASK DDB2
    #define PWMT1DREG DDRB
    #define PWMT1PORT PORTB

into

    #define PWMT1AMASK DDD3
    #define PWMT1BMASK DDD5
    #define PWMT1DREG DDRD
    #define PWMT1PORT PORTD

In order to change from pin 9 and 10 into 3 and 5.
However this doesn't work, and the piezo doesn't work with this approach.

Does anyone have an idea on what i'm doing wrong?

I'm not a big expert! Rather new to arduino and this kind of programming.

Thanks a lot in advance!

On the Arduino, some pins are assigned certain tasks and you have no choice but to use those pins for their intended purposes. ToneAC uses pins 9 & 10, assigned on an ATMega328 to Timer1 for output.

Use a different pin for your "other component".

You could use other pins but you would need to pick two PWM pins on the same timer. 9 and 10 are on Timer1 (OC1A and OC1B). Since the system relies on Timer0 for millis() (Pins 5 and 6) you would probably want to use Timer2 (Pins 3 and 11). You would be limited by Timer2 being an 8-bit timer.

johnwasser:
You could use other pins but you would need to pick two PWM pins on the same timer. 9 and 10 are on Timer1 (OC1A and OC1B). Since the system relies on Timer0 for millis() (Pins 5 and 6) you would probably want to use Timer2 (Pins 3 and 11). You would be limited by Timer2 being an 8-bit timer.

Thanks a lot for the answer!

I'm not good enough with electronics to understand how an 8-bit timer will influence my circuit or code :slight_smile:

However if i want to change to pin 3 and 11 it would be (as i understand it) on two different gates? The D and B gate, which will require more changes in the code. Do you have any suggestions on how i could address that problem?

Thanks again!

BR.
/ Daniel

You could rewrite ToneAC to use the 8 bit Timer2, and hence use pins 3 and 11. It would be quite a good learning experience.

Or, you could move your "other device" to a different pin, and use ToneAC as intended.

Or just use the Tone library with any pin.