Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 13 of 13

Thread: example waveform.ino

  1. #1
    Junior Member
    Join Date
    Oct 2019
    Posts
    17

    example waveform.ino

    I'm trying to test the sample waveform.ino file present in the audio example on a Tensy3.2, but it doesn't work well with me!
    I tested the knobs and potentiometers on the serial monitor, its ok.
    I don't have the audio kit, just knobs and potentiometers .
    I use an oscilloscope to see the waves.
    The waves are present on pins 9 and 11, i can see it!

    My problem: if I turn the potentiometers nothing happens, the frequency or the phase does not change!
    What did I miss?

    CODE + an other file myWaveforms:
    Code:
    // Waveform Example - Create 2 waveforms with adjustable
    // frequency and phase
    //
    // This example is meant to be used with 3 buttons (pin 0,
    // 1, 2) and 2 knobs (pins 16/A2, 17/A3), which are present
    // on the audio tutorial kit.
    //   https://www.pjrc.com/store/audio_tutorial_kit.html
    //
    // Use an oscilloscope to view the 2 waveforms.
    //
    // Button0 changes the waveform shape
    //
    // Knob A2 changes the frequency of both waveforms
    //   You should see both waveforms "stretch" as you turn
    //
    // Knob A3 changes the phase of waveform #1
    //   You should see the waveform shift horizontally
    //
    // This example code is in the public domain.
    
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    #include <Bounce.h>
    
    AudioSynthWaveform       waveform1;      //xy=171,84
    AudioSynthWaveform       waveform2;      //xy=178,148
    AudioOutputI2S           i2s1;           //xy=360,98
    AudioOutputAnalogStereo  dacs1;          //xy=372,173
    AudioConnection          patchCord1(waveform1, 0, i2s1, 0);
    AudioConnection          patchCord2(waveform1, 0, dacs1, 0);
    AudioConnection          patchCord3(waveform2, 0, i2s1, 1);
    AudioConnection          patchCord4(waveform2, 0, dacs1, 1);
    AudioControlSGTL5000     sgtl5000_1;     //xy=239,232
    
    Bounce button0 = Bounce(0, 15);
    Bounce button1 = Bounce(1, 15);
    Bounce button2 = Bounce(2, 15);
    
    int current_waveform=0;
    
    extern const int16_t myWaveform[256];  // defined in myWaveform.ino
    
    void setup() {
      Serial.begin(9600);
      pinMode(0, INPUT_PULLUP);
      pinMode(1, INPUT_PULLUP);
      pinMode(2, INPUT_PULLUP);
    
      // Audio connections require memory to work.  For more
      // detailed information, see the MemoryAndCpuUsage example
      AudioMemory(10);
    
      // Comment these out if not using the audio adaptor board.
      // This may wait forever if the SDA & SCL pins lack
      // pullup resistors
      //sgtl5000_1.enable();
      //sgtl5000_1.volume(0.8); // caution: very loud - use oscilloscope only!
    
      // Confirgure both to use "myWaveform" for WAVEFORM_ARBITRARY
      waveform1.arbitraryWaveform(myWaveform, 172.0);
      waveform2.arbitraryWaveform(myWaveform, 172.0);
    
      // configure both waveforms for 440 Hz and maximum amplitude
      waveform1.frequency(440);
      waveform2.frequency(440);
      waveform1.amplitude(1.0);
      waveform2.amplitude(1.0);
    
      current_waveform = WAVEFORM_TRIANGLE;
      waveform1.begin(current_waveform);
    }
    
    void loop() {
      // Read the buttons and knobs, scale knobs to 0-1.0
      button0.update();
      button1.update();
      button2.update();
      float knob_A2 = (float)analogRead(A2) / 1023.0;
      float knob_A3 = (float)analogRead(A3) / 1023.0;
    
      AudioNoInterrupts();
      // use Knob A2 to adjust the frequency of both waveforms
      waveform1.frequency(100.0 + knob_A2 * 900.0);
      waveform2.frequency(100.0 + knob_A2 * 900.0);
      // use Knob A3 to adjust the phase of only waveform #1
      waveform1.phase(knob_A3 * 360.0);
      AudioInterrupts();
    
      // Button 0 changes the waveform type
      if (button0.fallingEdge()) {
        switch (current_waveform) {
          case WAVEFORM_SINE:
            current_waveform = WAVEFORM_SAWTOOTH;
            Serial.println("Sawtooth");
            break;
          case WAVEFORM_SAWTOOTH:
            current_waveform = WAVEFORM_SAWTOOTH_REVERSE;
            Serial.println("Reverse Sawtooth");
            break;
          case WAVEFORM_SAWTOOTH_REVERSE:
            current_waveform = WAVEFORM_SQUARE;
            Serial.println("Square");
            break;
          case WAVEFORM_SQUARE:
            current_waveform = WAVEFORM_TRIANGLE;
            Serial.println("Triangle");
            break;
          case WAVEFORM_TRIANGLE:
            current_waveform = WAVEFORM_TRIANGLE_VARIABLE;
            Serial.println("Variable Triangle");
            break;
          case WAVEFORM_TRIANGLE_VARIABLE:
            current_waveform = WAVEFORM_ARBITRARY;
            Serial.println("Arbitary Waveform");
            break;
          case WAVEFORM_ARBITRARY:
            current_waveform = WAVEFORM_PULSE;
            Serial.println("Pulse");
            break;
          case WAVEFORM_PULSE:
            current_waveform = WAVEFORM_SAMPLE_HOLD;
            Serial.println("Sample & Hold");
            break;
          case WAVEFORM_SAMPLE_HOLD:
            current_waveform = WAVEFORM_SINE;
            Serial.println("Sine");
            break;
        }
        AudioNoInterrupts();
        waveform1.begin(current_waveform);
        waveform2.begin(WAVEFORM_SINE);
        AudioInterrupts();
      }
      
    }
    Last edited by Frank B; 01-03-2021 at 03:29 PM. Reason: Please use [code][/code] the next time.

  2. #2
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    500
    I noticed that you define this:
    Code:
    AudioOutputAnalogStereo dacs1;
    However, a stereo DAC is not supported on Teensy 3.2:

    Click image for larger version. 

Name:	DACS.PNG 
Views:	8 
Size:	15.4 KB 
ID:	23065

    Not sure what you see on pins 9 & 11 though...

    Regards,
    Paul

  3. #3
    Junior Member
    Join Date
    Oct 2019
    Posts
    17
    Thanks PaulS
    ok I didn't understand everything.
    I put the output in DAC and it works

  4. #4
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,544
    I use an oscilloscope to see the waves.
    The waves are present on pins 9 and 11, i can see it!
    You're going to have to explain this in detail. Pin 9 is a digital pin which can also be used to generate PWM output. Pin 11 is a digital pin only.
    So, it's a mystery how you can be seeing "the waves" on those Teensy 3.2 pins because there is only one DAC output on a Teensy 3.2 on Pin A14.
    Can you post a clear photo (or photos) of your Teensy3.2 and the way it is wired up to buttons and potentiometers?

    Pete

  5. #5
    Junior Member
    Join Date
    Oct 2019
    Posts
    17
    I'm not inventing anything. it is an example provided by the library, take a Teensy 3.2 and the example from the Audio / Synthesis / Waveforms library ...
    But whatever, the problem is solved by using DAC / A14.

  6. #6
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,544
    I didn't say you were inventing anything, but I couldn't see how you were getting useful waveforms from Pins 9 and 11.

    But whatever, the problem is solved by using DAC / A14.
    Yeah, whatever. Now you're using the correct pin for the DAC on a Teensy 3.2.

    But there's still a mystery.
    I've had a look at the code for AudioOutputAnalogStereo and it is specifically coded to handle only a Teensy 3.5 or 3.6 because they are the only Teensys which have two DACs.
    If it is compiled for any other processor it will use this code:

    Code:
    #else // not __MK64FX512__ or __MK66FX1M0__
    
    void AudioOutputAnalogStereo::begin(void)
    {
    }
    
    void AudioOutputAnalogStereo::update(void)
    {
    	audio_block_t *block;
    	block = receiveReadOnly(0); // input 0
    	if (block) release(block);
    	block = receiveReadOnly(1); // input 1
    	if (block) release(block);
    }
    
    #endif
    The begin function does nothing at all, so no DAC is setup.
    What the update does is look for an audio input block on the left or right channels. If it receives one, it immediately releases it (i.e. it throws it away). The end result is that nothing is output to a DAC.

    Pete

  7. #7
    Junior Member
    Join Date
    Oct 2019
    Posts
    17
    This is my code. It works.
    Is it now possible to send only x period type and x period of another type?
    Example send 58 sine wave and send 2 empty wave (= 0) and return send 58 + 2 .....
    An idea ?

    here in this picture send 35 + 1 (white signal)
    Click image for larger version. 

