I'm trying to use 3 pins as capacitive pins along side the Mozzi.
I had several attempts and so far the closest I had was using code from the ADCTouch library straight into the main code, but with the current implementation (removing averaging and taking less samples to speed things up) it's only fast enough to process 1 pin and I'd like to use 3.
Here is my implementation so far:
/* Based on the Gain 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:
https://groups.google.com/forum/#!forum/mozzi-users
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(){
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 ?
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
}
The issue is as soon as do the pinMode calls for the second pin I'd like to use (A5) so I'm guessing pinMode needs to work faster. To use pinMode(INPUT_PULLUP)
and pinMode(INPUT)
calls for pins A4,A5 and digital 10, now can I write this using direct port manipulation ?
(I'm new to avr/arduino internals and slowly getting the grips with this).