potentiometer troubles

Status
Not open for further replies.

r00n

Member
i hate to ask for help.

i have a teensy 3.6 and audioshield.
i have soldered connections to these and with jumper wires I compiled and ran the basic 4 voice drum machine code examples and there was no troubles.

inspired to build upon this example code, I copied and pasted code into sketch from many sources to get some form of sequencing using buttons which are connected to teensy on pins as per code... everything works as expected.

Then,
I wanted to add potentiometers to control drum voice parameters, freq, pitchmod etc. but i have been struggling to do so for 3 days.

I can't get any sensible information from any potentiometer.

i am using jumper wires to attach the teensy 3.6 to the audio shield. The analogue ground pin and the 3.3 v pins are attached to the audioshield from the 3.6 so i couldn't attach the potentiometer there. i took analogue ground and 3.3v pins to a breadboard and tried a parallel circuit with the potentiometer and audioshield, whilst connecting the potentiometer wiper to pin A12.... the result destroys the audio and i can't get the analogueread() function to give me any sensible numbers at all in the serial monitor.

i could do a circuit diagram if required.
thank you for your assistance.

Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include <synth_simple_drum.h>
/*---------------------------------------------------------------------------------*/
// GUItool: begin automatically generated code
AudioSynthSimpleDrum     drum2;          //xy=399,244
AudioSynthSimpleDrum     drum3;          //xy=424,310
AudioSynthSimpleDrum     drum1;          //xy=431,197
AudioSynthSimpleDrum     drum4;          //xy=464,374
AudioMixer4              mixer1;         //xy=737,265
AudioOutputI2S           i2s1;           //xy=979,214
AudioConnection          patchCord1(drum2, 0, mixer1, 1);
AudioConnection          patchCord2(drum3, 0, mixer1, 2);
AudioConnection          patchCord3(drum1, 0, mixer1, 0);
AudioConnection          patchCord4(drum4, 0, mixer1, 3);
AudioConnection          patchCord5(mixer1, 0, i2s1, 0);
AudioConnection          patchCord6(mixer1, 0, i2s1, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=930,518
// GUItool: end automatically generated code
/*---------------------------------------------------------------------------------*/
//variables
static uint16_t bpm = 120;
static uint16_t rez = 4;// smallest musical time
static uint16_t tempo = (double)((60000) / (bpm * rez));
static uint32_t next;
static uint16_t beat = 0;
static uint16_t patternSelect = 0;

/*---------------------------------------------------------------------------------*/
#define DEBOUNCE 15  // button debouncer, how many ms to debounce,
#define NUMBUTTONS sizeof(buttons)
/*---------------------------------------------------------------------------------*/
//buttons
byte buttons [] = {12, 24, 25, 26, 27, 28, 29, 30, 2, 3, 4, 5};// THE PIN THE BUTTONS ARE ON,
byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];
byte previous_keystate[NUMBUTTONS], current_keystate[NUMBUTTONS];
//potentiometers




/*---------------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------------*/
boolean pattern1 [] = {0, 0, 0, 0, 0, 0, 0, 0};
boolean pattern2 [] = {1, 0, 0, 0, 0, 0, 0, 0};
boolean pattern3 [] = {0, 0, 0, 0, 0, 0, 0, 0};
boolean pattern4 [] = {0, 0, 0, 0, 0, 0, 0, 0};
/*---------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------*/
void setup()

{ Serial.begin(115200);

  // audio library init
  AudioMemory(15);

  next = millis() + 1000;

  AudioNoInterrupts();

  //button setup

  for (byte i = 0; i < NUMBUTTONS; i++)
  {
    pinMode(buttons[i], INPUT);
    digitalWrite(buttons[i], HIGH);

  }

  //drum setup

  drum1.frequency(300);
  drum1.length(300);
  drum1.secondMix(0.0);
  drum1.pitchMod(0.2);

  drum2.frequency(60);
  drum2.length(300);
  drum2.secondMix(0.0);
  drum2.pitchMod(1.0);

  drum3.frequency(550);
  drum3.length(4000);
  drum3.secondMix(1.0);
  drum3.pitchMod(5.0);

  drum4.frequency(4000);
  drum4.length(100);
  drum4.secondMix(0.0);
  drum4.pitchMod(0.5);

  sgtl5000_1.enable();
  sgtl5000_1.volume(0.5);

  AudioInterrupts();

}
/*---------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------*/
/*--------------------------ok here we go------------------------------------------*/
/*---------------------------------------------------------------------------------*/
void loop()
{
  //read potentiometers

 
  //inputs
  byte thisSwitch = thisSwitch_justPressed();

  if (thisSwitch < 8)
  {
    switch (patternSelect)
    {
      case 0:
        pattern1[thisSwitch] = !pattern1[thisSwitch];
        break;
      case 1:

        pattern2[thisSwitch] = !pattern2[thisSwitch];
        break;
      case 2:
        pattern3[thisSwitch] = !pattern3[thisSwitch];
        break;
      case 3:
        pattern4[thisSwitch] = !pattern4[thisSwitch];
        break;
    }
  }

  else

  {
    switch (thisSwitch)
    {
      case 8:

        patternSelect = 0;
        Serial.println("switch 9 just pressed ");
        break;

      case 9:

        patternSelect = 1;
        Serial.println("switch 10 just pressed");
        break;

      case 10:

        patternSelect = 2;
        Serial.println("switch 11 just pressed");
        break;

      case 11:

        patternSelect = 3;
        Serial.println("switch 12 just pressed");
        break;

    }
  }

  /*---------------------------------------------------------------------------------*/
  /*-----------------------------------sequencer-------------------------------------*/
  /*---------------------------------------------------------------------------------*/
  static uint32_t num = 0;

  if (millis() == next)
  {
    int val = analogRead(A12);
    drum3.frequency(map(val, 0, 1023, 20, 10000));
    Serial.print(val);
    Serial.print("     ");
    Serial.println();
    beat = num % 8;
    if (pattern1[beat])
      drum1.noteOn();
    if (pattern2[beat])
      drum2.noteOn();
    if (pattern3[beat])
      drum3.noteOn();
    if (pattern4[beat])
      drum4.noteOn();
    num++;
    next = millis() + tempo;
  }
}

/*---------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------*/
byte thisSwitch_justPressed()
{
  byte thisSwitch = 255;
  check_buttons();  //check the switches & get the current state
  for (byte i = 0; i < NUMBUTTONS; i++) {
    current_keystate[i] = justpressed[i];
    if (current_keystate[i] != previous_keystate[i]) {
      if (current_keystate[i]) thisSwitch = i;
    }
    previous_keystate[i] = current_keystate[i];
  }
  return thisSwitch;
}
/*---------------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------------*/
void check_buttons()
{
  static byte previousstate[NUMBUTTONS];
  static byte currentstate[NUMBUTTONS];
  static long lasttime;
  byte index;

  if (millis() < lasttime) {
    lasttime = millis(); // we wrapped around, lets just try again
  }

  if ((lasttime + DEBOUNCE) > millis()) {
    return; // not enough time has passed to debounce
  }
  // ok we have waited DEBOUNCE milliseconds, lets reset the timer

  lasttime = millis();

  for (index = 0; index < NUMBUTTONS; index++)
  {
    justpressed[index] = 0;       // when we start, we clear out the "just" indicators
    justreleased[index] = 0;

    currentstate[index] = digitalRead(buttons[index]);   // read the button
    if (currentstate[index] == previousstate[index]) {
      if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
        // just pressed
        justpressed[index] = 1;
      }
      else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
        // just released
        justreleased[index] = 1;
      }
      pressed[index] = !currentstate[index];  // remember, digital HIGH means NOT pressed
    }
    //Serial.println(pressed[index], DEC);
    previousstate[index] = currentstate[index];   // keep a running tally of the buttons
  }
}
/*---------------------------------------------------------------------------------*/
 
Try putting analogRead at the top of the loop, you have it in an if statement.
Also, declare it as a float, like in the waveform example:
float val = (float)analogRead(A12) / 1023.0;
drum3.frequency(20 + val * 10000);
Then add your serial code.
Hope that helps
 
Also you should avoid "if" statements with "==" for timing in polling mode.
Better you try "if (millis() >= next)" .
I don't know the runtime of your code, but so you are sure not being 1ms (or more) over your "next"
 
both good suggestions.
They both have improved the code but not solved the potentiometer problem.
i shall triple check the wiring and see if i can get a pot to work.
Thanks very much for your assistance.
 
Status
Not open for further replies.
Back
Top