Name:	crank signal.png 
Views:	16 
Size:	6.3 KB 
ID:	23070


    Code:
    // Waveform Example - Create 1 waveforms with adjustable
    // frequency
    //
    // This example is meant to be used with 1 buttons (pin 0,
    // ) and 1 knobs (pins 16/A2)
    //
    // Use an oscilloscope to view waveform on DAC/A14 on Teensy 3.2.
    //
    // Button0 changes the waveform shape
    //
    // Knob A2 changes the frequency of both waveforms
    //   You should see both waveforms "stretch" as you turn
    //
    // This example code is in the public domain.
    
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    #include <Bounce.h>
    
    // GUItool: begin automatically generated code
    AudioSynthWaveform       waveform1;      //xy=599,243
    AudioOutputAnalog        dac1;           //xy=862,390
    AudioConnection          patchCord2(waveform1, dac1);
    
    Bounce button0 = Bounce(0, 15);
    
    int current_waveform=0;
    int i=0;
    int count = 0;
    int flag = 0;
    extern const int16_t myWaveform[256];  // defined in myWaveform.ino
    
    void setup() {
      Serial.begin(9600);
      pinMode(0, INPUT_PULLUP);
    
      // Audio connections require memory to work.  For more
      // detailed information, see the MemoryAndCpuUsage example
      AudioMemory(10);
    
      // Confirgure both to use "myWaveform" for WAVEFORM_ARBITRARY
      waveform1.arbitraryWaveform(myWaveform, 172.0);
    
      // configure both waveforms for 440 Hz and maximum amplitude
      waveform1.frequency(440);
      waveform1.amplitude(1.0);
    
      current_waveform = WAVEFORM_SINE;
      waveform1.begin(current_waveform);
    }
    
    void loop() {
      // Read the buttons and knobs, scale knobs to 0-1.0
      button0.update();
    
      float knob_A2 = (float)analogRead(A2) / 1023.0;
      //AudioNoInterrupts();
      
      waveform1.frequency(100.0 + knob_A2 * 9000.0);
     //AudioInterrupts();
      
      // Button 0 changes the waveform type
      if (button0.fallingEdge()) {
        switch (current_waveform) {
          case WAVEFORM_SINE:
            current_waveform = WAVEFORM_SAWTOOTH;
            Serial.println("Sawtooth");
            break;
          case WAVEFORM_SAWTOOTH:
            current_waveform = WAVEFORM_SAWTOOTH_REVERSE;
            Serial.println("Reverse Sawtooth");
            break;
          case WAVEFORM_SAWTOOTH_REVERSE:
            current_waveform = WAVEFORM_SQUARE;
            Serial.println("Square");
            break;
          case WAVEFORM_SQUARE:
            current_waveform = WAVEFORM_TRIANGLE;
            Serial.println("Triangle");
            break;
          case WAVEFORM_TRIANGLE:
            current_waveform = WAVEFORM_TRIANGLE_VARIABLE;
            Serial.println("Variable Triangle");
            break;
          case WAVEFORM_TRIANGLE_VARIABLE:
            current_waveform = WAVEFORM_ARBITRARY;
            Serial.println("Arbitary Waveform");
            break;
          case WAVEFORM_ARBITRARY:
            current_waveform = WAVEFORM_PULSE;
            Serial.println("Pulse");
            break;
          case WAVEFORM_PULSE:
            current_waveform = WAVEFORM_SAMPLE_HOLD;
            Serial.println("Sample & Hold");
            break;
          case WAVEFORM_SAMPLE_HOLD:
            current_waveform = WAVEFORM_SINE;
            Serial.println("Sine");
            break;
          }
        }
        AudioNoInterrupts();
    
        waveform1.begin(current_waveform);
        AudioInterrupts();  
    }

  8. #8
    Junior Member
    Join Date
    Oct 2019
    Posts
    17
    I was thinking of using something like this :

    Can it work?

    Code:
        if (flag == 1 && count < 2)
        {
          waveform1.amplitude(0.0);    
          count=count+1;
              if (count == 2) {
              flag = 0;count = 0;
              }
        }
        if (flag == 0 && count < 59)
        {
          waveform1.amplitude(1.0);
          count=count+1;
              if (count == 59) {
              flag = 1;count=0;
              }
        }

  9. #9
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,544
    It might work if it was counting audio cycles but as far as I know there's no way to do that.

    Pete

  10. #10
    Junior Member
    Join Date
    Oct 2019
    Posts
    17
    yes it does not work.

  11. #11
    Junior Member
    Join Date
    Oct 2019
    Posts
    17
    is it possible to stop an audio wave when you want it and that it stops when the phase only = 0 ?

    My idea is to generate on a pwm pin a signal (50%) at the same frequency as the sine wave. And count the number of pwm pulse on another pin. If I reach 58 pulse stop the sine wave (or amplitude = 0) and after 2 pulse restart it (or amplitude = 1).
    Last edited by NicoEFI; 01-10-2021 at 06:10 PM.

  12. #12
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    7,988
    Quote Originally Posted by NicoEFI View Post
    is it possible to stop an audio wave when you want it and that it stops when the phase only = 0 ?

    My idea is to generate on a pwm pin a signal (50%) at the same frequency as the sine wave. And count the number of pwm pulse on another pin. If I reach 58 pulse stop the sine wave (or amplitude = 0) and after 2 pulse restart it (or amplitude = 1).
    No Nico, there are no api calls that allow that.
    But why don't you just look at the sourcecode? It is not forbidden to modify it, and answers all questions

  13. #13
    Junior Member
    Join Date
    Oct 2019
    Posts
    17
    Quote Originally Posted by Frank B View Post
    No Nico, there are no api calls that allow that.
    But why don't you just look at the sourcecode? It is not forbidden to modify it, and answers all questions


    I am not able

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •