TOUCH CAPACITIVE SWITCH

saklar sentuh

#include <CapacitiveSensor.h>

int OUTPUT_PIN = 11; //PWM pin

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

void setup() {

cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 – just as an example

Serial.begin(9600);

pinMode(OUTPUT_PIN, OUTPUT);

}

int on = 0;

unsigned long last = millis();

void loop() {

long start = millis();

long total1 = cs_4_2.capacitiveSensor(30);

if (total1 >= 1000) {//this value can be adjusted for get desired sensitivity

//==============================

if (millis() – last > 250) {

on = !on;

analogWrite(OUTPUT_PIN, on ? 255 : 0);

//digitalWrite(OUTPUT_PIN, on ? HIGH : LOW); if do not want adjustable speed

}

last = millis();

//===================================================

}

Serial.println(total1);

delay(100);

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s