I have spent countless hours trying to get Teensy 3.5 with the audio shield to work together with accelstepper. i get the most bizarre results. the following sketch shows what would be expected in the serial monitor but stepper motion has nothing to do with the audio input. i have the following pins connected between the audio shield and the teensy, i do not have all pins connected because other things are going on another shield that connects the stepper motor. 3.3 volts from the teensy to the audio shield. gnd, and pins 23, 22 13 11 and 9 sending audio in from a ascam recorder playing an MP3 file. serial monitor shows detection of peaks and 0 when no signal input but all i get is an oscillation of the motor even with the signal off the onboard LED flickers all the time which also seems strange. not sure shy pin 13 was used for RX since it is connected to the LED?
i have tried different ordering of the includes but this does not seem to matter. Is there some conflict between accelstepper and the audio library? i have not tried yet but can try running the stepper directly without the accelstepper library.
i have tried different ordering of the includes but this does not seem to matter. Is there some conflict between accelstepper and the audio library? i have not tried yet but can try running the stepper directly without the accelstepper library.
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include <AccelStepper.h>
const int myInput = AUDIO_INPUT_LINEIN;
AudioInputI2S audioInput; // audio shield: mic or line-in
AudioAnalyzePeak peak_L;
AudioAnalyzePeak peak_R;
AudioAnalyzeRMS rms_L;
AudioAnalyzeRMS rms_R;
AudioOutputI2S audioOutput; // audio shield: headphones & line-out
AudioConnection c1(audioInput, 0, peak_L, 0);
AudioConnection c2(audioInput, 1, peak_R, 0);
AudioConnection c3(audioInput, 0, rms_L, 0);
AudioConnection c4(audioInput, 1, rms_R, 0);
//AudioConnection c5(audioInput, 0, audioOutput, 0);
//AudioConnection c6(audioInput, 1, audioOutput, 1);
AudioControlSGTL5000 audioShield;
AccelStepper stepper(AccelStepper::DRIVER,3,4);
void setup()
{
Serial.begin(57600);
Serial.println("begin setup");
AudioMemory(6);
audioShield.enable();
audioShield.inputSelect(myInput);
audioShield.volume(1);
stepper.setPinsInverted(true,true,true);
stepper.setMinPulseWidth(20);
stepper.setCurrentPosition(0) ;
stepper.setMaxSpeed(1000.0);
stepper.setAcceleration(1000.0);
while(!Serial && millis() < 2000 );
}
elapsedMillis fps;
uint8_t cnt=0;
int stroke;
int totalPeak;
void loop() {
if(fps > 4) {
if (peak_L.available() && peak_R.available() && rms_L.available() && rms_R.available()) {
fps=0;
float leftPeak = peak_L.read() * 30.0;
float rightPeak = peak_R.read() * 30.0;
float leftRMS = rms_L.read() * 30.0;
float rightRMS = rms_R.read() * 30.0;
float totalRms = (leftRMS + rightRMS);
totalPeak = leftPeak + rightPeak;
// stroke = totalRms;
stroke = totalPeak * 5;
Serial.print("stroke ");
Serial.println(stroke);
if (stroke > 20){
stepper.runToNewPosition(stroke);
}
}
}
}