// Sketch for lighting 2 Adafruit Neopixels with a touch sensor. // Adapted from Dave Melis's touch sensor sketch for ATtiny, from the // MIT Media Lab High Low Tech Group #include #define PIN 1 Adafruit_NeoPixel strip = Adafruit_NeoPixel(2, PIN, NEO_GRB + NEO_KHZ800); int calibration = 0; int previous; int togglestate = LOW; void setup() { //Calibrate the touch sensor! for (int i = 0; i < 8; i++) { calibration += chargeTime(PB2); delay(20); } calibration = (calibration + 4) / 8; strip.begin(); strip.show(); // Initialize all pixels to 'off' } void loop() { int n = chargeTime(PB2); // check if the state of the touch sensor has changed if (previous <= calibration && n > calibration) { if (togglestate == LOW) togglestate = HIGH; else togglestate = LOW; if(togglestate==LOW) { strip.setPixelColor(0,0,0,0); //if touch sensor not being touched, turn Neopixels off strip.setPixelColor(1,0,0,0); } else { // but if they are being touched... strip.setPixelColor(0,255,0,255); //Make the first one this colour strip.setPixelColor(1,100,0,255); //And the second one this colour } } previous = n; //record the state of the LED - is it on or off strip.show(); //Turn the Neopixels on or off! delayMicroseconds(500); } //This function detects when the touch sensor is being touched byte chargeTime(byte pin) { byte mask = (1 << pin); byte i; DDRB &= ~mask; // input PORTB |= mask; // pull-up on for (i = 0; i < 16; i++) { if (PINB & mask) break; } PORTB &= ~mask; // pull-up off DDRB |= mask; // discharge return i; }