Cap Touch Light Switch (on/off)
 Page :   [ 1 ]    [ 2 ]  

Someone asked me to post some code for this.  This project was a LONG time ago so I had to go hunting for the code.  i THINK this is the code that was running on it just to test things out.  It may not be but it looks about right :)

But then again it might just be complete crap as it is just some code to do some basic testing!

It appears to cycle through red, green and blue each time the cap sense is fired.

 

                
#include <Wire.h>
#include "Adafruit_MPR121.h"
#include <ESP8266WiFi.h>
#include <WiFiClient.h>

#define RED 14
#define GREEN 12
#define BLUE 13

Adafruit_MPR121 cap = Adafruit_MPR121();

unsigned long touchStartTime = 0; // last time update
bool touching = false;
unsigned long interval = 1000; 

unsigned long releaseStartTime = 0; // last time update
bool releasing = false;

uint16_t lasttouched = 0;
uint16_t currtouched = 0;

uint16_t curPin = 13;

void setup() {

  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);

  analogWrite(RED,0);
  analogWrite(GREEN,0);
  analogWrite(BLUE,0);

  Serial.begin(9600);

  while (!Serial) {
    ; 
  }

  Serial.println("Adafruit MPR121 Capacitive Touch sensor");

  if (!cap.begin(0x5A)) {
    Serial.println("MPR121goo not found, check wiring?");
    while (1);
  }

  // uncommenting this made it most sensitive (or so it seemed)
  cap.setThresholds (0.9997, 0); //Maximum sensitivity
  
  Serial.println("MPR121 found!");

}

void loop() {

  currtouched = cap.touched();

    if ((millis() - touchStartTime) > interval){

      if (currtouched & _BV(1)){

        if ((!touching) && (!releasing)){

          touching = true;
          touchStartTime = millis();
          analogWrite(RED,0);
          analogWrite(GREEN,0);
          analogWrite(BLUE,0);

          analogWrite(curPin,255);

          curPin++;

          if (curPin > 14){
            curPin = 12;
          }

        }

      }

      if ((millis() - releaseStartTime) > 500){
        releasing = false;
      }

      if ((currtouched == 0) && touching){
        releaseStartTime = millis();
        releasing = true;
        touching = false;
      }

    }

  lasttouched = currtouched;

  return;
}

(Page 2 of 2)