How to use Mozzi with a capacitive input ?

524 views
Skip to first unread message

George Profenza

unread,
Jan 21, 2015, 8:33:27 AM1/21/15
to mozzi...@googlegroups.com
Hello,

I'm new to Mozzi and so far it sounds great!

The problem I have is I'm trying to use analog pins as capacitive sensors.
So far I've looked at the Teensy Capacitive Library and ADCTouch.
Capacitive Library uses millis() internally and I couldn't get that to work with Mozzi so far.

ADCTouch looks like a simpler implementation and I had a stab at getting it to work in a sketch with Mozzi,
but since things advanced arduino notions like interrupts/port manipulations/etc. are over my head at the moment,
I'm having a hard time trying to get this playing nicely.

Here's how I tried to change the ADCTouch code to work with Mozzi:
int ADCTouchRead(byte ADCChannel, int samples = 100)
{
 
long _value = 0;
 
for(int _counter = 0; _counter < samples; _counter ++)
 
{
 pinMode
(ADCChannel, INPUT_PULLUP);
 
 ADMUX
|=   0b11111;
 ADCSRA
|= (1<<ADSC); //start conversion
 
while(!(ADCSRA & (1<<ADIF))); //wait for conversion to finish
 ADCSRA
|= (1<<ADIF); //reset the flag
 
 pinMode
(ADCChannel, INPUT);
 _value
+= mozziAnalogRead(ADCChannel);
 
}
 
return _value / samples;
}


To be honest I'm not sure how this code actually work. Here's a modified version of the Control_Gain sample I'm using at the moment:
/*  Example changing the gain of a sinewave,
    using Mozzi sonification library.
  
    Demonstrates the use of a control variable to influence an
    audio signal.
  
    Circuit: Audio output on digital pin 9 on a Uno or similar, or
    DAC/A14 on Teensy 3.0/3.1, or 
    check the README or http://sensorium.github.com/Mozzi/
  
    Mozzi help/discussion/announcements:
  
    Tim Barrass 2012, CC by-nc-sa.
*/

#include <MozziGuts.h>
#include <Oscil.h> // oscillator template
#include <tables/sin2048_int8.h> // sine table for oscillator

// use: Oscil <table_size, update_rate> oscilName (wavetable), look in .h file of table #included above
Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin(SIN2048_DATA);

// control variable, use the smallest data size you can for anything used in audio
byte gain = 255;

//ADC Touch setup
int ref0, ref1;       //reference values to remove offset

int ADCTouchRead(byte ADCChannel, int samples = 100)
{
long _value = 0;
for(int _counter = 0; _counter < samples; _counter ++)
{
pinMode(ADCChannel, INPUT_PULLUP);
ADMUX |=   0b11111;
ADCSRA |= (1<<ADSC); //start conversion
while(!(ADCSRA & (1<<ADIF))); //wait for conversion to finish
ADCSRA |= (1<<ADIF); //reset the flag
pinMode(ADCChannel, INPUT);
_value += mozziAnalogRead(ADCChannel);
}
return _value / samples;
}


void setup(){
  startMozzi(); // start with default control rate of 64
  aSin.setFreq(3320); // set the frequency
  
  ref0 = ADCTouchRead(A2, 500);    //create reference values to 
  ref1 = ADCTouchRead(A3, 500);      //account for the capacitance of the pad
}


void updateControl(){
  // as byte, this will automatically roll around to 255 when it passes 0
  if(gain > 3){
    gain = gain - 3 ; 
  }else{
    gain = 0;
  }
  //*
//  pauseMozzi();
    int value0 = ADCTouchRead(A2);   //no second parameter
    int value1 = ADCTouchRead(A3);     //   --> 100 samples

    value0 -= ref0;       //remove offset
    value1 -= ref1;
    
    if(value0 > 40) {
      aSin.setFreq(3320); // set the frequency
      gain = 255;
    }
    if(value1 > 40) {
      aSin.setFreq(3760);
      gain = 255;
    }
//  unPauseMozzi();
  //*/
}


int updateAudio(){
  return (aSin.next()* gain)>>8; // shift back to STANDARD audio range, like /256 but faster
}


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


