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

Thread: single button starts audio and relays in timed sequence...BUGS!

  1. #1

    single button starts audio and relays in timed sequence...BUGS!

    I. AM. A. NOOB.

    I'm missing something very basic and stupid here I know but I would love it if somebody could point that error out.

    Code is below. TEENSY3.1 using the TEENSY audio shield. Behavior right now: it almost randomly plays the audio file but skips over the relays. It won't stop! Stooopid something there. Here you go, let's see who spots it first TIA



    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <Bounce.h>

    // WAV files converted to code by wav2sketch
    #include "AudioSampleClap_r1.h"


    // Create the Audio components. These should be created in the
    // order data flows, inputs/sources -> processing -> outputs
    //
    AudioPlayMemory sound0;

    AudioMixer4 mix1; // two 4-channel mixers are needed in
    AudioMixer4 mix2; // tandem to combine 6 audio sources
    AudioOutputI2S headphones;
    AudioOutputAnalog dac; // play to both I2S audio board and on-chip DAC

    // Create Audio connections between the components
    //
    AudioConnection c1(sound0, 0, mix1, 0);
    AudioConnection c8(mix1, 0, headphones, 0);
    AudioConnection c9(mix1, 0, headphones, 1);
    // Create an object to control the audio shield.
    //
    AudioControlSGTL5000 audioShield;

    // Bounce objects to read six pushbuttons (pins 0-5)
    //
    Bounce button0 = Bounce(0, 5);

    //setting up relays
    int fan = 23;
    int heat = 21;
    int wled = 19;
    int bled = 17;


    void setup() {
    // Configure the pushbutton pins for pullups.
    // Each button should connect from the pin to GND.
    pinMode(0, INPUT_PULLUP);

    // Audio connections require memory to work. For more
    // detailed information, see the MemoryAndCpuUsage example
    AudioMemory(10);

    // turn on the output
    audioShield.enable();
    audioShield.volume(0.5);

    // by default the Teensy 3.1 DAC uses 3.3Vp-p output
    // if your 3.3V power has noise, switching to the
    // internal 1.2V reference can give you a clean signal
    //dac.analogReference(INTERNAL);

    // reduce the gain on mixer channels, so more than 1
    // sound can play simultaneously without clipping
    mix1.gain(0, 0.4);
    }

    void loop() {
    // Update the button object
    button0.update();

    // When the button 0 is pressed, the sequence of events begins with sound, then fan,
    // then heat, then lights flashing. THEN TURNS OFF and waits for button to be pushed again

    if (button0.risingEdge()) {
    sound0.play(AudioSampleClap_r1); //sound starts
    delay(100); // wait for 100 ms
    digitalWrite(fan, HIGH); // turn the fan on (HIGH is the voltage level)
    delay(100); // wait for 100 ms
    digitalWrite(heat, HIGH); // turn the heat on
    delay(10); // wait for 10 ms
    digitalWrite(wled, HIGH); //bright white LEDs on
    delay(25);
    digitalWrite(bled, HIGH); //bright white LEDs on
    delay(25);
    digitalWrite(bled, LOW);
    delay(25);
    digitalWrite(wled, LOW);
    }
    else {
    // turn everything off:
    digitalWrite(wled, LOW);
    digitalWrite(bled, LOW);
    digitalWrite(fan, LOW);
    digitalWrite(heat, LOW);
    }

    delay(1000);
    }

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    16,214
    Not sure how the button code runs - catching the risingEdge is going to be a quick single event. As I see this the delay(1000) is where most time will be spent and for that second - unless the button is queued it will disappear.

    first step would be maybe MOVE the delay(1000) inside the end of the IF case. And you only need to turn things off after they were turned them on.

    Code:
    void loop() {
      // Update the button object
      button0.update();
    
      // When the button 0 is pressed, the sequence of events begins with sound, then fan,
      // then heat, then lights flashing. THEN TURNS OFF and waits for button to be pushed again
    
      if (button0.risingEdge()) {
        sound0.play(AudioSampleClap_r1); //sound starts
        delay(100); // wait for 100 ms
        digitalWrite(fan, HIGH); // turn the fan on (HIGH is the voltage level)
        delay(100); // wait for 100 ms
        digitalWrite(heat, HIGH); // turn the heat on
        delay(10); // wait for 10 ms
        digitalWrite(wled, HIGH); //bright white LEDs on
        delay(25);
        digitalWrite(bled, HIGH); //bright white LEDs on
        delay(25);
        digitalWrite(bled, LOW);
        delay(25);
        digitalWrite(wled, LOW);
        delay(1000);
    
        // turn everything off:
        digitalWrite(wled, LOW);
        digitalWrite(bled, LOW);
        digitalWrite(fan, LOW);
        digitalWrite(heat, LOW);
    
      }
    }
    You can refine from there if that shows more events being caught.

    When posting code insert in [ CODE ] blocks. I click the QUOTE ICON to get the blocks and then change QUOT to COD , or use the extended version with a button for CODE
    Last edited by defragster; 04-23-2015 at 05:29 PM.

  3. #3
    Hi defragster, thank you for the help!

    I started with the audio example file SamplePlayer, which has the loop where 5 buttons are being monitored using the "fallingedge" state. I flipped it back to that and even with adjusting the delay you mentioned I'm still in the same boat.

    Is this not a case for a if / else? If fallingEdge is true then do the loop, else do nothing...?

    YUP, noob I am.

  4. #4
    ....just switched from pin0 to pin2...working fine now...crossing fingers...

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    26,563
    Normally you should not use delay() in the loop, when you're checking for edge changes.

  6. #6
    Senior Member
    Join Date
    Dec 2014
    Posts
    310
    As Paul says; you want to dump that delay(1000) in your loop().

    Detecting button presses via the bounce library works by calling the update() function (as you've done), then checking for a rising or falling edge. However, you'll only pick that up if your last update was just as it was rising or falling.

    By introducing that delay(1000), you're only checking for a button press once per second, so chances are you're going to miss it. =)


    When posting code, try and use [*code] and [*/code] tags around your code at the start at end respectively (without the *s). Helps keep the formatting and makes it more readable.
    Last edited by Cosford; 04-23-2015 at 08:46 PM.

  7. #7
    NOOB here: dumped the delay, thx for the tip. I need all these events to happen in a specifically timed sequence with precise delays in between them. Right now with the code below I have pin2 starting the audio and my test LED on pin 23 ("fan") stays on only during audio playback. Stupid question, but if I have to dump out the delays inside the loop does that put me in "THEN" land? Missing core concepts here, I know. TIA, here's the code:

    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <Bounce.h>
    
    // WAV files converted to code by wav2sketch
    #include "AudioSampleClap_r1.h"       
    
    
    // Create the Audio components.  These should be created in the
    // order data flows, inputs/sources -> processing -> outputs
    //
    AudioPlayMemory    sound0;
    
    AudioMixer4        mix1;    // two 4-channel mixers are needed in
    AudioMixer4        mix2;    // tandem to combine 6 audio sources
    AudioOutputI2S     headphones;
    AudioOutputAnalog  dac;     // play to both I2S audio board and on-chip DAC
    
    // Create Audio connections between the components
    //
    AudioConnection c1(sound0, 0, mix1, 0);
    AudioConnection c8(mix1, 0, headphones, 0);
    AudioConnection c9(mix1, 0, headphones, 1);
    // Create an object to control the audio shield.
    // 
    AudioControlSGTL5000 audioShield;
    
    // Bounce objects to read six pushbuttons (pins 0-5)
    //
    Bounce button0 = Bounce(2, 5);
    
    //setting up relays
    int fan = 23;
    int heat = 21;
    int wled = 19;
    int bled = 17;
    
    
    void setup() {
      // Configure the pushbutton pins for pullups.
      // Each button should connect from the pin to GND.
      pinMode(2, INPUT_PULLUP);
    
      // Audio connections require memory to work.  For more
      // detailed information, see the MemoryAndCpuUsage example
      AudioMemory(10);
    
      // turn on the output
      audioShield.enable();
      audioShield.volume(0.5);
    
      // by default the Teensy 3.1 DAC uses 3.3Vp-p output
      // if your 3.3V power has noise, switching to the
      // internal 1.2V reference can give you a clean signal
      //dac.analogReference(INTERNAL);
    
      // reduce the gain on mixer channels, so more than 1
      // sound can play simultaneously without clipping
      mix1.gain(0, 0.4);
    }
    
    void loop() {
      // Update the button object
      button0.update();
    
    delay(100);
    
      // When the button 0 is pressed, the sequence of events begins with sound, then fan,
      // then heat, then lights flashing. THEN TURNS OFF and waits for button to be pushed again
    
      if (button0.fallingEdge()) {
        //delay(100);
       
        digitalWrite(fan, HIGH); // turn the fan on (HIGH is the voltage level)
        
       // delay(2000);
        
         sound0.play(AudioSampleClap_r1); //sound starts
        // wait for 100 ms
        delay(100); // wait for 100 ms
        digitalWrite(heat, HIGH); // turn the heat on
        delay(10); // wait for 10 ms
        digitalWrite(wled, HIGH); //bright white LEDs on
        delay(25);
        digitalWrite(bled, HIGH); //bright white LEDs on
        delay(25);
        digitalWrite(bled, LOW);
        delay(25);
        digitalWrite(wled, LOW);
        delay(500);
    
        // turn everything off:
        digitalWrite(wled, LOW);
        digitalWrite(bled, LOW);
        digitalWrite(fan, LOW);
        digitalWrite(heat, LOW);
    
      }
    }

  8. #8
    just quick update: commenting out everything but audio still turns on LED on pin 23...dunno why:
    Code:
    void loop() {
      // Update the button object
      button0.update();
    
    delay(100);
    
      // When the button 0 is pressed, the sequence of events begins with sound, then fan,
      // then heat, then lights flashing. THEN TURNS OFF and waits for button to be pushed again
    
      if (button0.fallingEdge()) {
        //delay(100);
       
       // digitalWrite(fan, HIGH); // turn the fan on (HIGH is the voltage level)
        
       // delay(2000);
        
         sound0.play(AudioSampleClap_r1); //sound starts
        // wait for 100 ms
       // delay(100); // wait for 100 ms
      //  digitalWrite(heat, HIGH); // turn the heat on
      //  delay(10); // wait for 10 ms
      //  digitalWrite(wled, HIGH); //bright white LEDs on
      //  delay(25);
      //  digitalWrite(bled, HIGH); //bright white LEDs on
      //  delay(25);
       // digitalWrite(bled, LOW);
      //  delay(25);
      //  digitalWrite(wled, LOW);
      //  delay(500);
    
        // turn everything off:
     //  digitalWrite(wled, LOW);
     //   digitalWrite(bled, LOW);
     //   digitalWrite(fan, LOW);
     //   digitalWrite(heat, LOW);
    
      }
    }

  9. #9
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    26,563
    Looks like you *still* have a 100 ms delay in loop(), between button0.update() and button0.fallingEdge(). Get rid of that 100 ms delay!

    If you only have 1 button, and you're happy with ignoring it while your sequence of events is running, then you can use delays inside the if block.

    But when you want to have more buttons or other stuff, you'll need to restructure things to not use delay() at all. For now, get rid of that 100 ms delay in loop().

  10. #10
    Getting better with your help sir!
    Code:
        #include <Audio.h>
        #include <Wire.h>
        #include <SPI.h>
        #include <SD.h>
        #include <Bounce.h>
        #include <elapsedMillis.h>
    
        // WAV files converted to code by wav2sketch
        #include "AudioSampleClap_r1.h"       
        // Create the Audio components.  These should be created in the
        // order data flows, inputs/sources -> processing -> outputs
        //
        AudioPlayMemory    sound0;
        AudioMixer4        mix1;    // two 4-channel mixers are needed in
        AudioMixer4        mix2;    // tandem to combine 6 audio sources
        AudioOutputI2S     headphones;
        AudioOutputAnalog  dac;     // play to both I2S audio board and on-chip DAC
        // Create Audio connections between the components
        //
        AudioConnection c1(sound0, 0, mix1, 0);
        AudioConnection c8(mix1, 0, headphones, 0);
        AudioConnection c9(mix1, 0, headphones, 1);
        // Create an object to control the audio shield.
    
        AudioControlSGTL5000 audioShield;
        // Bounce objects to read six pushbuttons (pins 0-5)
    
        Bounce button0 = Bounce(2, 5);
        //setting up relays
        int fan = 23;
        int heat = 21;
        int wled = 19;
        int bled = 17;
    
        //sequence variable, indicates which step we are in
        int sequenceStep;
    
        elapsedMillis timeElapsed;//global timing variable
    
        void setup() {
          // Configure the pushbutton pins for pullups.
          // Each button should connect from the pin to GND.
          pinMode(2, INPUT_PULLUP);
          // Audio connections require memory to work.  For more
          // detailed information, see the MemoryAndCpuUsage example
          AudioMemory(10);
          // turn on the output
          audioShield.enable();
          audioShield.volume(0.5);
          // by default the Teensy 3.1 DAC uses 3.3Vp-p output
          // if your 3.3V power has noise, switching to the
          // internal 1.2V reference can give you a clean signal
          //dac.analogReference(INTERNAL);
          // reduce the gain on mixer channels, so more than 1
          // sound can play simultaneously without clipping
          mix1.gain(0, 0.4);
          
          sequenceStep = 0;
        }
    
        void loop() {
          // Update the button object
          button0.update();
           delay(100);
          // When the button 0 is pressed, the sequence of events begins with sound, then fan,
          // then heat, then lights flashing. THEN TURNS OFF and waits for button to be pushed again]
          // Only start sequence if we finished the last one already
          if (button0.fallingEdge() && sequenceStep == 0) {
           sequenceStep = 1;
           timeElapsed = 0;
          }
          
          //if 100 millis have passed since the first step go to step 2 (fan)
          if (sequenceStep == 1 and timeElapsed >= 100) {
            sequenceStep = 2;
            timeElapsed = 0;
            digitalWrite(fan, HIGH); // turn the fan on (HIGH is the voltage level)
          }
           
           // if 2000 millis have passed since step 2 turn on heat and play sound
           if (sequenceStep == 2 and timeElapsed >= 2000) {
            sequenceStep = 3;
            timeElapsed = 0;
            digitalWrite(heat, HIGH); // turn the heat on
            sound0.play(AudioSampleClap_r1); //sound starts
          }
          
             // if 100 millis have passed turn on wled
           if (sequenceStep == 3 and timeElapsed >= 100) {
            sequenceStep = 4;
            timeElapsed = 0;
            digitalWrite(wled, HIGH); //bright white LEDs on
          }
          
               // if 25 millis have passed turn on bled
           if (sequenceStep == 4 and timeElapsed >= 25) {
            sequenceStep = 5;
            timeElapsed = 0;
            digitalWrite(bled, HIGH); //bright white LEDs on
          }
          
            // if 25 millis have passed turn off bled
           if (sequenceStep == 5 and timeElapsed >= 25) {
            sequenceStep = 6;
            timeElapsed = 0;
            digitalWrite(bled, LOW); //bright white LEDs off
          }
          
              // if 25 millis have passed turn off wled
           if (sequenceStep == 6 and timeElapsed >= 25) {
            sequenceStep = 7;
            timeElapsed = 0;
            digitalWrite(wled, LOW); //bright white LEDs off
          }
          
                // wait 500 millis and turn everything off and reset 
           if (sequenceStep == 7 and timeElapsed >= 500) {
            sequenceStep = 0;
            timeElapsed = 0;
            //these were already turned off above
             //  digitalWrite(wled, LOW);
             //   digitalWrite(bled, LOW);
            digitalWrite(fan, LOW);
           digitalWrite(heat, LOW);
          }
          
        }

  11. #11
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    26,563
    Quote Originally Posted by grayfx View Post
    Code:
           delay(100);
    Delete this line!!!

  12. #12
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    16,214
    @grayfx: The loop() is called as often as nothing pauses the loop ... as often as the loop exits, and it checks the buttons on entry. Any time you are in delay() buttons will be missed.

    I think my version in Post #2 was a better starting point than where you went after when you put a delay() after a single check. You can change that to button 2 if you find it works better.

    That code will still ignore button presses for 1.5 seconds - but at that time the system is doing something and as noted you can work to improve that later, as you maybe don't want to get a second press while acting on the first.

    For controlled waits not missing buttons something like this - if I'm not mis-assuming how the button code works.

    Code:
    static int  SawButton = 0;
    
    DelayBut( unsigned long wait ) {
    unsigned long  stop = millis() + wait;
    while ( millis() < stop )  { // may exit early every 49 days
        if (button2.update()) {
          if ( button2.risingEdge() )
            SawButton = 1;
        }
      }
    }
    
    void loop() {
     button2.update();
     DelayBut( 100 );
    
      // When the button 2 is pressed, the sequence of events begins with sound, then fan,
      // then heat, then lights flashing. THEN TURNS OFF and waits for button to be pushed again
    
      if (SawButton ) {
        SawButton = 0;
        sound0.play(AudioSampleClap_r1); //sound starts
        DelayBut(100); // wait for 100 ms
        digitalWrite(fan, HIGH); // turn the fan on (HIGH is the voltage level)
        DelayBut(100); // wait for 100 ms
    
        ...
    
    }
    <edit> hope nobody else saw that . . . this was not compiled or tested code - just an idea . . .
    Last edited by defragster; 04-24-2015 at 05:40 PM.

  13. #13
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    16,214
    You did okay with the if() watching for the button. Just because there is an if() doesn't mean you need an else. If your case the off things stay off [you might want to set them off in setup()]. But your else case setting them off anytime the button press wasn't detected isn't a productive activity - that is why it was moved (in post #2) as always done, only after the button press had turned it on.

    If you only have one button and one thing to do something as shown (in post #2) should work. During the busy time servicing the detected button, if you don't need to do anything else then you can act accordingly - either ignore the button - or use the DelayBut() type of solution, so when you leave a queued button won't have been lost.

    If you want to take one button and do other stuff in loop() you'd need to find a creative way to do that - by doing whatever else you want - but not accepting that same button (or any conflicting task) for some wait time with more conditionals in the if().

    Note: the indicated DelayBut() might be improved adding a yield(); after the while() and before the if() - this will invite the Teensy to do other outside tasks that might otherwise be put off from the tight while loop.
    Also as noted the while() test in DelayBut() will prematurely exit if the provided wait crosses the rollover period of millis(), so that could be calculated a different way if that is a concern.
    Also this code assumes you only have one button, if you want to build on this with more buttons, you'd need to account for those as well.

  14. #14
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    16,214
    Button usage in another thread pointed to this: https://www.pjrc.com/teensy/td_libs_Bounce.html

    Looking suggests this might be better - though perhaps not ideal.

    Code:
    static int SawButton = 0;
    
    DelayBut( unsigned long wait ) {
      unsigned long stop = millis() + wait;
      while ( millis() < stop ) { // may exit early every 49 days
        // you need to call the update method every scan for the edge detection to work.
        if (button2.update()) {
          if ( button2.risingEdge() )
            SawButton = 1;
        }
      }
    }
    <edited into the above>

  15. #15
    I appreciate all the help gents! QUICK question: pin 23 goes HIGH when any audio is being played. Design feature or broken TEENSY? Paul this also happens in your SamplePlayer sketch. Also I'm seeing fairly constant low voltage "leaking" out of pin 19 (LED barely lit, not a loose connection either). Fried TEENSY? Normal?

    Thanks

  16. #16
    Senior Member
    Join Date
    Jan 2015
    Location
    frisia
    Posts
    285
    Normal: pin 23 isthe LRCLK. Pin 19 is the SCL, which has a 2k2 pullup, hence the "leaking", see schematic

  17. #17
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    26,563
    The AudioOutputI2S object automatically takes over pins 9, 11, 13, 22 and 23. Those are the pins used to communicate stereo audio to/from the Teensy and audio board.

    If you remove AudioOutputI2S, those pins become available for normal usage.

    Pins 18 and 19 are used by the Wire library, which is used to control the chip on the audio board.

  18. #18
    Hi Paul, good to know. I need 4 digital outs to control relays and one digital in (pushbutton to start sequence). Which would you recommend? TIA

  19. #19
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    4,216
    According to my notes, the audio shield does not use pins 0-5, 8, 16-17 (A2/A3), 20-21 (A6/A7). I might suggest leaving 0/1 alone, just in case you need to add support for something that uses a UART. You can use A0-A9 for digital I/O as well as analog I/O (some of the other analog pins are analog only and can not be used for digital read/write). So perhaps using 2, 3, 5, and 8 for the digital outputs, and A2 for an analog input.

    In addition, you can use the 2 analog pins on the inside row of the Teensy (A10/A11) for analog uses. On the Teensy LC, you can also use these pins for digital output/input (pins 24-25), but you can't use them for digital stuff in the 3.0/3.1.

    There is also the analog only pin on the back of the Teensy 3.1 (A14) and Teensy LC (A12), which also is the DAC (digital/analog output pin).

    You can also solder wires to the solder pads underneath the Teensy 3.0/3.1 (the LC does not have pads underneath it). There are 10 pins that can be used for digital input/outputs, 2 pins that are analog only (A12-A13). Of the 10 digital pins, there are 6 pins that can also do analog inputs on the 3.1 (on the 3.0 they are only digital pins). Note, at present, you do not want to use pin #33, since if it is held low when the Teensy resets, it causes the Teensy to enter EZ_Port mode. This is probably not recommend for a NOOB.

    I created this spreadsheet that I've posted from time to time that documents the various pin assignments of the various microprocessors (Teensy 3.0/3.1/LC, ATmega 328p boards, ATmega 32u4 boards, etc.). I also included the pin assignments of the 3 Teensy shields that I was aware of: https://docs.google.com/spreadsheets...gid=1103027528
    Last edited by MichaelMeissner; 04-25-2015 at 12:19 AM.

  20. #20
    Thanks Michael!

  21. #21

    figured it out, knowing taken pins was the key!!!

    OK guys, just knowing that those pins were dedicated to audio or wire or otherwise TAKEN (I don't feel too incredibly stupid now as it seems that should be pretty obviously noted on the audio shield info, no?) things started working. Michael your notes are gold.

    Here's the working code:
    Code:
    #include <Audio.h>
        #include <Wire.h>
        #include <SPI.h>
        #include <SD.h>
        #include <Bounce.h>
        #include <elapsedMillis.h>
        // WAV files converted to code by wav2sketch
        #include "AudioSampleClap_r1.h"       
        // Create the Audio components.  These should be created in the
        // order data flows, inputs/sources -> processing -> outputs
        //
        AudioPlayMemory    sound0;
        AudioMixer4        mix1;    // two 4-channel mixers are needed in
        AudioMixer4        mix2;    // tandem to combine 6 audio sources
        AudioOutputI2S     headphones;
        AudioOutputAnalog  dac;     // play to both I2S audio board and on-chip DAC
        // Create Audio connections between the components
        //
        AudioConnection c1(sound0, 0, mix1, 0);
        AudioConnection c8(mix1, 0, headphones, 0);
        AudioConnection c9(mix1, 0, headphones, 1);
        // Create an object to control the audio shield.
        AudioControlSGTL5000 audioShield;
        // Bounce objects to read six pushbuttons (pins 0-5)
        Bounce button0 = Bounce(1, 1);
        //setting up relays
        int fan = 2;
        int heat = 3;
        int wled = 4;
        int bled = 5;
        //sequence variable, indicates which step we are in
        int sequenceStep;
        elapsedMillis timeElapsed;//global timing variable
        void setup() {
          // Configure the pushbutton pins for pullups.
          // Each button should connect from the pin to GND.
          pinMode(1, INPUT_PULLUP);
          pinMode(2, OUTPUT);
          pinMode(3, OUTPUT);
          pinMode(4, OUTPUT);
          pinMode(5, OUTPUT);
          
          // Audio connections require memory to work.  For more
          // detailed information, see the MemoryAndCpuUsage example
          AudioMemory(10);
          // turn on the output
          audioShield.enable();
          audioShield.volume(0.5);
          // by default the Teensy 3.1 DAC uses 3.3Vp-p output
          // if your 3.3V power has noise, switching to the
          // internal 1.2V reference can give you a clean signal
          //dac.analogReference(INTERNAL);
          // reduce the gain on mixer channels, so more than 1
          // sound can play simultaneously without clipping
          mix1.gain(0, 0.4);
          
          sequenceStep = 0;
        }
        void loop() {
          // Update the button object
    
          button0.update();
    
          // When the button 0 is pressed, the sequence of events begins with sound, then fan,
          // then heat, then lights flashing. THEN TURNS OFF and waits for button to be pushed again]
          // Only start sequence if we finished the last one already
          if (button0.fallingEdge() && sequenceStep == 0) {
           sequenceStep = 1;
           timeElapsed = 0;
          }
          
          //if 100 millis have passed since the first step go to step 2 (fan)
          if (sequenceStep == 1 && timeElapsed >= 100) {
            sequenceStep = 2;
            timeElapsed = 0;
            digitalWrite(fan, HIGH); // turn the fan on (HIGH is the voltage level)
          }
           
          // if 3000 millis have passed since step 2 turn on heat and play sound
           if (sequenceStep == 2 && timeElapsed >= 3000) {
           sequenceStep = 3;
           timeElapsed = 0;
           digitalWrite(heat, HIGH); // turn the heat on
           sound0.play(AudioSampleClap_r1); //sound starts
          }
          
             // if 100 millis have passed turn on wled
          if (sequenceStep == 3 && timeElapsed >= 3000) {
            sequenceStep = 4;
           timeElapsed = 0;
           digitalWrite(wled, HIGH); //bright white LEDs on
          }
          
               // if 25 millis have passed turn on bled
           if (sequenceStep == 4 && timeElapsed >= 2000) {
            sequenceStep = 5;
            timeElapsed = 0;
            digitalWrite(bled, HIGH); //bright white LEDs on
          }
          
            // if 25 millis have passed turn off bled
           if (sequenceStep == 5 && timeElapsed >= 2500) {
            sequenceStep = 6;
            timeElapsed = 0;
            digitalWrite(bled, LOW); //bright white LEDs off
          }
          
              // if 25 millis have passed turn off wled
           if (sequenceStep == 6 && timeElapsed >= 2500) {
            sequenceStep = 7;
            timeElapsed = 0;
            digitalWrite(wled, LOW); //bright white LEDs off
          }
          
                // wait 500 millis and turn everything off and reset 
           if (sequenceStep == 7 && timeElapsed >= 13100) {
            sequenceStep = 0;
            timeElapsed = 0;
            //these were already turned off above
               digitalWrite(wled, LOW);
               digitalWrite(bled, LOW);
               digitalWrite(fan, LOW);
               digitalWrite(heat, LOW);
          }
          
        }

Posting Permissions

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