107 lines
3.1 KiB
C++
107 lines
3.1 KiB
C++
#include <CapSense.h>
|
|
|
|
#include "WProgram.h"
|
|
void setup();
|
|
void loop();
|
|
CapSense cs_4_2 = CapSense(4,2); // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add wire, foil
|
|
CapSense cs_4_5 = CapSense(4,5); // 10M resistor between pins 4 & 6, pin 6 is sensor pin, add wire, foil
|
|
CapSense cs_4_8 = CapSense(4,8); // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add wire, foil
|
|
|
|
int sensorValue1 = 0; // the sensor value
|
|
int sensorValue2 = 0; // the sensor value
|
|
int sensorValue3 = 0; // the sensor value
|
|
int sensorMin1 = 1023; // minimum sensor value
|
|
int sensorMin2 = 1023; // minimum sensor value
|
|
int sensorMin3 = 1023; // minimum sensor value
|
|
int sensorMax1 = 0; // maximum sensor value
|
|
int sensorMax2 = 0; // maximum sensor value
|
|
int sensorMax3 = 0; // maximum sensor value
|
|
|
|
void setup()
|
|
{
|
|
|
|
pinMode(13, OUTPUT);
|
|
digitalWrite(13, HIGH);
|
|
|
|
cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);
|
|
cs_4_5.set_CS_AutocaL_Millis(0xFFFFFFFF);
|
|
cs_4_8.set_CS_AutocaL_Millis(0xFFFFFFFF);
|
|
|
|
// calibrate during the first five seconds
|
|
while (millis() < 10000) {
|
|
sensorValue1 = cs_4_2.capSense(30);
|
|
sensorValue2 = cs_4_5.capSense(30);
|
|
sensorValue3 = cs_4_8.capSense(30);
|
|
// record the maximum sensor value
|
|
if (sensorValue1 > sensorMax1) {
|
|
sensorMax1 = sensorValue1;
|
|
}
|
|
// record the minimum sensor value
|
|
if (sensorValue1 < sensorMin1) {
|
|
sensorMin1 = sensorValue1;
|
|
}
|
|
if (sensorValue2 > sensorMax2) {
|
|
sensorMax2 = sensorValue2;
|
|
}
|
|
// record the minimum sensor value
|
|
if (sensorValue2 < sensorMin2) {
|
|
sensorMin2 = sensorValue2;
|
|
}
|
|
if (sensorValue3 > sensorMax3) {
|
|
sensorMax3 = sensorValue3;
|
|
}
|
|
// record the minimum sensor value
|
|
if (sensorValue3 < sensorMin3) {
|
|
sensorMin3 = sensorValue3;
|
|
}
|
|
}
|
|
// signal the end of the calibration period
|
|
digitalWrite(13, LOW);
|
|
|
|
Serial.begin(19200);
|
|
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
long start = millis();
|
|
long total1 = cs_4_2.capSense(30);
|
|
long total2 = cs_4_5.capSense(30);
|
|
long total3 = cs_4_8.capSense(30);
|
|
|
|
sensorValue1 = map(total1, sensorMin1, sensorMax1, 0, 255);
|
|
sensorValue1 = constrain(sensorValue1, 0, 255);
|
|
sensorValue2 = map(total2, sensorMin2, sensorMax2, 0, 255);
|
|
sensorValue2 = constrain(sensorValue2, 0, 255);
|
|
sensorValue3 = map(total3, sensorMin3, sensorMax3, 0, 255);
|
|
sensorValue3 = constrain(sensorValue3, 0, 255);
|
|
|
|
Serial.print(millis() - start); // check on performance in milliseconds
|
|
Serial.print(" "); // tab character for debug windown spacing
|
|
|
|
//Serial.print("Sensor 1 vale ");
|
|
Serial.print(sensorValue1); // print sensor output 1
|
|
Serial.print(" ");
|
|
//Serial.print("Sensor 2 vale ");
|
|
Serial.print(sensorValue2); // print sensor output 2
|
|
Serial.print(" ");
|
|
//Serial.print("Sensor 3 vale ");
|
|
Serial.println(sensorValue3); // print sensor output 3
|
|
|
|
//delay(1000); // arbitrary delay to limit data to serial port
|
|
}
|
|
|
|
|
|
int main(void)
|
|
{
|
|
init();
|
|
|
|
setup();
|
|
|
|
for (;;)
|
|
loop();
|
|
|
|
return 0;
|
|
}
|
|
|