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

Thread: Lame Newbe Question to change digital switch to analog sensor

  1. #1

    Lame Newbe Question to change digital switch to analog sensor

    I'm sorry this question is so lame, but could someone help me with changing the code of this drum program from playing sounds triggered from a button/switch on digital pins 1, 2 and 3 to a piezo on analog pins A14, A15 and A16?

    Here is the code. It works fine but I want to change it to analog.

    I am not attaching the source sound files since they are linking to the program and working fine.

    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    // GUItool: begin automatically generated code
    AudioPlayMemory          playMem3;       //xy=269,225
    AudioPlayMemory          playMem1;       //xy=289,373
    AudioPlayMemory          playMem2;       //xy=293,297
    AudioMixer4              mixer1;         //xy=742,272
    AudioOutputI2S           Audio_Board_Output;           //xy=1031,274
    AudioConnection          patchCord1(playMem3, 0, mixer1, 0);
    AudioConnection          patchCord2(playMem1, 0, mixer1, 3);
    AudioConnection          patchCord3(playMem2, 0, mixer1, 1);
    AudioConnection          patchCord4(mixer1, 0, Audio_Board_Output, 0);
    AudioConnection          patchCord5(mixer1, 0, Audio_Board_Output, 1);
    AudioControlSGTL5000     sgtl5000_1;     //xy=591,435
    // GUItool: end automatically generated code
    #include "AudioSampleC1_drum.h"
    boolean previousC1_drumButtonStatus = false;
    unsigned long previousC1_drumTime = 0;
    //six more lines from magazine
    void setup()
      pinMode(2, INPUT_PULLUP);
    void loop()
      boolean newC1_drumButtonStatus = !digitalRead(2);
      if(newC1_drumButtonStatus !=
          previousC1_drumButtonStatus && millis() >
          previousC1_drumTime + 30) {
        previousC1_drumButtonStatus = newC1_drumButtonStatus;
        previousC1_drumTime = millis();

  2. #2
    Or do piezo triggers/sensors only work with MIDI out? Which is not what I want. I am looking for a small, compact drum machine that does not need a computer or MIDI module. I cannot find an example on the web. I just want my teensy and audio shield to play the sound files that I have in the program folder that I have formatted with Wav2Sketch.

  3. #3
    Senior Member ETMoody3's Avatar
    Join Date
    Mar 2014
    New Ulm, Mn
    You’re looking at rewriting both the setup and loop so much I wouldn’t call it “changing” I’d call it a “ page one re-write”.

    The sensor will work as you instruct.

    You need to have the program poll the sensor, then act on the poll.

    You’d change the pinMode line(s) in setup to the appropriate analog pin, and change the mode to “input”.

    The first line in loop is where the program is reading the digital pin.
    You want a variable in place to store the sensor data, then have the analogRead populate that variable.

    The line that triggers the sound will require modification to pass along the analog value as whatever parameter you intend, with the caveat that you’ll need to convert the raw adc reading into something relevant to the file playback.

    Of course, you probably just want someone to write that for you.

  4. #4
    ETMoody3, Thanks for the advice and input (no punn intended). I understand what you are saying. There are actually a few other features I would like my program to do, like be able to read the audio files that I will load onto the SD card.

    The problem is that I spend a heck of a lot of time looking at code on the web and never seem to learn how to program just what I want. I get too many errors that I do not understand. I also can't find a project out there that is similar enough to what I want to accomplish. The sparkfun hand pan 2 is the closest, but I don't want capacitive (finger touch) sensors.

    I asked the forum leader if it was against the rules of the forum to offer payment to somebody to write the code for me, but have not heard back yet.

  5. #5
    Senior Member ETMoody3's Avatar
    Join Date
    Mar 2014
    New Ulm, Mn
    I charge US75$/hr . I’ve been told I undersell myself at that rate.

    All the resources you need to learn are available to you, free.
    My best advice to anyone learning to code is to slow down, and learn to be the computer to the extent of your cognitive ability.

    Think your code through. If it fails to make sense, make it make sense *to you*. Never mind what anyone else may think of your choice. Make it work first, go for pretty later if you wish.

    A major problem, and one I have cussed out many writers for, is example and online blog code demonstrating advanced and concise coding tricks without adequate explanation.

    You may safely and reasonably expect the compiler to optimize a thing or ten.

    When in doubt, flash it and see.

    In your specific case, a programmer needs to know a few things you haven’t included in your query, like what parameter you want the analog reading to be converted to.

  6. #6
    Senior Member oddson's Avatar
    Join Date
    Feb 2013
    Isle in the Salish Sea
    Or do piezo triggers/sensors only work with MIDI out? ...
    You don't need MIDI, but a piezo to midi sketch is a good place to start.

    Paul gives example code for a single piezo trigger.

    I've extended that for multiple sensors using arrays and an indexed loop.

    Once you understand these sketches you can look to replacing the midi calls with audio library calls to play the wave files directly.

    If you can't figure out how the midi code works just ask. I'm not much help on the audio stuff but there are lots of others that can help when you're at that stage.

Posting Permissions

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