This causes Mozzi to be silent though. If I comment out the ADCTouchRead calls, I can hear Mozzi again.

I found a couple of related posts on this group: I2C + Mozzi and Problem with ADSR.
I also peaked at the RCPoll sample which is using digitalRead(). 
Do I need do something similar to this, but use analogRead() ?

What is the easiest way to use a capacitive library with Mozzi ?

Thank you,
George

nescivi

unread,
Jan 21, 2015, 2:31:53 PM1/21/15
to mozzi...@googlegroups.com
Hi,

analog conversion takes time - if you do it a hundred times, it might be
slower than the audio update rate, and thus delay the production of an
audio sample.

If you want to get an average over a hundred samples, you should get one
sample per control update, and then take the average.

And only at the 100th sample do something with your measured data.

sincerely,
Marije

On 21-01-15 14:33, George Profenza wrote:
> Hello,
>
> I'm new to Mozzi and so far it sounds great!
>
> The problem I have is I'm trying to use analog pins as capacitive sensors.
> So far I've looked at the Teensy Capacitive Library
> <http://www.pjrc.com/teensy/td_libs_CapacitiveSensor.html> and ADCTouch
> <http://playground.arduino.cc/Code/ADCTouch>.
> Capacitive Library uses millis() internally and I couldn't get that to
> work with Mozzi so far.
>
> ADCTouch looks like a simpler implementation and I had a stab at getting
> it to work in a sketch with Mozzi,
> but since things advanced arduino notions like interrupts/port
> manipulations/etc. are over my head at the moment,
> I'm having a hard time trying to get this playing nicely.
>
> Here's how I tried to change the ADCTouch code to work with Mozzi:
> |
> intADCTouchRead(byteADCChannel,intsamples =100)
> {
> long_value =0;
> for(int_counter =0;_counter <samples;_counter ++)
> {
> pinMode(ADCChannel,INPUT_PULLUP);
>
> ADMUX |= 0b11111;
> ADCSRA |=(1<<ADSC);//start conversion
> while(!(ADCSRA &(1<<ADIF)));//wait for conversion to finish
> <https://groups.google.com/forum/#!searchin/mozzi-users/capacitive/mozzi-users/ttbeSUC38kA/lb7rBaXO5NUJ> and
> Problem with ADSR
> <https://groups.google.com/forum/#!searchin/mozzi-users/capacitive/mozzi-users/PZib2lz3kAA/eqqY6YYeVg4J>.
> I also peaked at the RCPoll sample which is using digitalRead().
> Do I need do something similar to this, but use analogRead() ?
>
> What is the easiest way to use a capacitive library with Mozzi ?
>
> Thank you,
> George
>
> --
> 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
> <mailto:mozzi-users...@googlegroups.com>.
> To post to this group, send email to mozzi...@googlegroups.com
> <mailto:mozzi...@googlegroups.com>.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/mozzi-users/88cb42e9-a99c-4d9f-91da-7e4c66decffb%40googlegroups.com
> <https://groups.google.com/d/msgid/mozzi-users/88cb42e9-a99c-4d9f-91da-7e4c66decffb%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

George Profenza

unread,
Jan 21, 2015, 6:39:38 PM1/21/15
to mozzi...@googlegroups.com
Hi Marije,

Thank you for the insight. Sounds very useful.

In the morning when I have access to the hardware 
I'll first try lowering the ADCTouch sample rate and see how that affects
precision, but simply hearing clear sweet Mozzi sounds will be a good start.

I'll also try counting samples as you've mentioned and taking an average later.
I may get back with more questions, but in the meantime,
thank you for the handy tips!

George Profenza

unread,
Jan 23, 2015, 10:17:09 AM1/23/15
to mozzi...@googlegroups.com
Hi Marije,

I've tried following your suggestion. 
Simply lowering the number of ADCTouch samples didn't resolve the issue.

I've also tried counting the samples separately, without using the blocking for loop,
tried removing the while loop, but even when waiting for a single sample, the sound
is very distorted (noisy, cracks).

Is there any way to do the ADCTouch part in a 'separate thread' somehow, not to block audio ?
(I'm new to embedded programming, so don't know what the equivalent is).

Independent of ADCTouch, has anyone managed to get Mozzi working with a capacitive sensor ?
If so, how ?

Thanks,
George


On Wednesday, 21 January 2015 19:31:53 UTC, nescivi wrote:

nescivi

unread,
Jan 23, 2015, 10:28:00 AM1/23/15
to mozzi...@googlegroups.com
Hi George,

the issues where things are blocking audio are:

> while(!(ADCSRA &(1<<ADIF)));//wait for conversion to finish

This is a busy wait loop that can easily take a few ms if you do it a
hundred times.

In this part, I am not sure what the backend implementation is:

> _value +=mozziAnalogRead(ADCChannel);


Basically what you want to do, is each control cycle (or even audio
cycle), is:

// check if the ADC conversion is finished.

if ( ADCSRA &(1<<ADIF) ) {

then read the value, increase the counter, and start the next conversion:

_value +=mozziAnalogRead(ADCChannel);
counter++
ADMUX |= 0b11111;
ADCSRA |=(1<<ADSC);//start conversion

// If the counter is at the number of samples, you take the average.

if ( counter == samples ){
capacitiveValue = _value / samples;
counter = 0;
}

}

Or don't do the divide and just live with the scaled version. Divisions
can be expensive in terms of calculation power.
If you take a number of samples that is a power of two, you could do
bitshifting instead.

sincerely,
Marije
> > an email to mozzi-users...@googlegroups.com <javascript:>
> > <mailto:mozzi-users...@googlegroups.com <javascript:>>.
> > To post to this group, send email to mozzi...@googlegroups.com
> <javascript:>
> > <mailto:mozzi...@googlegroups.com <javascript:>>.
> <https://groups.google.com/d/msgid/mozzi-users/88cb42e9-a99c-4d9f-91da-7e4c66decffb%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/optout>.
>
> --
> 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
> <mailto:mozzi-users...@googlegroups.com>.
> To post to this group, send email to mozzi...@googlegroups.com
> <mailto:mozzi...@googlegroups.com>.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/mozzi-users/9a4e618d-3d85-4402-a1df-1a868be9702e%40googlegroups.com
> <https://groups.google.com/d/msgid/mozzi-users/9a4e618d-3d85-4402-a1df-1a868be9702e%40googlegroups.com?utm_medium=email&utm_source=footer>.

George Profenza

unread,
Jan 23, 2015, 12:52:51 PM1/23/15
to mozzi...@googlegroups.com
Hello Marije,

The division tip was golden! Thank you for that.
I've completely dropped the ADCSRA &(1<<ADIF) check
and I'm not doing division, just looking for larger values and reseting the accumulated value.

I can now play a tone using a capacitive trigger, woo hoo!
There is bit of a lag (about half a second), but it's better than nothing :)

I am now trying to use two more pins, but this seems to cause issues again.
Would you happen to have a few more tricks to speed things up on the ADCTouch side ?

Thanks again for your tips, great stuff!
>     > <mailto:mozzi-users+unsub...@googlegroups.com <javascript:>>.
>     > To post to this group, send email to mozzi...@googlegroups.com
>     <javascript:>
>     > <mailto:mozzi...@googlegroups.com <javascript:>>.
>     > To view this discussion on the web, visit
>     >
>     https://groups.google.com/d/msgid/mozzi-users/88cb42e9-a99c-4d9f-91da-7e4c66decffb%40googlegroups.com
>     <https://groups.google.com/d/msgid/mozzi-users/88cb42e9-a99c-4d9f-91da-7e4c66decffb%40googlegroups.com>
>
>     >
>     <https://groups.google.com/d/msgid/mozzi-users/88cb42e9-a99c-4d9f-91da-7e4c66decffb%40googlegroups.com?utm_medium=email&utm_source=footer
>     <https://groups.google.com/d/msgid/mozzi-users/88cb42e9-a99c-4d9f-91da-7e4c66decffb%40googlegroups.com?utm_medium=email&utm_source=footer>>.
>
>     > For more options, visit https://groups.google.com/d/optout
>     <https://groups.google.com/d/optout>.
>
> --
> 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

nescivi

unread,
Jan 23, 2015, 5:56:15 PM1/23/15
to mozzi...@googlegroups.com
Hiho,

basically, before each conversion, you switch to one of the other
channels, and when you store the value, you store it in the assiocated
variable (or add to it).

Putting the ADC check in the audioLoop (don't recall exactly how that
went within Mozzi, it's been a while) will speed things up.

sincerely,
Marije
> > > <mailto:mozzi-users...@googlegroups.com
> <javascript:> <javascript:>>.
> > > To post to this group, send email to mozzi...@googlegroups.com
> > <javascript:>
> > > <mailto:mozzi...@googlegroups.com <javascript:>>.
> > > To view this discussion on the web, visit
> > >
> >
> https://groups.google.com/d/msgid/mozzi-users/88cb42e9-a99c-4d9f-91da-7e4c66decffb%40googlegroups.com
> <https://groups.google.com/d/msgid/mozzi-users/88cb42e9-a99c-4d9f-91da-7e4c66decffb%40googlegroups.com>
>
> >
> <https://groups.google.com/d/msgid/mozzi-users/88cb42e9-a99c-4d9f-91da-7e4c66decffb%40googlegroups.com
> <https://groups.google.com/d/msgid/mozzi-users/88cb42e9-a99c-4d9f-91da-7e4c66decffb%40googlegroups.com>>
>
> >
> > >
> >
> <https://groups.google.com/d/msgid/mozzi-users/88cb42e9-a99c-4d9f-91da-7e4c66decffb%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/mozzi-users/88cb42e9-a99c-4d9f-91da-7e4c66decffb%40googlegroups.com?utm_medium=email&utm_source=footer>
>
> >
> <https://groups.google.com/d/msgid/mozzi-users/88cb42e9-a99c-4d9f-91da-7e4c66decffb%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/mozzi-users/88cb42e9-a99c-4d9f-91da-7e4c66decffb%40googlegroups.com?utm_medium=email&utm_source=footer>>>.
>
> >
> > > For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>
> > <https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>>.
> >
> > --
> > 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 <javascript:>
> > <mailto:mozzi-users...@googlegroups.com <javascript:>>.
> > To post to this group, send email to mozzi...@googlegroups.com
> <javascript:>
> > <mailto:mozzi...@googlegroups.com <javascript:>>.
> > To view this discussion on the web, visit
> >
> https://groups.google.com/d/msgid/mozzi-users/9a4e618d-3d85-4402-a1df-1a868be9702e%40googlegroups.com
> <https://groups.google.com/d/msgid/mozzi-users/9a4e618d-3d85-4402-a1df-1a868be9702e%40googlegroups.com>
>
> >
> <https://groups.google.com/d/msgid/mozzi-users/9a4e618d-3d85-4402-a1df-1a868be9702e%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/mozzi-users/9a4e618d-3d85-4402-a1df-1a868be9702e%40googlegroups.com?utm_medium=email&utm_source=footer>>.
>
> > For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
> --
> 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
> <mailto:mozzi-users...@googlegroups.com>.
> To post to this group, send email to mozzi...@googlegroups.com
> <mailto:mozzi...@googlegroups.com>.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/mozzi-users/5dd4d2bb-5e34-4514-91c3-1bc7e9a22d94%40googlegroups.com
> <https://groups.google.com/d/msgid/mozzi-users/5dd4d2bb-5e34-4514-91c3-1bc7e9a22d94%40googlegroups.com?utm_medium=email&utm_source=footer>.

Tim Barrass

unread,
Jan 23, 2015, 11:58:53 PM1/23/15
to mozzi...@googlegroups.com
Hi,

the RCpoll sketch works for me with a free wire attached as a sensor to a pin (d5 in the sketch), and with the sensor.next() line moved to updateAudio() for improved resolution as suggested by Marije, and the freq variable made global instead of local.  It could definitely be improved with a filter in updateAudio(eg. Mozzi Smooth() or RollingAverage()), and your circuit might be different with a separate send and receive pin instead of using the internal pull up resistor, which would require a slight mod to RCpoll - maybe a modified CapPoll would be useful in a future release.

You would use a separate RCpoll object for each sensing pin.

I learnt from the CapSense library https://github.com/moderndevice/CapSense.

TIm

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 an email to mozzi...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/mozzi-users/54C2D18D.7040805%40gmail.com.

George Profenza

unread,
Jan 26, 2015, 3:29:25 PM1/26/15
to mozzi...@googlegroups.com
Hi,

Thank you both for the input. 
Unfortunately I'm not out of the woods yet.

Marije, I've tried moving the sensor code in updateAudio() but what I had before stops working:
each time I reset the board (power off/power on), there's a different sound (either a tick, the note, or a bit of noise)
and the capacitance doesn't to work anymore.

I've also setup a couple of array to keep track of the current value and sample count per pin, but this stops working when I change
the pinMode again for the second pin.

Here's is my currently working code, reading just one sensor, but ready to store data for 3:

/*  Example changing the gain of a sinewave,
    using Mozzi sonification library.
  
    Demonstrates the use of a control variable to influence an
    audio signal.
  
    Circuit: Audio output on digital pin 9 on a Uno or similar, or
    DAC/A14 on Teensy 3.0/3.1, or 
    check the README or http://sensorium.github.com/Mozzi/
  
    Mozzi help/discussion/announcements:
  
    Tim Barrass 2012, CC by-nc-sa.
*/

#include <MozziGuts.h>
#include <Oscil.h> // oscillator template
#include <tables/sin2048_int8.h> // sine table for oscillator

// use: Oscil <table_size, update_rate> oscilName (wavetable), look in .h file of table #included above
Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin(SIN2048_DATA);

// control variable, use the smallest data size you can for anything used in audio
byte gain = 255;

//ADC Touch setup
int sampleCount[3] = {0};
int values[3] = {0};

void setup(){
  startMozzi(); // start with default control rate of 64
  aSin.setFreq(4200); // set the frequency
}

void updateControl(){
  // as byte, this will automatically roll around to 255 when it passes 0
  if(gain > 3){
    gain = gain - 3 ; 
  }else{
    gain = 0;
  }
  //*
  //begin ADCTouch 
  pinMode(A4, INPUT_PULLUP);
  
  ADMUX |=   0b11111;
  ADCSRA |= (1<<ADSC); //start conversion
  ADCSRA |= (1<<ADIF); //reset the flag
  
  pinMode(A4, INPUT);
  
  values[0] = mozziAnalogRead(A4);
  sampleCount[0]++;
  //end ADCTouch 
  
  if(sampleCount[0] == 64){//wait for 64 samples
    if(values[0] > 500) {//did we get a touch with all these samples ?
//        aSin.setFreq(3320); // set the frequency
      gain = 255;//amp it up
    }
    sampleCount[0] = 0;//reset count
  }
  //*/
}


int updateAudio(){
  return (aSin.next()* gain)>>8; // shift back to STANDARD audio range, like /256 but faster
}


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




I think the issue may the the actual pinMode calls.  
I'm trying to find faster ways of doing this with the atmega328p.
Since I'm not very experienced with this, it's a bit slow, but I'm coming across
port mappings which might help to rewrite pinMode faster ?

Tim, with the RCPoll and changed to pin number to what I'd like to use, but I only get a bit of noise
that doesn't change. This may be mostly because I'm not using the setup the example recommends,
so I'm missing the .1uf sensing cap, 220-1K resistor and the sensor.
I'm trying to get by a setup as simple as possible on the board I'm using which is the SquareWear
(has a capacitive sensor sample of it's own which I couldn't get working with Mozzi)
Is there a way of using a single pin/wire without much else in tandem with RCPoll ?

I haven't tried the CapSense library yet and I'm not sure I understood you message fully.
Do you recommend starting with the RCPoll because it does capacitive sensing and it includes what you've learned from CapSense
or I should try to use CapSense with Mozzi as is ?

Thank you both for the helpful replies,
George
>     >     > <mailto:mozzi-users+unsub...@googlegroups.com
>     <javascript:> <javascript:>>.
>     >     > To post to this group, send email to mozzi...@googlegroups.com
>     >     <javascript:>
>     >     > <mailto:mozzi...@googlegroups.com <javascript:>>.
>     >     > To view this discussion on the web, visit
>     >     >
>     >
>     https://groups.google.com/d/msgid/mozzi-users/88cb42e9-a99c-4d9f-91da-7e4c66decffb%40googlegroups.com
>     <https://groups.google.com/d/msgid/mozzi-users/88cb42e9-a99c-4d9f-91da-7e4c66decffb%40googlegroups.com>
>
>     >
>     <https://groups.google.com/d/msgid/mozzi-users/88cb42e9-a99c-4d9f-91da-7e4c66decffb%40googlegroups.com
>     <https://groups.google.com/d/msgid/mozzi-users/88cb42e9-a99c-4d9f-91da-7e4c66decffb%40googlegroups.com>>
>
>     >
>     >     >
>     >
>     <https://groups.google.com/d/msgid/mozzi-users/88cb42e9-a99c-4d9f-91da-7e4c66decffb%40googlegroups.com?utm_medium=email&utm_source=footer
>     <https://groups.google.com/d/msgid/mozzi-users/88cb42e9-a99c-4d9f-91da-7e4c66decffb%40googlegroups.com?utm_medium=email&utm_source=footer>
>
>     >
>     <https://groups.google.com/d/msgid/mozzi-users/88cb42e9-a99c-4d9f-91da-7e4c66decffb%40googlegroups.com?utm_medium=email&utm_source=footer
>     <https://groups.google.com/d/msgid/mozzi-users/88cb42e9-a99c-4d9f-91da-7e4c66decffb%40googlegroups.com?utm_medium=email&utm_source=footer>>>.
>
>     >
>     >     > For more options, visit https://groups.google.com/d/optout
>     <https://groups.google.com/d/optout>
>     >     <https://groups.google.com/d/optout
>     <https://groups.google.com/d/optout>>.
>     >
>     > --
>     > 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 <javascript:>
>     > <mailto:mozzi-users+unsub...@googlegroups.com <javascript:>>.

>     > To post to this group, send email to mozzi...@googlegroups.com
>     <javascript:>
>     > <mailto:mozzi...@googlegroups.com <javascript:>>.
>     > To view this discussion on the web, visit
>     >

Tim Barrass

unread,
Jan 26, 2015, 10:29:16 PM1/26/15
to mozzi...@googlegroups.com
Hi George,

>Is there a way of using a single pin/wire without much else in tandem with RCPoll ?
Yes, that's what I tested the other day, just a wire, nothing else - but do make the slight mods to the sketch I described.

>Do you recommend starting with the RCPoll because it does capacitive sensing and it includes what you've learned from CapSense
Yes

>should try to use CapSense with Mozzi as is ?
No, but the Capsense library has reliable demos which you can use to see if your setup is working how you expect before adding the further confusion of Mozzi - if it works with Capsense's CapPinSketch, it should work with Mozzi (well, it did for me the other day).

Good luck, it can work..
Tim

PaulStoffregen

unread,
Feb 21, 2015, 6:32:26 AM2/21/15
to mozzi...@googlegroups.com
If you're using a Teensy 3.1 board, you might be able to use the built-in capacitive touch sensing.

Those pins work much like the analog pins.  You use touchRead(pin) instead of analogRead(pin), and it returns a number that's the capacitance in 1/50th of pF units.  The accuracy and stability of measurements is far superior to using GPIO like the CapacitiveSensor.  But most importantly for use with Mozzi, the hardware actually does all the work, which should be much less disruptive to Mozzi's real-time sound generation code.

Unfortunately, the touchRead() function isn't going to give you non-blocking performance you need.  You're going to have to grab the code from hardware/teensy/cores/teensy/touch.c and hack it into 2 pieces: the part that begins the measurement, and the part that checks if the hardware has finished and give you the number.

Look here, on line 83, for the part that waits:

https://github.com/PaulStoffregen/cores/blob/master/teensy3/touch.c#L83

Instead of waiting, you'll create code with those last couple lines that tells you if the measurement is done and gives the data.  Yeah, it's a bit of hacking, but hopefully not too tough to copy those lines and rearrange to suit your needs.
Reply all
Reply to author
Forward
0 new messages