Hi, I'm playing with the CapacitiveSensor library. I have a working CapacitiveSensor using pins 7 and 6, but the other two sensors I declared are not working:
The wiring is identical for all three and I triple-checked it. Calling capacitiveSensor(100) on cs2 and cs3 immediately returns a result of -1 (with 0 elapsed millis).
According to the documentation at Arduino Playground - CapacitiveSensor this means the pin is invalid (although the same doc still says the engineers haven't gotten around to implementing that return code yet).
I tried a couple others pins with same results.
Can you tell me which pins are valid? I thought the library said its usable on ALL I/O pins. Or does the -1 indicate a different problem?
Note I'm using Serial, I2S (ArduinoSound) and SD in the same project.
Also note that documentation links to a dead old forum thread as the "official" place to discuss the library. Gotta say I was really excited about the Arduino (MKR Zero specifically) but the level of documentation is stunningly disappointing. (Didn't realize I2S was hardwired - might be nice if your docs linked to the SAMD Peripheral Multiplexer docs; and haven't even been able to find a list of appropriate constant names for all ports without resorting to variant.h)
but the other two sensors I declared are not working:
The one with pin 0 as one of the pins will clearly NOT work, if you are using Serial to prove that the others work.
We have NO idea what values you assigned to PIN_A3 or PIN_A4, so we can only assume that again you are trying to use one pin for two different purposes.
There is a reason that we expect you to post ALL of your code.
Easy there; no need to be patronizing. I'm new here, didn't know the protocol.
I'm not intentionally trying to utilize multiple pins for competing purposes. That's why I was hoping to find some better documentation about which pins are utilized by the built-in libraries. Presently I'm working on reverse engineering that info from source code files, the Arduino schematic, and SAMD datasheet. Thanks for catching the pin 0 Serial conflict.
Here's the full listing. Appreciate your help.
/*
This reads a wave file from an SD card and plays it using the I2S interface to
a MAX08357 I2S Amp Breakout board.
Circuit:
* Arduino MKRZero board
* MAX08357:
* GND connected GND
* VIN connected 5V
* LRC connected to pin 3 / PA11 (MKR1000, MKRZero)
* BCLK connected to pin 2 / PA10 / I2S/SCK[0] (original proto attempted pin 2 / PB10)
* DIN connected to pin A6 / PA07 / I2S/SD[0] (original proto attempted pin A5 / PB11 which has I2S/SCK[1])
*/
/* These earlier attempts to remap are unused
//#undef PIN_I2S_SD
//#undef PIN_I2S_SCK
//#define PIN_I2S_SD (5u)
//#define PIN_I2S_SCK (4u)
See C:\Users\<username>\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.6.18\variants\mkrzero\variant.h
*/
#include <SD.h>
#include <ArduinoSound.h>
#include <CapacitiveSensor.h>
// Note: pin0 and 1 are for serial!
CapacitiveSensor cs1 = CapacitiveSensor(7, 6); //pin 6 is reciever and connects to wire / foil, pin 7 is sender, interconnect with 1Mohm
CapacitiveSensor cs2 = CapacitiveSensor(A3, A4);
CapacitiveSensor cs3 = CapacitiveSensor(0, PIN_A5);
const char filename[] = "MONOLOUD.WAV";
// MONOLOUD.WAV
// MUSIC.WAV
// 001.wav
SDWaveFile waveFile;
bool sdinitialized = true; // temporary
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // initialize digital pin LED_BUILTIN as an output.
// Trying to get CapacitiveSensors to work (chunk shouldn't be required?)
pinMode(7, OUTPUT);
pinMode(6, INPUT);
pinMode(PIN_A3, OUTPUT);
pinMode(PIN_A4, INPUT);
pinMode(0, OUTPUT);
pinMode(PIN_A5, INPUT);
cs1.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate
cs2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate
cs3.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate
// Enable audio subboard and voltage converter
pinMode(PIN_A2, OUTPUT); // sound board enabled
pinMode(PIN_A1, OUTPUT); // vboost high for on
digitalWrite(PIN_A2, 1);
digitalWrite(PIN_A1, 1);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
tryInitSD();
}
void tryInitSD() {
if (sdinitialized) return;
// setup the SD card, depending on your shield of breakout board
// you may need to pass a pin number in begin for SS
Serial.print("Initializing SD card...");
if (!SD.begin()) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
sdinitialized = true;
// create a SDWaveFile
waveFile = SDWaveFile(filename);
// check if the WaveFile is valid
if (!waveFile) {
Serial.println("wave file is invalid!");
return;
}
// print out some info. about the wave file
Serial.print("Bits per sample = ");
Serial.println(waveFile.bitsPerSample());
long channels = waveFile.channels();
Serial.print("Channels = ");
Serial.println(channels);
long sampleRate = waveFile.sampleRate();
Serial.print("Sample rate = ");
Serial.print(sampleRate);
Serial.println(" Hz");
long duration = waveFile.duration();
Serial.print("Duration = ");
Serial.print(duration);
Serial.println(" seconds");
// adjust the playback volume
AudioOutI2S.volume(5);
// check if the I2S output can play the wave file
if (!AudioOutI2S.canPlay(waveFile)) {
Serial.println("unable to play wave file using I2S!");
while (1); // do nothing
}
// start playback
Serial.println("starting playback");
AudioOutI2S.play(waveFile);
}
void loop() {
tryInitSD();
// check if playback is still going on
//if (!AudioOutI2S.isPlaying()) {
// playback has stopped
//Serial.println("playback stopped");
long start = millis();
long total1 = cs1.capacitiveSensor(100);
long interval1 = millis() - start;
float avg1 = (float)total1 / (float)interval1;
start = millis();
long total2 = cs2.capacitiveSensor(100);
long interval2 = millis() - start;
float avg2 = (float)total2 / (float)interval2;
start = millis();
long total3 = cs3.capacitiveSensor(100);
long interval3 = millis() - start;
float avg3 = (float)total3 / (float)interval3;
digitalWrite(LED_BUILTIN, avg1 > 250.0 ? HIGH : LOW); // TODO: REPLACE
Serial.print(interval1); // check on performance in milliseconds
Serial.print("\t"); // tab character for debug window spacing
Serial.print(total1);
Serial.print("\t");
Serial.print(avg1);
Serial.print("\t\t");
Serial.print(interval2); // check on performance in milliseconds
Serial.print("\t"); // tab character for debug window spacing
Serial.print(total2);
Serial.print("\t");
Serial.print(avg2);
Serial.print("\t\t");
Serial.print(interval3); // check on performance in milliseconds
Serial.print("\t"); // tab character for debug window spacing
Serial.print(total3);
Serial.print("\t");
Serial.println(avg3);
delay(100);
//Serial.println("playback restarting");
//AudioOutI2S.play(waveFile);
//}
}