#include #ifdef __AVR__ #include #endif #include #include #include //FASTLED #define LED_PIN 23 #define LED_PIN2 19 #define LED_PIN3 18 #define NUM_LEDS 50 #define BRIGHTNESS 110 #define LED_TYPE WS2812 #define COLOR_ORDER GRB CRGB leds[NUM_LEDS]; #define UPDATES_PER_SECOND 100 //COLOR PALETTES DEFINE_GRADIENT_PALETTE(happycoral) { 0,85,27,44, 17,102,27,37, 34,189,50,40, 51,196,55,75, 68,201,64,86, 85,203,63,87, 102,200,60,81, 119,206,78,121, 136,198,65,95, 153,224,91,101, 170,242,96,70, 187,198,39,37, 204,158,22,28, 221,158,20,24, 238,149,22,25, 255,171,24,28 }; DEFINE_GRADIENT_PALETTE(annoyedcoral) { 0,24,69,109, 63,32,71,102, 127,71,88,94, 191,112,80,64, 255,149,100,69 }; DEFINE_GRADIENT_PALETTE(unhappycoral) { 0,131,194,195, 63,205,226,215, 127,214,223,213, 191,202,209,193, 255,169,178,159 }; CRGBPalette16 currentPalette; CRGBPalette16 targetPalette; TBlendType currentBlending; //SENSORS //Indicator LED const int ledPin = 26; //Accelerometer Variables int ADXL345 = 0x53; // The ADXL345 sensor I2C address float X_out, Y_out, Z_out; // Outputs //Temp Sensor int tempPin = 5; //Input pin from temp sensor. float tempread; //Capacitive Touch const int digIn = 25; int capPin = 33; //Input pin from cap sensor. float capread; //LAMP FUNCTIONALITY int unhappy = 0; uint8_t timeSinceLastPress; uint8_t blendRate = 5; typedef enum { HAPPY, ANNOYED, UNHAPPY, NONE } state_t; state_t state; state_t previous_state; void setup() { Serial.begin(115200); // initialize sensor pins pinMode(ledPin, OUTPUT); pinMode(tempPin, INPUT); pinMode(digIn, OUTPUT); pinMode(capPin, INPUT); //Inititalize Accelerometer Wire.begin(); // Initiate the Wire library Wire.beginTransmission(ADXL345); // Start communicating with the device Wire.write(0x2D); // Access/ talk to POWER_CTL Register - 0x2D Wire.write(8); // (8dec -> 0000 1000 binary) Bit D3 High for measuring enable Wire.endTransmission(); delay(10); Serial.println("Accelerometer initialized!"); Wire.begin(); // Initiate the Wire library delay( 3000 ); // power-up safety delay FastLED.addLeds(leds, NUM_LEDS).setCorrection( TypicalLEDStrip ); FastLED.addLeds(leds, NUM_LEDS).setCorrection( TypicalLEDStrip ); FastLED.addLeds(leds, NUM_LEDS).setCorrection( TypicalLEDStrip ); FastLED.setBrightness( BRIGHTNESS ); state_t state = HAPPY; state_t previous_state = NONE; currentPalette = RainbowColors_p; currentBlending = LINEARBLEND; } // the loop function runs over and over again forever void loop() { //SENSORS getAcceleration(); getCap(); Serial.println("unhappy= "); Serial.println(unhappy); Serial.println("state= "); Serial.println(state); Serial.println("previous_state= "); Serial.println(previous_state); //UPDATE CORAL MOOD switch (state) { case HAPPY: if (unhappy>5) { //it's been bugged too many times previous_state=state; state = ANNOYED; } break; case ANNOYED: if ((millis()/1000)-timeSinceLastPress >= 15) { //fix Serial.println("returning to happy!"); previous_state=state; state = HAPPY; unhappy = 0; } if (unhappy >=7) { state = UNHAPPY; } break; case UNHAPPY: if ((millis()/1000)-timeSinceLastPress >= 10) { Serial.println("returning to annoyed!"); previous_state=state; state = ANNOYED; unhappy = 0; } break; } updateCoralPalette(); static uint8_t startIndex = 0; startIndex = startIndex + 1; /* motion speed */ FillLEDsFromPaletteColors( startIndex); FastLED.setTemperature(Tungsten100W); FastLED.show(); FastLED.delay(1000 / UPDATES_PER_SECOND); Serial.println(); delay(1000); } void getCap() { float x,y,z; digitalWrite(digIn, HIGH); delayMicroseconds(5); capread = analogRead(capPin); if (capread < 2200) { //pressed! unhappy +=1; Serial.print(" don't touch me pls"); timeSinceLastPress = (millis() / 1000); } else if (capread <2000){ //pressed harder unhappy +=5; Serial.print(" stoppPP"); timeSinceLastPress = (millis() / 1000); } else if (capread < 1900){ //really poked it unhappy +=10; Serial.print(" go AWAYY "); timeSinceLastPress = (millis() / 1000); } float accel = sqrt(X_out*X_out + Y_out*Y_out + Z_out*Z_out); if (accel>361) { unhappy +=1; } Serial.print(" accel = "); Serial.print(accel); Serial.print(" cap= "); Serial.print(capread); Serial.println(); digitalWrite(digIn, LOW); delay(25); } void getAcceleration() { Wire.beginTransmission(ADXL345); Wire.write(0x32); // Start with register 0x32 (ACCEL_XOUT_H) Wire.endTransmission(false); Wire.requestFrom(ADXL345, 6, true); // Read 6 registers total, each axis value is stored in 2 registers X_out = ( Wire.read()| Wire.read() << 8); // X-axis value X_out = X_out/256; //For a range of +-2g, we need to divide the raw values by 256, according to the datasheet Y_out = ( Wire.read()| Wire.read() << 8); // Y-axis value Y_out = Y_out/256; Z_out = ( Wire.read()| Wire.read() << 8); // Z-axis value Z_out = Z_out/256; Serial.print(" Xa= "); Serial.print(X_out); Serial.print(" Ya= "); Serial.print(Y_out); Serial.print(" Za= "); Serial.print(Z_out); Serial.println(); } void FillLEDsFromPaletteColors( uint8_t colorIndex) { uint8_t brightness = 255; for( int i = 0; i < NUM_LEDS; i++) { leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending); colorIndex += 3; } } void updateCoralPalette(){ if (previous_state != state && state == 0) { currentPalette = RainbowColors_p; currentBlending = LINEARBLEND; } if(previous_state != state && state == 1) { currentPalette = happycoral; currentBlending = LINEARBLEND; } if(previous_state != state && state == 2) { currentPalette = unhappycoral; currentBlending = LINEARBLEND; } } void indicatorLED() { digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second } void fill(CRGB newColor) // there might be an built function in FASTled also... { for (int i = 0; i < NUM_LEDS; i++) { leds[i] = newColor; } FastLED.show(); }