Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problem using hifi mode with serial communication #11

Open
Hactari opened this issue Jul 17, 2013 · 4 comments
Open

problem using hifi mode with serial communication #11

Hactari opened this issue Jul 17, 2013 · 4 comments

Comments

@Hactari
Copy link

Hactari commented Jul 17, 2013

Hi,
First of all my compliments for this brilliant library.

I am using mozzi to create an electronic hurdy gurdy, where I am generating the sound of two strings with mozzi, based on the speed of an encoder. I use 1 Arduino for running mozzi (Duemilanove with ATmega168), and a second Arduino to read the encoder (Arduino Pro Mini 328), because I didn't want to mess up Mozzi with the interrupts that I need for the encoder.

On the second Arduino I calculate the speed of the encoder, and I send it over a serial link to the first arduino, to change the sound based on the speed.

I set everything up in 'standard' mode, and it was working fine (except for my wife complaining about the high-pitched squeal). Then I switched to hifi mode, including the proposed filtering. I checked this on a single Arduino, and it works fine.

BUT: for some reason my serial communication has stopped working. I have double checked, and it seems that switching to hifi mode kills my serial communication. Is this a known fact with an existing work around? Advice is much appreciated.

Jaap

@sensorium
Copy link
Owner

Hi Jaap,
are you just using Serial.read(), or another protocol? I'll have to look closer depending on how you're doing it, but it's likely that the processor is being blocked with delays or loops reading the serial. The way to fix this, which also happens with the standard Arduino analog reading and I2C communication, has been to look at the Arduino code and separate the part which initiates a read from the part which waits for a response, calling the first in Mozzi's updateControl() so it can happen behind the scenes and getting the result next time updateControl() is called.
If you want to send a bit more detail, I'll be able to be more specific.
Tim

@Hactari
Copy link
Author

Hactari commented Jul 19, 2013

Hi Tim,

Thanks a lot for your quick response. I was expecting it to be something
like this indeed.

I am just using Serial.read indeed. There is a copy of my scripts below:

Cheers, Jaap

---------------------------------------------- first arduino

/*
ToDo: include encoder resolution to translate pulses to actual rotation
speed
*/

#include <PinChangeInt.h>
#define PIN1 2

uint8_t latest_interrupted_pin;
uint8_t interrupt_count[20]={0}; // 20 possible arduino pins

// the following function is called when an interrupt occurs
void quicfunc() {
latest_interrupted_pin=PCintPort::arduinoPin;
interrupt_count[latest_interrupted_pin]++;
};

void setup() {
pinMode(PIN1, INPUT); digitalWrite(PIN1, HIGH); //
PCintPort::attachInterrupt(PIN1, &quicfunc, FALLING); // add interrupt
Serial.begin(115200); // start serial
communication
}

uint8_t counts;
uint8_t old_counts;

void loop() {
old_counts = counts;
counts = interrupt_count[PIN1];

Serial.write(counts); //send speed
// Serial.print(";");
// Serial.print(counts - old_counts, DEC); // send acceleration
// Serial.println(";");

interrupt_count[PIN1] = 0;
delay(100);
}


------------------------------------- mozzi arduino

/*
first try at making a hurdy gurdy sound

next step:
make realistic drone sound
improve trumpet sound
add hardware filter or use HiFi

output pin: 9
HIFI mode: output pins 9 and 10

*/

//#define AUDIO_MODE HIFI
#include <MozziGuts.h>
#include <Oscil.h> // oscillator template
#include <tables/sin2048_int8.h> // sine table for oscillator
#include <tables/saw2048_int8.h> // saw table for oscillator
#include <MozziGuts.h>
#include <mozzi_utils.h>
#include <mozzi_analog.h>

// use: Oscil <table_size, update_rate> oscilName (wavetable)
Oscil <SAW2048_NUM_CELLS, AUDIO_RATE> aSaw(SAW2048_DATA);
Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin(SIN2048_DATA);

// use #define for CONTROL_RATE, not a constant
#define CONTROL_RATE 64 // powers of 2 please
//unsigned char gain = 255;
uint8_t val;

void setup(){
startMozzi(CONTROL_RATE); // set a control rate of 64 (powers of 2 please)
aSaw.setFreq(2*392); // set the frequency
aSin.setFreq(392); // set the frequency
adcEnableInterrupt();
Serial.begin(115200);
}

void updateControl(){
// put changing controls in here
//adcReadAllChannels();
//gain = adcGetResult(3)/4;
//Serial.println(gain);

       if (Serial.available()) {
            val = Serial.read();
            analogWrite(11,val);
      //      Serial.println(val);
    //   for (i=0;i<val;i++){
    //    analogWrite(11,255);
    //    delay(200);
    //    analogWrite(11,0);
    //    delay(200);
    // }

    }

val = 255; // test the sound, when arduino with encoder is not
connected

}

int updateAudio(){
return ( (aSin.next() + aSaw.next() )* val)>>8;
}

void loop(){
audioHook(); // required here
}


On Fri, Jul 19, 2013 at 7:39 AM, Mr Sensorium notifications@github.comwrote:

Hi Jaap,
are you just using Serial.read(), or another protocol? I'll have to look
closer depending on how you're doing it, but it's likely that the processor
is being blocked with delays or loops reading the serial. The way to fix
this, which also happens with the standard Arduino analog reading and I2C
communication, has been to look at the Arduino code and separate the part
which initiates a read from the part which waits for a response, calling
the first in Mozzi's updateControl() so it can happen behind the scenes and
getting the result next time updateControl() is called.
If you want to send a bit more detail, I'll be able to be more specific.
Tim


Reply to this email directly or view it on GitHubhttps://github.com//issues/11#issuecomment-21232361
.

@sensorium
Copy link
Owner

Hi Jaap,
just had a quick search and found NBSerial library, for non-blocking serial- there's version 2.5 at the bottom of this page:
http://forum.arduino.cc/index.php?topic=70623.0
It would be great to know if this works for you. It looks like there are a few other similar libraries around which might also be worth looking at.
Good luck!
Tim

@Hactari
Copy link
Author

Hactari commented Jul 19, 2013

That's great Tim. I can't really try it out tonight, as I'm camping for the weekend. But I'll test it as soon as I can get my laptop online (and out of sight of my wife ;-).

Jaap

Mr Sensorium notifications@github.com wrote:

Hi Jaap,
just had a quick search and found NBSerial library, for non-blocking serial- there's version 2.5 at the bottom of this page:
http://forum.arduino.cc/index.php?topic=70623.0
It would be great to know if this works for you. It looks like there are a few other similar libraries around which might also be worth looking at.
Good luck!
Tim


Reply to this email directly or view it on GitHub:
#11 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants