Back to Parent

//By: Dillon Shu

#include <Arduino_APDS9960.h>

#include <Adafruit_NeoPixel.h>

#define PIXEL_PIN D2
#define PIXEL_COUNT 13
#define PIXEL_TYPE NEO_GRB + NEO_KHZ800

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

void setup() {
  
  while (!Serial);

  if (!APDS.begin()) {
    Serial.println("Error initializing APDS-9960 sensor!");
  }

  strip.begin();
  strip.show(); // Initialize all pixels to 'off'

  APDS.setGestureSensitivity(90);
}

//color palette 
uint32_t green = strip.Color(0, 255, 0);
uint32_t light_green = strip.Color(0, 127, 0);
uint32_t light_red = strip.Color(127, 0, 0);
uint32_t red = strip.Color(255, 0, 0);
uint32_t off = strip.Color(0, 0, 0);

float happiness = 0.85; //happiness multiplier that determines brightness of light source

//led setting variables w/ default values
uint32_t led_color = strip.Color(255, 255, 255); //light source led color
uint32_t friendship_indicator_color = strip.Color(255, 255, 255);//indicator led color
int num_indicator_leds = 3;
int friendship_indicator_led = PIXEL_COUNT - num_indicator_leds;//first indicator led position
int lit_indicator_leds = 3;

//unpredictability meter
float disruptive = 0.01;//0 = not angry, 1 = angry. multiplier, used as (1-disruptive)
int max_predictability = 500;//real_predictability = disruptive_multiplier * predictability, used to randomly make the lamp do things

//variable conformity
float happiness_upper_req = 0.75; // value > upper == very happy
float happiness_lower_req = 0.25; // upper > value > lower == happy, lower > value == angry
int proximity_bound = 250; // above value == happiness + 0.01, below equal == happiness - 0.01

bool on = false;

int brightness = 0; //not in use

void configureLEDs()//sets led variables for indicator and lighting
{
  random_disruptive();
  if((happiness <= 0.99) && (happiness >= 0.79))
  {
    lit_indicator_leds = 3;
    friendship_indicator_color = green;
    more_angry(false);
  }
  else if ((happiness < 0.79) && (happiness >= 0.59))
  {
    lit_indicator_leds = 2;
    friendship_indicator_color = green;
    more_angry(false);
  }
  else if((happiness < 0.59) && (happiness >= 0.39))
  {
    lit_indicator_leds = 1;
    friendship_indicator_color = green;
    more_angry(false);
  }
  else if((happiness < 0.39) && (happiness >= 0.25))
  {
    lit_indicator_leds = 1;
    friendship_indicator_color = red;
    if (happiness)
    more_angry(true);
  }
  else if((happiness < 0.25) && (happiness >= 0.10))
  {
    lit_indicator_leds = 2;
    friendship_indicator_color = red;
    more_angry(true);
  }
  else if((happiness < 0.10) && (happiness >= 0.01))
  {
    lit_indicator_leds = 3;
    friendship_indicator_color = red;
    more_angry(true);
  }
  int color = 255 * happiness;
  led_color = strip.Color(color, color, color);
  
  Serial.print("Color - ");
  Serial.println(color);
}

void set_indicator_leds()//set indicator leds
{
  int num_to_light = num_indicator_leds - lit_indicator_leds;
  for(int i=friendship_indicator_led; i < strip.numPixels() - num_to_light; i++) {
      strip.setPixelColor(i, friendship_indicator_color);
  }
}

void pulse_red(int start, int amount, int iterations)
{
  int pulse = start;
  uint32_t led_config = led_color;
  for(int i = 1; i <= iterations; i++)
  {
    led_color = strip.Color(pulse, 0, 0);
    for(int i=0; i < strip.numPixels() - num_indicator_leds; i++) {
      strip.setPixelColor(i, led_color);
    }
    strip.show();
    wait(50);  
    pulse = pulse + amount;
  }
  set_indicator_leds();
  led_color = led_config;
}

void angry_red_lights()//pulsing red lights for 10 seconds
{
  for(int i = 0; i < 10; i++)
  {
    pulse_red(55, 40, 5);
    pulse_red(255, -40, 5);
  }
  strip.clear();
}

