Announcement

Collapse
No announcement yet.

Digital volume for square waves in Arduino Nano.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    I've figured out to set up a library for ArduinoIDE 1.5 onwards.

    Includes in src folder, library.properties in the root folder of the library, #include <...> (instead of #include "..." in the sketches and examples.

    By doing this the example sketches appear in the "File -> Examples" menu.

    Here's the adapted library.

    VolumeSymmetric.zip

    https://www.geotech1.com/forums/filedata/fetch?id=419991&d=1707351751
    Attached Files

    Comment


    • #17
      D5 is PWM output for the speaker. You don't need to configure it, it's already done in the code and can't be changed. = to use this program with metal detector the sound should be related to target, let say to amplitude of the signal, usuly this is dane by applying a signal to the analog pin.

      Comment


      • #18
        Originally posted by pito View Post
        D5 is PWM output for the speaker. You don't need to configure it, it's already done in the code and can't be changed. = to use this program with metal detector the sound should be related to target, let say to amplitude of the signal, usuly this is dane by applying a signal to the analog pin.
        The "analog pin" in this Arduino is not analog, it's PWM. D5 is your "analog pin". Is it not clear yet?

        Your program must use the functions setVolumeRaw() or setVolumePercent() to modulate the sound according to the target strength.

        Use the "target.ino" example, connect a loudspeaker to pin D5 and listen. Easy!

        Comment


        • #19
          here frequency is depend to voltage on A0, in your case is not related to voltage on analog pin, so is kind of music box.

          Click image for larger version  Name:	image.png Views:	0 Size:	55.2 KB ID:	419996


          HTML Code:
          void setup() {
           pinMode(9, OUTPUT);
          }
          
          void loop() {
          
           int frequencyinput = analogRead(A0);
           frequencyinput = map(frequencyinput, 0, 1203, 50, 5000);
            tone(9, frequencyinput);
           delay(100);
          }​

          Comment


          • #20
            Originally posted by pito View Post
            here frequency is depend to voltage on A0, in your case is not related to voltage on analog pin, so is kind of music box.

            Click image for larger version Name:	image.png Views:	0 Size:	55.2 KB ID:	419996


            HTML Code:
            void setup() {
            pinMode(9, OUTPUT);
            }
            
            void loop() {
            
            int frequencyinput = analogRead(A0);
            frequencyinput = map(frequencyinput, 0, 1203, 50, 5000);
            tone(9, frequencyinput);
            delay(100);
            }​
            My friend, I posted a LIBRARY, not a PI sketch.

            You write your own PI sketch that reads A0, maps the frequency and then passes the frequency to the library using "setFrequency()". You can also read another pot in A1 ans map the volume to a value betwen 0 and 127 and pass it to the library using setVolumeRaw() or setVolumePercent().

            I won't write your code, i wrote a library so you can write your own code, whether it's a music box or a PI it's the same library.

            Code:
            #include <VolumeSymmetric.h>
            
            VolumeSymmetric v;
            
            void setup() {
                v.init();
            }
            
            void loop() {
            
                int frequencyinput = analogRead(A0);
                int volumeinput = analogRead(A1);
                frequencyinput = map(frequencyinput, 0, 1203, 50, 5000);​
                volumeinput= map(volumeinput, 0, 1203, 0, 127);
                v.setFrequency(frequencyinput);
                v.setVolumeRaw(volumeinput);
               _delay_ms(100);
            }​
            ​you must use _delay_ms() instead of delay() for this library.

            Comment


            • #21
              Thanks

              Comment

              Working...
              X