You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
3.5 KiB
C++

#include <CapacitiveSensor.h>
CapacitiveSensor cs_4_2 = CapacitiveSensor(4,2); // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired
int LEDPin1 = 38;
int LEDPin2 = 39;
int LEDPin3 = 40;
int LEDPin5 = 41;
int LEDPin4 = 42;
int LEDPin6 = 43;
int LEDPin7 = 44;
int LEDPin8 = 45;
int LEDPin9 = 46;
int LEDPin10 = 47;
int LEDPin11 = 48;
int LEDPin12 = 49;
int LEDPin13 = 50;
int LEDPin14 = 51;
int LEDPin15 = 52;
int LEDPin16 = 53;
void setup()
{
cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example
Serial.begin(9600);
pinMode(LEDPin1, OUTPUT);
pinMode(LEDPin2, OUTPUT);
pinMode(LEDPin3, OUTPUT);
pinMode(LEDPin4, OUTPUT);
pinMode(LEDPin5, OUTPUT);
pinMode(LEDPin6, OUTPUT);
pinMode(LEDPin7, OUTPUT);
pinMode(LEDPin8, OUTPUT);
pinMode(LEDPin9, OUTPUT);
pinMode(LEDPin10, OUTPUT);
pinMode(LEDPin11, OUTPUT);
pinMode(LEDPin12, OUTPUT);
pinMode(LEDPin13, OUTPUT);
pinMode(LEDPin14, OUTPUT);
pinMode(LEDPin15, OUTPUT);
pinMode(LEDPin16, OUTPUT);
digitalWrite(LEDPin1, LOW);
digitalWrite(LEDPin2, LOW);
digitalWrite(LEDPin3, LOW);
digitalWrite(LEDPin4, LOW);
digitalWrite(LEDPin5, LOW);
digitalWrite(LEDPin6, LOW);
digitalWrite(LEDPin7, LOW);
digitalWrite(LEDPin8, LOW);
digitalWrite(LEDPin9, LOW);
digitalWrite(LEDPin10, LOW);
digitalWrite(LEDPin11, LOW);
digitalWrite(LEDPin12, LOW);
digitalWrite(LEDPin13, LOW);
digitalWrite(LEDPin14, LOW);
digitalWrite(LEDPin15, LOW);
digitalWrite(LEDPin16, LOW);
}
void loop()
{
long start = millis();
long total1 = cs_4_2.capacitiveSensor(30);
long valCons = constrain(total1, 10000, 280000);
long mappedVal = map(valCons, 10000, 280000, 0, 1024);
if (mappedVal >200) {
digitalWrite(LEDPin1, HIGH);
}
else {
digitalWrite(LEDPin1, LOW);
}
if (mappedVal >345) {
digitalWrite(LEDPin2, HIGH);
}
else {
digitalWrite(LEDPin2, LOW);
}
if (mappedVal >400) {
digitalWrite(LEDPin3, HIGH);
}
else {
digitalWrite(LEDPin3, LOW);
}
if (mappedVal >367) {
digitalWrite(LEDPin4, HIGH);
}
else {
digitalWrite(LEDPin4, LOW);
}
if (mappedVal >993) {
digitalWrite(LEDPin5, HIGH);
}
else {
digitalWrite(LEDPin5, LOW);
}
if (mappedVal >398) {
digitalWrite(LEDPin6, HIGH);
}
else {
digitalWrite(LEDPin6, LOW);
}
if (mappedVal >912) {
digitalWrite(LEDPin7, HIGH);
}
else {
digitalWrite(LEDPin7, LOW);
}
if (mappedVal >800) {
digitalWrite(LEDPin8, HIGH);
}
else {
digitalWrite(LEDPin8, LOW);
}
if (mappedVal >622) {
digitalWrite(LEDPin9, HIGH);
}
else {
digitalWrite(LEDPin9, LOW);
}
if (mappedVal >22) {
digitalWrite(LEDPin10, HIGH);
}
else {
digitalWrite(LEDPin10, LOW);
}
if (mappedVal >114) {
digitalWrite(LEDPin11, HIGH);
}
else {
digitalWrite(LEDPin11, LOW);
}
if (mappedVal >112) {
digitalWrite(LEDPin12, HIGH);
}
else {
digitalWrite(LEDPin12, LOW);
}
if (mappedVal >313) {
digitalWrite(LEDPin13, HIGH);
}
else {
digitalWrite(LEDPin13, LOW);
}
if (mappedVal >214) {
digitalWrite(LEDPin14, HIGH);
}
else {
digitalWrite(LEDPin14, LOW);
}
if (mappedVal >315) {
digitalWrite(LEDPin15, HIGH);
}
else {
digitalWrite(LEDPin15, LOW);
}
if (mappedVal >100) {
digitalWrite(LEDPin16, HIGH);
}
else {
digitalWrite(LEDPin16, LOW);
}
Serial.print(total1);
Serial.print("\t");
Serial.print(valCons);
Serial.print("\t");
Serial.println(mappedVal); // print sensor output 1
delay(100); // arbitrary delay to limit data to serial port
}