Mozzi sample

413 views
Skip to first unread message

AndreasC81

unread,
Feb 14, 2015, 4:11:26 PM2/14/15
to mozzi...@googlegroups.com
Hi

I'm trying to get a code to work so I can play a sample on my Arduino Nano.

The code looks like this:

#include <MozziGuts.h>
#include <SampleHuffman.h>
#include "umpah_huff.h"
SampleHuffman umpah(UMPAH_SOUNDDATA,UMPAH_HUFFMAN,UMPAH_SOUNDDATA_BITS);

const int buttonPin = 7;
int buttonState = 0;

void setup() {
  umpah.setLoopingOn();
  startMozzi();
   
  pinMode(buttonPin, INPUT);
}

void updateControl(){

}

int updateAudio(){
  return umpah.next();
}

void loop() {
  audioHook();
 
  buttonState = digitalRead(buttonPin);
  if (buttonState == LOW) {
    unPauseMozzi();
  } else {
    pauseMozzi();
  }
}


As can be seen in the code, I'm trying to control start and stop with the push of a button. Problem is that it doesn't work so good. I mean, the Arduino responds to the button press and button release but the sound doesn't come out so well. It sounds awful and not at all like it should. This leads me to Believe that there is something I'm doing wrong in the code, as it works very well without the IF statement.

What's is wrong with the code?

Thanks.

Tim Barrass

unread,
Feb 14, 2015, 7:50:38 PM2/14/15
to mozzi...@googlegroups.com
Hi,
your button code is control code, so put it in updateControl(), not loop().  Avoid putting any extra code in loop, as this needs to run as fast as possible to fill the audio buffer. 

Also, make the button trigger only on transitions, so Mozzi will pause or unpause just once each time the button state changes- something like..

if((buttonstate != previousButtonState) {

   if (buttonState == LOW) {
      unPauseMozzi();
    } else {
      pauseMozzi();
    }

}

I hope that works for you

Tim


--
You received this message because you are subscribed to the Google Groups "Mozzi-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mozzi-users...@googlegroups.com.
To post to this group, send email to mozzi...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/mozzi-users/2409d053-8f1f-4109-bffb-8468a7c61af3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

AndreasC81

unread,
Feb 14, 2015, 9:39:51 PM2/14/15
to mozzi...@googlegroups.com
Thanks

One question. How am I going to declare 'buttonstate'? int buttonState = 0; doesn't seem to work.

AndreasC81

unread,
Feb 15, 2015, 6:30:59 AM2/15/15
to mozzi...@googlegroups.com
I seem to have a huge problem getting this to work.

Thing is that without any if-statement, everything works fine. The sample loops indefinitely without any problems. The problem is that with an if-statement, no matter where I put it in the code or how I write it, the sound from the speaker sounds almost like when you have bad reception on your radio. Sometimes you can hear the sample behind all that noise and sometimes you just hear noise. Can this have something to do with available SRAM or something else entirely? I can't understand why it works so poorly just because I add an if-statement when it works so great without it. How can a single if-statement make things work like this?

Tim Barrass

unread,
Feb 15, 2015, 7:01:23 AM2/15/15
to mozzi...@googlegroups.com
Hi Andreas,
this works for me on a Nano, though it might be better to just stop playing the sample than to turn the whole system off and on like this with pauseMozzi() and unPauseMozzi(), which gives a click each time it starts and stops while playing the sample.



#include <MozziGuts.h>
#include <SampleHuffman.h>
#include "umpah_huff.h"

#define STOP_PIN 4

SampleHuffman umpah(UMPAH_SOUNDDATA,UMPAH_HUFFMAN,UMPAH_SOUNDDATA_BITS);

void setup() {
  umpah.setLoopingOn();
  startMozzi();
}


void updateControl(){
  static int previous;
  int current = digitalRead(STOP_PIN);
  if(previous==LOW && current==HIGH){
    pauseMozzi();
  }else if(previous==HIGH && current==LOW){
    unPauseMozzi();
  }
  previous=current;

}


int updateAudio(){
  return umpah.next();
}


void loop() {
  audioHook();
}

AndreasC81

unread,
Feb 15, 2015, 7:24:08 AM2/15/15
to mozzi...@googlegroups.com
Well, it works better. Now I can hear the sample clearly without any noise. But it doesn't work perfectly. If I push the button and hold it for a while, then release, then wait some time before I repeat the whole process, it works, but if I'm too fast, like pressing the button and then release emediately after andI then press the button again nothing can be heard in the speaker.

Also, as I use pauseMozzi() and unPauseMozzi, it does what it says. When I unpause, the sample continues from where it was paused. I'd much rather like to have it so it starts over from the beginning when I push the button. You mentioned that I could stop playing the sample instead of pausing Mozzi. Can I start playing the sample from the beginning with a cossesponding function and what does those functions look like?

AndreasC81

unread,
Feb 15, 2015, 2:22:31 PM2/15/15
to mozzi...@googlegroups.com
I solved it with a transistor since I didn't seem to be able to solve it with code.

I write:

void updateControl(){
static int previous;
int current = digitalRead(STOP_PIN);
if(previous==HIGH && current==LOW){
digitalWrite(activatePin, HIGH);
start();
}else if(previous==LOW && current==HIGH){
digitalWrite(activatePin, LOW);
}
previous=current;

}

With this code, the sample will loop when power is applied, but since pin 9 is wired to the collector on an npn-transistor with it's emitter wired to the audio amplification stage, there's going to be no sound from the speaker as long as the transistor is off. When pin 7 goes LOW, pin 8 which is wired to the base of the transistor goes HIGH, thus turning the transistor on which will send the sound signal to the amplification stage. At the same time, the sample starts from the beginning on each button press using start();

This will behave the same way as if I was doing start stop in code, but since I couldn't figure out how to do it, if even possible, I came up with this idea instead.

Thank you for all help, you helped me to reach this solution.
Reply all
Reply to author
Forward
0 new messages