void animism()//have the lamp act unpredictably
{
  int predictability_value = max_predictability - max_predictability * disruptive;
  int rand = random(max_predictability);

  /*
  Serial.print("Random Predictability Value - ");
  Serial.println(rand);
  Serial.print("Predictability Value - ");
  Serial.println(predictability_value);
  */
  Serial.print("disruptive Multiplier - ");
  Serial.println(disruptive);

  if (rand > predictability_value)
  {
    rand = random(1,5);
    if (rand < 2)
    {
      angry_red_lights();
    }
    else if (rand >= 2)
    {
      for(int i=0; i < strip.numPixels() - num_indicator_leds; i++) {
        strip.setPixelColor(i, off);
      }
      set_indicator_leds();
      strip.show();
      wait(5000);
      Serial.println("off");
    }
  }
}

void random_disruptive()//sometimes people just get mad for no reason... 
{
  int rand = random(100);
  int count = 1;
  /*
  Serial.print("Random disruptive Value - ");
  Serial.println(rand);
  */
  if(rand < 10)
  {
    while (count < rand)//increment disruptive X times
    {
      more_angry(true);
      count++;
    }
 }      
}

void more_angry(bool more)//more == true then increase increase the disruptive
{
  //Serial.println("a");
  if(more && disruptive < 0.99)    
  {
    //Serial.println("more");
    disruptive = disruptive + 0.0025;
  }
  else if (!more && disruptive > 0.01)
  {
    //Serial.println("less");
    disruptive = disruptive - 0.01;
  }
}

void adjustHappiness(int proximity)
{
    if((proximity <= proximity_bound) && (happiness < 0.98))
    {
      happiness = happiness + 0.01;
    }
    else if ((proximity > proximity_bound) && happiness > 0.01)
    {
      happiness = happiness - 0.01;
    }
    //Serial.print("Happiness - ");
    //Serial.println(happiness);
}


unsigned long time_now = 0;
void wait(int duration)//using instead of delay to try to keep gesture sensor running
{
  time_now = millis();
  while(time_now + duration > millis())
  {
    detectGesture();
  }
}
void detectGesture()//on/off mechanism
{
  if(APDS.gestureAvailable())
  {
    int gesture = APDS.readGesture();
    Serial.print("Gesture - ");
    Serial.println(gesture);
    if(gesture == GESTURE_RIGHT)
    {
      if(!on)
      {
        disruptive = 0.01;
        happiness = 0.85;
        on = true;
        Serial.println("Right");
      }
    }
    else if(gesture == GESTURE_LEFT)
    {
      on = false;
      Serial.println("Left");
      for(int i=0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, off);
      }
      strip.show();
    }
    else if(gesture == GESTURE_UP)
    {
      on = false;
      Serial.println("Up");
      for(int i=0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, off);
      }
      strip.show();
    }
    else if(gesture == GESTURE_DOWN)
    {
      on = false;
      Serial.println("Down");
      for(int i=0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, off);
      }
      strip.show();
    }
  }
}

void loop() {
  Serial.println("---------");
  //detectGesture();
  
  if(APDS.gestureAvailable())
  {
    int gesture = APDS.readGesture();
    Serial.print("Gesture - ");
    Serial.println(gesture);
    if(gesture == GESTURE_RIGHT)
    {
      if(!on)
      {
        disruptive = 0.01;
        happiness = 0.5;
        on = true;
        Serial.println("Right");
      }
    }
    else if(gesture == GESTURE_LEFT)
    {
      on = false;
      Serial.println("Left");
      for(int i=0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, off);
      }
      strip.show();
    }
    else{
      Serial.println(gesture);
    }
  }
  
  if (on && APDS.proximityAvailable()) {
    // read the proximity
    // - 0   => close
    // - 255 => far
    // - -1  => error
    int proximity = APDS.readProximity();
    
    Serial.print("Proximity - ");
    Serial.println(proximity);
    
    adjustHappiness(proximity);

    configureLEDs2();

    animism();

    for(int i=0; i < strip.numPixels() - num_indicator_leds; i++) {
      strip.setPixelColor(i, led_color);
    }
    set_indicator_leds();

    strip.show();
    strip.clear();
    
  }
  wait(350);
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0