#include CapacitiveSensor sensor1 = CapacitiveSensor(7,6); // Capacitive sensor library with send pin set to 7 and receive pin set to 6 // CapacitiveSensor sensor2 = CapacitiveSensor(7,9); int led = 11; // Light output through pin 11 int groundHigh = 10000; //the threshold for triggering the capative sensor int Time = 200; // Time delay of 1/5 of a second int toggle = 0; // On-off value int brightness = 255; // led brightness value int dimmerTrigger = 0; //a cycle count value for a trigger to fade the light int x = 0; int Fading = 0; // value for fading bright or dim void setup() { pinMode(led, OUTPUT); // Set pin 11 (for the light) to output Serial.begin(9600); // Set the serial port rate } void loop() { long total1 = sensor1.capacitiveSensorRaw(3); // read the sensor and assign value to total1 Serial.print(total1); Serial.print(" , "); Serial.println(brightness); // print sensor and brightness output if(total1 > groundHigh){ // If sensor reads above the threshold if(toggle == 0){ // and the toggle value is off analogWrite(led, brightness); // Turn on the light toggle = 1; // Set toggle value to on delay(Time); }else{ analogWrite(led, 0); // Otherwise turn off the light toggle = 0; // Set toggle value to delay(Time); } } delay(50); if(total1 < groundHigh){ // If threshold is not held dimmerTrigger = 0; // reset the counter Time = 200; }else{ dimmerTrigger++; // Otherwise if trigger is held if(dimmerTrigger == 1){ // Add to the counter Time = 150; } } if(dimmerTrigger == 4){ // If held for 4 cycles x = 0; while(x < 20){ // Start fading cycle, If value reaches 20 break the loop long total1 = sensor1.capacitiveSensorRaw(3); // Read sensor Serial.print(total1); // Print sensor and brightness data Serial.print(" , "); Serial.println(brightness); if(total1 > groundHigh){ // If trigger is held x = 0; // reset cycle count if(Fading == 1){ // If brightness was last set to 0 brightness = brightness + 10; // start fading on // x = 0; if(brightness > 255){ brightness = 255; // Cap at 255 for full brightness } }else{ // Otherwise if Fading is last set to full brightness = brightness - 10; // Dim the brightness if(brightness < 0){ brightness = 0; // Cap the reduction to 0 } } } if(brightness == 0){ Fading = 1; // Toggle Fading to 1 if last brightness was last set to 0 } if(brightness == 255){ Fading = 0; // Toggle Fading to 1 if last brightness was last set to 255 } analogWrite(led, brightness); // Set the light to the new value in the Fade cycle x++; // Count the fade cycle delay(100); // Delay to 1/10 of a second so you can see the fading } analogWrite(led, 0); // Let you know the fading loop has ended by blinking the light delay(100); analogWrite(led, brightness); // on delay(100); analogWrite(led, 0); // off delay(100); analogWrite(led, brightness); // on dimmerTrigger = 0; } }