Created
June 14, 2018 06:18
-
-
Save grisevg/813a818aa2ccd8d18ee3058d06d679c0 to your computer and use it in GitHub Desktop.
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" | |
#include <CapacitiveSensor.h> | |
#include <Servo.h> | |
// WS2812B definitions | |
#define NUM_LEDS 17 | |
#define DATA_PIN 7 | |
CRGB leds[NUM_LEDS]; | |
int bright = 3; | |
//SERVO | |
#define SERVO_DELAY 300 | |
#define FULL_LEFT 180 | |
#define FULL_RIGHT 20 | |
#define MIDDLE 100 | |
#define LITTLE_LEFT 120 | |
#define LITTLE_RIGHT 80 | |
Servo myservo; // create servo object to control a servo | |
//CAPACITIVE | |
#define CAP_TOLERANCE 200 | |
CapacitiveSensor cs_9_10 = CapacitiveSensor(9,10); | |
// This function sets up the leds and tells the controller about them | |
void setup() | |
{ | |
// sanity check delay - allows reprogramming if accidently blowing power w/leds | |
delay(3000); | |
myservo.attach(6); // attaches the servo on pin 9 to the servo object | |
myservo.write(MIDDLE); // tell servo to go to position in variable 'pos' | |
pinMode(3, INPUT_PULLUP); | |
pinMode(4, INPUT_PULLUP); | |
FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS); | |
FastLED.setBrightness(bright); // sets the maximum brightness level. All values are scalled to fit in this range | |
allOff(); | |
} | |
int wait = 100; // This is the delay in ms between LED colour changes | |
byte smileData[NUM_LEDS] = {1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1}; | |
byte frownData[NUM_LEDS] = {1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1}; | |
byte grimaceData[NUM_LEDS] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1}; | |
byte oooohData[NUM_LEDS] = {0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1}; | |
uint8_t random_dir = 0; | |
uint8_t random_face = 0; | |
// This function runs over and over, and is where you do the magic to light your leds. | |
void loop() | |
{ | |
if (digitalRead(4) == LOW) { | |
myservo.write(FULL_LEFT); // tell servo to go to position in variable 'pos' | |
smile(0, 255, 0); | |
} else if (digitalRead(3) == LOW) { | |
myservo.write(FULL_RIGHT); // tell servo to go to position in variable 'pos' | |
frown(255, 0, 0); | |
} else { | |
long tota1 = cs_9_10.capacitiveSensor(30); | |
if (tota1 > CAP_TOLERANCE) { | |
if (random_face == 0) { | |
smile(0,255,0); | |
myservo.write(LITTLE_LEFT); // tell servo to go to position in variable 'pos' | |
} else if (random_face == 1) { | |
grimace(0, 255, 255); | |
myservo.write(MIDDLE); // tell servo to go to position in variable 'pos' | |
} else if (random_face == 2) { | |
frown(255, 0, 0); | |
myservo.write(LITTLE_RIGHT); // tell servo to go to position in variable 'pos' | |
} else if (random_face == 3) { | |
grimace(0, 255, 255); | |
myservo.write(MIDDLE); // tell servo to go to position in variable 'pos' | |
} | |
random_face = (random_face + 1) % 4; | |
} else { | |
myservo.write(MIDDLE); // tell servo to go to position in variable 'pos' | |
grimace(0, 255, 255); | |
} | |
} | |
delay(100); | |
} | |
void smile(int red, int green, int blue) | |
{ | |
for(int i=0; i<NUM_LEDS; i++) | |
{ | |
leds[i] = 0; | |
if (smileData[i] != 0) | |
leds[i] = CRGB(green, red, blue); | |
} | |
FastLED.show(); | |
} | |
void grimace(int red, int green, int blue) | |
{ | |
for(int i=0; i<NUM_LEDS; i++) | |
{ | |
leds[i] = 0; | |
if (grimaceData[i] != 0) | |
leds[i] = CRGB(green, red, blue); | |
} | |
FastLED.show(); | |
} | |
void frown(int red, int green, int blue) | |
{ | |
for(int i=0; i<NUM_LEDS; i++) | |
{ | |
leds[i] = 0; | |
if (frownData[i] != 0) | |
leds[i] = CRGB(green, red, blue); | |
} | |
FastLED.show(); | |
} | |
void ooooh(int red, int green, int blue) | |
{ | |
for(int i=0; i<NUM_LEDS; i++) | |
{ | |
leds[i] = 0; | |
if (oooohData[i] != 0) | |
leds[i] = CRGB(green, red, blue); | |
} | |
FastLED.show(); | |
} | |
// Turns all the LEDs to OFF | |
void allOff() | |
{ | |
for (int i=0; i<NUM_LEDS; i++) | |
leds[i] = 0; | |
FastLED.show(); | |
} | |
// Sets all the LEDs to the same colour | |
void setAll(int red, int green, int blue) | |
{ | |
for (int i=0; i<NUM_LEDS; i++) | |
{ | |
leds[i].g = red; | |
leds[i].r = green; | |
leds[i].b = blue; | |
} | |
FastLED.show(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment