Audio Board + rotary encoder issue?

Status
Not open for further replies.

dika

Member
Hello,
I have a strange behavior with using a Teensy 3.2 in combination with the audio board and the following code:
------------------------------------------------------------------------------------------------------------------------

#include <Bounce.h>

Bounce button0 = Bounce(0, 10);
Bounce button1 = Bounce(1, 10);


// libraries for Teensy and Teens Audio Shield
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>


// https://www.pjrc.com/teensy/gui/index.html?info=AudioAnalyzePeak#
// GUItool: begin automatically generated code
AudioPlaySdWav playSdWav1; //xy=224,134
AudioAnalyzePeak peak1; //xy=518,232
AudioAnalyzePeak peak2; //xy=526,311
AudioOutputI2S i2s1; //xy=837,146
AudioConnection patchCord1(playSdWav1, 0, i2s1, 0);
AudioConnection patchCord2(playSdWav1, 0, peak1, 0);
AudioConnection patchCord3(playSdWav1, 1, i2s1, 1);
AudioConnection patchCord4(playSdWav1, 1, peak2, 0);
AudioControlSGTL5000 sgtl5000_1; //xy=342,534
// GUItool: end automatically generated code


// Use these Pins with the Teensy Audio Shield
#define SDCARD_CS_PIN 10
#define SDCARD_MOSI_PIN 7
#define SDCARD_SCK_PIN 14


void setup() {
Serial.begin(9600);
delay(500);


AudioMemory(10);
sgtl5000_1.enable();
sgtl5000_1.volume(0.5);
SPI.setMOSI(SDCARD_MOSI_PIN);
SPI.setSCK(SDCARD_SCK_PIN);
if (!(SD.begin(SDCARD_CS_PIN))) {
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
delay(1000);

pinMode(0, INPUT);
pinMode(1, INPUT);

}

elapsedMillis msecs;

void loop() {


testenc();



if (playSdWav1.isPlaying() == false) {
Serial.println("Start playing");
//playSdWav1.play("SDTEST1.WAV");
//playSdWav1.play("SDTEST2.WAV");
//playSdWav1.play("SDTEST3.WAV");
playSdWav1.play("SDTEST4.WAV");
delay(10); // wait for library to parse WAV info
}

if (msecs > 15) {
if (peak1.available() && peak2.available()) {
msecs = 0;
float leftNumber = peak1.read();
float rightNumber = peak2.read();
//Serial.print(leftNumber);
//Serial.print(", ");
//Serial.print(rightNumber);
//Serial.println();

}
}
}


void testenc()
{
button0.update();
button1.update();

if (button0.fallingEdge()) { Serial.println("button 0 fallingEdge"); }
if (button1.fallingEdge()) { Serial.println("button 1 fallingEdge"); }
if (button0.risingEdge()) { Serial.println("button 0 risingEdge"); }
if (button1.risingEdge()) { Serial.println("button 1 risingEdge"); }

}
-------------------------------------------------------------------------------------------

using this combination the output on the serial port is like:

-------------------------------------------------------------------------------------------
...
button 1 fallingEdge
button 1 risingEdge
button 1 fallingEdge
button 1 risingEdge
button 1 fallingEdge
button 1 risingEdge
button 1 fallingEdge
...
-------------------------------------------------------------------------------------------

but no response from the button0!

When I use only the Teensy 3.2 WITHOUT the audioshield everything works as expected with the following 'naked' code:

-------------------------------------------------------------------------------------------
#include <Bounce.h>

Bounce button0 = Bounce(0, 10);
Bounce button1 = Bounce(1, 10);


void setup() {

Serial.begin(9600);
Serial.println("Encoder Test:");

pinMode(0, INPUT);
pinMode(1, INPUT);

}


void loop() {

testenc();
}

void testenc()
{
button0.update();
button1.update();

if (button0.fallingEdge()) {
Serial.println("button 0 fallingEdge");
//Keyboard.print("-");
}
if (button1.fallingEdge()) {
Serial.println("button 1 fallingEdge");
//Keyboard.print("+");
}
if (button0.risingEdge()) {
Serial.println("button 0 risingEdge");
//Keyboard.print("+");
}
if (button1.risingEdge()) {
Serial.println("button 1 risingEdge");
//Keyboard.print("+");
}

}


-------------------------------------------------------------------------------------------

and the expected result is:

-------------------------------------------------------------------------------------------
...
button 0 fallingEdge
button 1 fallingEdge
button 1 risingEdge
button 0 risingEdge
button 1 fallingEdge
button 1 risingEdge
button 0 fallingEdge
button 1 fallingEdge
button 0 risingEdge
button 0 fallingEdge
button 1 risingEdge
button 0 risingEdge
button 1 fallingEdge
button 0 fallingEdg
...
++++++++++++++++++++

So my rotary encoder obviously is ok.

What am I missing?
 
I tried running the code here. I don't have a rotary encoder & circuitry connected to any Teensy with audio board, so I changed the pinmode to INPUT_PULLUP and just touched a wire between GND and the pins.

It seems to work fine. Here's what I see:
 

Attachments

  • sc.png
    sc.png
    41.5 KB · Views: 120
Paul, many thanks for your quick answer.
If I understand the function of a rotary encoder correct button0 and button1 must trigger alternately. In your attachement I recognize, that button0 triggers appr. a dozend and than button1 a few more - may be an bouncing issue?
But anyhow, I forgot to mention that I work with external 10k Pullup (using Teensy 3.2 AND Audio Board), so I used pinmode INPUT. My fault of telling you this parameter.

Ofcourse I first also tried out the Encoder.h library, but still the same behavior with my project - thats the reason, why I try now Bounce.h.

So, I will have to investigate deeper, whats going on wrong with my circuit. I will build up a simple test circuit with two independed buttons instead of my rotary encoder () and will report later here.
Again, many thanks for your guidance and your mrvelous Teensy products!
 
If I understand the function of a rotary encoder correct button0 and button1 must trigger alternately. In your attachement I recognize, that button0 triggers appr. a dozend and than button1 a few more - may be an bouncing issue?

I tested only very quickly, by touching a wire between GND and pins 0 or 1. I just used a short wire, not a real rotary encoder. The screenshot above was done by just touching the wire to pin 0 a few times, then pin 1, and apparently I managed to accidentally touch pin 0 one more time as I pulled my hand away, back to the keyboard to make the screenshot and post a reply. Since you posted code using only Bounce and not Encoder, and the stated problem was no response on pin 0, this seemed like an ok way to test quickly. I answer quite a few questions every day, so when a hardware test is needed, anything I can do to save time really helps (at least on my end...)

If there's some need to actually test with a rotary encoder, I can dig one up or just order another from Digikey and do the test. But that will take me a little longer than a which check touching a piece of wire to hardware I already happen to have set up on my desk.

But at this point, it looks like pin 0 really is working. Is it possible the problem might be a just loose connection on your hardware?
 
...every thing is possible with my circuit ... ;-) I will let you know ASAP,
warm regards, Dirk
 
short update: Things getting better.
I modified the code and external circuit. So I get rid of external 10k pullup resistors to +5V (may be the issue?!) and changed back to the internal pullup resistors. Now I use ISR for Pins 0, 1, 2.
More details later (it's 10:45 pm local), warm regards Dirk
 
Status
Not open for further replies.
Back
Top