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).
1 Answer 1
First, figure out which port and pin you're using. Then, manipulate them.
DDRC &= ~_BV(PC5);
PORTC |= _BV(PC5);
...
DDRF &= ~_BV(PF5);
PORTF |= _BV(PF5);
...
DDRF &= ~_BV(PF0);
PORTF |= _BV(PF0);
-
Thank you for the quick reply(+1). Since I'm still a n00b I had to do a bit of googling. Found this nice diagram and also found a note that I should rename these constants to
PORTC5/PORTF5/PORTF0
and that's how I could compile my modified code. Also, I assume these are for INPUT_PULLUP. and for INPUT I just need to doPORT |= 0
,PORTF |= 0
right ? Thanks again for your help and sorry to "pull your sleeve", but could use more details as a noviceGeorge Profenza– George Profenza2015年01月27日 00:29:42 +00:00Commented Jan 27, 2015 at 0:29 -
2@GeorgeProfenza You're not improving your code by renaming the constants. Nobody will be able to understand it when you ask for support or try to Google for information. I'd advise to use the standard names. Download the full datasheet for the ATmega328 from the Atmel website and read chapter 14. I/O-Ports. The AVR datasheets are usually pretty good and you should consider to check out other chapters too. You'll also notice that these register names are usedjippie– jippie2015年01月27日 05:45:02 +00:00Commented Jan 27, 2015 at 5:45
-
throughout the datasheet.jippie– jippie2015年01月27日 05:46:00 +00:00Commented Jan 27, 2015 at 5:46
-
@jippie The datasheet actually used
PORTC5
instead ofPC5
in the older versions of the datasheet. My 2009 version does (guess I should update my local pdf file)Gerben– Gerben2015年01月27日 16:45:31 +00:00Commented Jan 27, 2015 at 16:45 -
@Gerben Always use the latest release of (any) the datasheet. Check errata if you like, mistakes are corrected every so often an some can cost you lots of troubleshooting time. Whenever possible check the vendor website of a device an use other websites only for backup.jippie– jippie2015年01月27日 19:42:54 +00:00Commented Jan 27, 2015 at 19:42