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

Thread: Distorted waveform, please help!

  1. #1
    Junior Member
    Join Date
    Oct 2018
    Posts
    9

    Distorted waveform, please help!

    Hi everyone. I have brought my test down to the most bare bones code and I am still having issues. Essentially its one button, one knob, and the audio system design tool is just a waveform into an envelope into the output. The waveform is distorted, and I can't figure out why. Can someone give me a hint? It's driving me mad.

    It's a Teensy 3.6 that I have attached with headers above the audio board. The Synthplay sketch as a test runs beautifully.

    Code:
    #include <Arduino.h>
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    #include <Bounce.h>
    
    Bounce button0 = Bounce(0, 15);
    
    // GUItool: begin automatically generated code
    AudioSynthWaveform       waveform1;      //xy=497,821
    AudioEffectEnvelope      envelope1;      //xy=709,832
    AudioOutputI2S           audioOut;       //xy=1146,858
    AudioConnection          patchCord1(waveform1, envelope1);
    AudioConnection          patchCord2(envelope1, 0, audioOut, 0);
    AudioConnection          patchCord3(envelope1, 0, audioOut, 1);
    AudioControlSGTL5000     sgtl5000_1;     //xy=787,955
    // GUItool: end automatically generated code
    
    void setup() {
     Serial.begin(9600);
    
    //Full Synth requires some serious memory
      AudioMemory(10);
      sgtl5000_1.enable();
    
      // Full volume is 1.0 which will damage speakers or ears in most cases
      sgtl5000_1.volume(0.4); 
    
      pinMode(0, INPUT_PULLUP);
    
      waveform1.begin(0.6, 440, WAVEFORM_TRIANGLE);
      
      envelope1.attack(250);
      envelope1.decay(350);
      envelope1.sustain(0.6);
      envelope1.release(350);
    }
    void loop() {
      button0.update();
    
      float knob1 = (float)analogRead(A9) / 1023.0;
      waveform1.frequency(1650 * knob1 + 0.25);
    
      if (button0.fallingEdge()) {
        envelope1.noteOn();
      }
      if (button0.risingEdge()) {
        envelope1.noteOff();
      }
    }

  2. #2
    Senior Member
    Join Date
    Apr 2013
    Posts
    1,810
    Your loop is going to be cycling very quickly and may be disabling interrupts a lot, adding a delay(1); may give time for the various functions that need time with interrupts active without harming the user interface responce.

  3. #3
    Senior Member
    Join Date
    Jul 2014
    Posts
    1,914
    Quote Originally Posted by halogravity View Post
    The waveform is distorted, and I can't figure out why. Can someone give me a hint? It's driving me mad.
    what do yo mean by distorted?
    can you give a picture with a waveform?

  4. #4
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    1,615
    Your sketch works for me on T3.2@120mhz with button and pot on A2. I get a nice triangle wave, Vpp = 560mv, freq varies with pot adjustmnet. If you don't have a pot attached, you get random values from analogRead() which will make the triangle wave quickly change frequencies with some flat sections -- is that what you mean by distorted??

  5. #5
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    1,615
    use A2, A9 is pin 23 which is used by the audio adapter -- review the schematic
    https://www.pjrc.com/store/teensy3_audio.html

  6. #6
    Junior Member
    Join Date
    Oct 2018
    Posts
    9
    Quote Originally Posted by manitou View Post
    use A2, A9 is pin 23 which is used by the audio adapter -- review the schematic
    https://www.pjrc.com/store/teensy3_audio.html
    Thanks so much for the replies everyone! Excuse the noob question, but should I be avoiding any pins that the audio shield use as a rule?

    GremlinWrangler
    Your loop is going to be cycling very quickly and may be disabling interrupts a lot, adding a delay(1); may give time for the various functions that need time with interrupts active without harming the user interface responce.
    GremlinWrangler should I add the delay after all the code in the loop?

    I'm going to try these suggestions and report back. Now that someone has verified the code as working I have a feeling it may be my old jumper wires... which would be embarrassing but solvable! Thanks everyone!

  7. #7
    Senior Member
    Join Date
    Jul 2014
    Posts
    1,914
    Quote Originally Posted by halogravity View Post
    GremlinWrangler should I add the delay after all the code in the loop?
    Not speaking for GremlinWrangler, I think, you do not need a delay in the loop. loop is running only if no interrupt service routine is running.

  8. #8
    Junior Member
    Join Date
    Oct 2018
    Posts
    9
    So for anyone with this problem in the future...

    Make sure your breadboard power and ground rails aren't divided int sections. I moved the ground and 3.3v of the pot way closer to the teensy, and it works crazy smooth now! Thanks everyone, stupid noob mistake as is generally the case.

  9. #9
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    18,529
    Quote Originally Posted by halogravity View Post
    should I be avoiding any pins that the audio shield use as a rule?
    Short & simple answer: Yes, avoid those pins. Use the other pins which aren't needed by the audio shield.

    Longer answer: If you need more pins, some of the audio shield pins can be used in some circumstances. 4 pins are used for the SD card. If you never put a SD card in the socket, you can use those pins. 1 pin is used to select the optional memory chip. If you never solder a memory chip to the bottom side of the board, you can use that pin. 1 pin is for reading the optional pot. If you don't solder a pot to the board, that pin can be used. But there are some caveats. The pot pin has a 0.1 uF capacitor connected. The chip select pins for the memory chip and SD card have pullup resistors.

    Even if you do have a SD card, the MISO, MOSI and SCK pins can be used for connecting other SPI chips. SPI shares those 3 signals, and then each chip needs its own chip select pin. But beware some SPI chips lack proper tri-state of their MISO pin. The RA8875 display is probably the most common one. If you connect one of those, you'll need to add a tri-state buffer chip. Fortunately, most SPI chips do properly tri-state MISO which allows them to share the signals with other SPI chips.

    The SDA and SCL signals are also sharable with other I2C chips. Each I2C chip needs to have a unique address. Separate chip select signals aren't used with I2C, which is why the chip have assigned addresses. Fortunately the address used by SGTL5000 on the audio shield is rarely used by other chips, so almost all other I2C chips can share those signals.

    But the simplest thing is to just use the other pins which the audio shield doesn't use at all.

  10. #10
    Junior Member
    Join Date
    Oct 2018
    Posts
    9
    Thanks Paul! And thanks for a great product!

Posting Permissions

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