Created
March 15, 2020 23:15
-
-
Save ceiborg/272b33ee68216ea1118458614b06b59d to your computer and use it in GitHub Desktop.
Custom Capsense with FastLed and Low Pass filters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "FastLED.h" | |
#define NUM_LEDS 9 | |
CRGB leds[NUM_LEDS]; | |
#define PIXELLED 6 | |
float minHue = 1.0f; | |
float maxHue = 1.0f; | |
float hue =0.0f; | |
#define resolution 8 | |
#define mains 50 // 60: north america, japan; 50: most other places | |
#define refresh 2 * 1000000 / mains | |
void setup() { | |
Serial.begin(115200); | |
FastLED.addLeds<NEOPIXEL, PIXELLED>(leds, NUM_LEDS); | |
// unused pins are fairly insignificant, | |
// but pulled low to reduce unknown variables | |
for(int i = 2; i < 14; i++) { | |
pinMode(i, OUTPUT); | |
digitalWrite(i, LOW); | |
} | |
for(int i = 8; i < 11; i++) | |
pinMode(i, INPUT); | |
startTimer(); | |
} | |
void loop() { | |
//Serial.print(time(8, B00000001), DEC); | |
//Serial.print(","); | |
//Serial.print(time(9, B00000010), DEC); | |
//Serial.print(","); | |
//Serial.println(time(10, B00000100), DEC); | |
int h = time(8, B00000001); | |
hue += (h-hue) *0.05f; | |
if ( (hue <minHue) && (hue > 1.0f) ) | |
minHue= hue; | |
if (hue> maxHue) | |
maxHue = hue; | |
//me hago un comentario | |
minHue += (hue-minHue)/(0.1f*minHue); | |
maxHue -= (maxHue-hue)/(0.1f*maxHue); | |
int hueMapped = cmap (hue, minHue, maxHue, 0, 255); | |
Serial.print(minHue); | |
Serial.print(","); | |
Serial.print(maxHue); | |
Serial.print(","); | |
Serial.print(h); | |
Serial.print(","); | |
Serial.println (hue); | |
FastLED.show(); | |
delay (20); | |
for (int i=0;i<NUM_LEDS; i++){ | |
leds[i].setHue(hueMapped); | |
} | |
} | |
int cmap( int v, int _min, int _max, int _from, int _to){ | |
return map (constrain (v, _min, _max), _min, _max, _from, _to); | |
} | |
long time(int pin, byte mask) { | |
unsigned long count = 0, total = 0; | |
while(checkTimer() < refresh) { | |
// pinMode is about 6 times slower than assigning | |
// DDRB directly, but that pause is important | |
pinMode(pin, OUTPUT); | |
PORTB = 0; | |
pinMode(pin, INPUT); | |
while((PINB & mask) == 0) | |
count++; | |
total++; | |
} | |
startTimer(); | |
return (count << resolution) / total; | |
} | |
extern volatile unsigned long timer0_overflow_count; | |
void startTimer() { | |
timer0_overflow_count = 0; | |
TCNT0 = 0; | |
} | |
unsigned long checkTimer() { | |
return ((timer0_overflow_count << 8) + TCNT0) << 2; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment