Arduino_Slider_Trinkey_Demo.ino
1 // SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries 2 // 3 // SPDX-License-Identifier: MIT 4 5 #include <Adafruit_NeoPixel.h> 6 #include "Adafruit_FreeTouch.h" 7 8 // Create the neopixel strip with the built in definitions NUM_NEOPIXEL and PIN_NEOPIXEL 9 Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_NEOPIXEL, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800); 10 // Create the touch pad 11 Adafruit_FreeTouch qt = Adafruit_FreeTouch(PIN_TOUCH, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE); 12 13 int16_t neo_brightness = 255; // initialize with highest brightness 14 15 void setup() { 16 Serial.begin(9600); 17 //while (!Serial); 18 19 strip.begin(); 20 strip.setBrightness(neo_brightness); 21 strip.show(); // Initialize all pixels to 'off' 22 23 analogReadResolution(12); // set highest resolution 24 25 if (! qt.begin()) 26 Serial.println("Failed to begin qt"); 27 } 28 29 void loop() { 30 uint16_t touch = qt.measure(); 31 Serial.print("Touch: "); Serial.println(touch); 32 33 uint16_t potval = analogRead(PIN_POTENTIOMETER); 34 Serial.print("Slider: "); 35 Serial.println((float)potval / 4095); 36 37 uint8_t wheelval = map(potval, 0, 4095, 0, 255); 38 //Serial.print("Wheel: "); 39 //Serial.println(wheelval); 40 41 // If the pad is touched, turn off neopix! 42 if (touch > 500) { 43 Serial.println("Touched!"); 44 strip.setBrightness(0); 45 } else { 46 strip.setBrightness(255); 47 } 48 49 for(int i=0; i< strip.numPixels(); i++) { 50 strip.setPixelColor(i, Wheel((wheelval+85) % 255)); 51 } 52 53 strip.show(); 54 delay(10); 55 } 56 57 // Input a value 0 to 255 to get a color value. 58 // The colours are a transition r - g - b - back to r. 59 uint32_t Wheel(byte WheelPos) { 60 if(WheelPos < 85) { 61 return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); 62 } else if(WheelPos < 170) { 63 WheelPos -= 85; 64 return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); 65 } else { 66 WheelPos -= 170; 67 return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); 68 } 69 }