Audio and Steppermoter at the same time

Status
Not open for further replies.

Jenny99

Member
Hi There

I'm trying to breathe life into my two wheel robot made out of a tissue box, a 3.5 Teensy a USB-speaker and 2 stepper motors and several sensors..
See here: 20181224_163648.jpg


Each module like the audio and stepper-Motor are running fine in individual Sketches, x.ino's. But together they dont, any idea? ::
Code:
// MultiStepper.pde
// -*- mode: C++ -*-
//
// Shows how to multiple simultaneous steppers
// Runs one stepper forwards and backwards, accelerating and decelerating
// at the limits. Runs other steppers at the same time
//
// Copyright (C) 2009 Mike McCauley
// $Id: MultiStepper.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $

#include <AccelStepper.h>

// Define some steppers and the pins the will use
AccelStepper stepper1(AccelStepper::FULL4WIRE, 0, 1, 2, 3);; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
AccelStepper stepper2(AccelStepper::FULL4WIRE, 25,26, 27, 28);

// AUDIO
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
//https://online-audio-converter.com/de/
// WIn 10 Sprach recorder

AudioPlaySdWav           playWav1;
AudioOutputAnalog      audioOutput;
AudioConnection          patchCord1(playWav1, 0, audioOutput, 0);
AudioConnection          patchCord2(playWav1, 1, audioOutput, 1);
AudioControlSGTL5000     sgtl5000_1;

#define SDCARD_CS_PIN    BUILTIN_SDCARD
#define SDCARD_MOSI_PIN  11  // not actually used
#define SDCARD_SCK_PIN   13  // not actually used


void setup()
{  
    stepper1.setMaxSpeed(400.0);
    stepper1.setAcceleration(100.0);
    stepper1.moveTo(200);
    
    stepper2.setMaxSpeed(-400.0);
    stepper2.setAcceleration(100.0);
    stepper2.moveTo(-200);

    // AUDIO
  Serial.println("Start AUDIO...");

  AudioMemory(8);
  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);
    }
  }

// END AUDIO
playFile("SILENTN.WAV");
    
}

void playFile(const char *filename)
{
  Serial.print("Playing file: ");
  Serial.println(filename);
  playWav1.play(filename);
  delay(5);
  //while (playWav1.isPlaying()) {
  //}
}

void loop()
{
      stepper1.run();
      stepper2.run();
       
}
 
Last edited:
Does it work if playFile() is never called?

Putting CODE [ # Hashtag on reply box toolbar ] markup around the code portion preserves indentation and helps readability
 
See : // END AUDIO playFile("SILENTN.WAV");

Does it work if playFile() is never called?


See line where:
// END AUDIO
playFile("SILENTN.WAV");

Putting CODE [ # Hashtag on reply box toolbar ] markup around the code portion preserves indentation and helps readability



:::::::::::::::::::::::::::::::::::::::::::::::::::::::.
 
See: playFile("SILENTN.WAV");

Thanks for you help..

I do have called the playFile() ..
Any other idea?


The sound is actually playing but the stepper-motor is not turning… somehow I guess the Audio is disturbing stepper-motor lib/code. this is my guess,..


Cheers Marcel
 
The code shows that line active, not commented

The question was if not doing the playFile works then perhaps it's Audio interrupts
 
As building_sdcard is not using SPI, I would first remove all references to SPI
if you move the AccelStepper declarations after AudioControlSGTL5000 does then the audio play or does the stepper work?
 
As building_sdcard is not using SPI, I would first remove all references to SPI
if you move the AccelStepper declarations after AudioControlSGTL5000 does then the audio play or does the stepper work?

Nice hint!
Now I am getting closer,.. with the following code the sound does loop{ play for 5000ms and the stepper-motor's do 1 step}. Still not at the same time but, better..

Interrupt was mentioned as well.. how to control or adapt it?


Code:
#include <AccelStepper.h>
// Define some steppers and the pins the will use
AccelStepper stepper1(AccelStepper::FULL4WIRE, 0, 1, 2, 3);; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
AccelStepper stepper2(AccelStepper::FULL4WIRE, 25,26, 27, 28);


// AUDIO
#include <Audio.h>
#include <SD.h>
#include <SerialFlash.h>
//https://online-audio-converter.com/de/
// WIn 10 Sprach recorder

AudioPlaySdWav           playWav1;
AudioOutputAnalog        audioOutput;
AudioConnection          patchCord1(playWav1, 0, audioOutput, 0);
AudioConnection          patchCord2(playWav1, 1, audioOutput, 1);
AudioControlSGTL5000     sgtl5000_1;
#define SDCARD_CS_PIN    BUILTIN_SDCARD

void setup()
{  
    stepper1.setMaxSpeed(600.0);
    stepper1.setAcceleration(500.0);
    stepper1.moveTo(1000);
    
    stepper2.setMaxSpeed(-600.0);
    stepper2.setAcceleration(500.0);
    stepper2.moveTo(-1000);

    // AUDIO
  Serial.println("Start AUDIO...");

  AudioMemory(8);
  //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);
    }
  }
// END AUDIO


    
}

void playFile(const char *filename)
{
  Serial.print("Playing file: ");
  Serial.println(filename);
  playWav1.play(filename);
  delay(5);
  //while (playWav1.isPlaying()) {
  //}
}

void loop()
{
      playFile("SILENTN.WAV");
      delay(5000);
      stepper1.run();
      stepper2.run();
          
}
 
AccelStepper requires you to call stepper.run() as often as possible, at least much more often than the step frequency. Due to the delay(5000) in your loop the stepper.run() methods are called only every 5s which will not work.

BTW: That stepper.run() function is quite often confusing users. Should really be named .tick() or something...
 
Status
Not open for further replies.
Back
Top