<h4>Denise Li</h4>
<h4>Justin Kufro</h4>
Flowers need sunlight to survive, so to make our artificial flower fit in we gave it the ability to search for sufficient light sources. The goal was to give the flower two distinct personalities: a smooth, calm, one with almost hypnotic motion; and a jittery, indecisive one that can’t commit to a single light source.

The mechanism uses a photoresistor for measuring light, and a servo that manipulates the spring. The model responds to whatever entity provides it with a sufficient light source. That could be the sun, or simply someone nearby with a flashlight in-hand.
<h3>Video</h3>

<h3>Files</h3>
<a href=”https://courses.ideate.cmu.edu/16-223/f2018/work/wp-content/uploads/2018/09/solidworks.zip”>solidworks</a>
<h3>Code</h3>

#include &lt;Servo.h&gt;

// CONSTANTS
const int SERVO_PIN = 9;
const int BUTTON_PIN = 2;
const int PHOTORESISTOR_PIN = A0;
const int MIN_SERVO_ANGLE = 10;
const int DEFAULT_SERVO_ANGLE = 90;
const int MAX_SERVO_ANGLE = 170;
const int ACCEPTABLE_LIGHT = 800;

// globals
Servo servo;
float servo_angle = DEFAULT_SERVO_ANGLE;
float smooth_servo_speed = 0.005;
float jittery_servo_speed = 0.008;
int servo_direction = 1;
int fixated_angle = DEFAULT_SERVO_ANGLE;
int photoresistor_value = 0;
bool personality = true; // false =&gt; smooth, true =&gt; jittery
bool state = false; // false =&gt; search, true =&gt; fixated

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT);
  servo.attach(SERVO_PIN);
}

void set_new_servo_angle(float servo_speed, int minimum, int maximum, int multiplier) {
  servo_angle = servo_angle + (servo_speed * servo_direction * multiplier);
  if (servo_angle &gt; maximum) {
    servo_angle = maximum;
    servo_direction = -1;
  }
  if (servo_angle &lt; minimum) { servo_angle = minimum; servo_direction = 1; } } bool smooth_search() { if (photoresistor_value &gt;= ACCEPTABLE_LIGHT) {
    fixated_angle = servo_angle;
    return true; // go to fixated state
  }
  set_new_servo_angle(smooth_servo_speed, MIN_SERVO_ANGLE, MAX_SERVO_ANGLE, 1);
  return false;
}

bool smooth_fixated() {
  if (photoresistor_value &lt; ACCEPTABLE_LIGHT) { return false; // go back to searching state } set_new_servo_angle(smooth_servo_speed, fixated_angle - 20, fixated_angle + 20, 1); return true; } int get_rand_direction(int sample) { if (random(0, sample) == 0) { return -1; } return 1; } bool jittery_search() { if (photoresistor_value &gt;= ACCEPTABLE_LIGHT) {
    fixated_angle = servo_angle;
    return true; // go to fixated state
  }
  int jumpyness = 1;
  if (get_rand_direction(3000) == -1) {
    jumpyness = 6000;
  }
  set_new_servo_angle(jittery_servo_speed, MIN_SERVO_ANGLE, MAX_SERVO_ANGLE, jumpyness);
  servo_direction = servo_direction * get_rand_direction(2000);
  return false;
}

bool jittery_fixated() {
  if (photoresistor_value &lt; ACCEPTABLE_LIGHT) {
    return false; // go back to searching state
  }
  int jumpyness = 1;
  if (get_rand_direction(10000) == -1) {
    jumpyness = 80000;
    set_new_servo_angle(jittery_servo_speed, fixated_angle - 20, fixated_angle + 20, jumpyness);
    servo.write(random(MIN_SERVO_ANGLE, MAX_SERVO_ANGLE));
    delay(1000);
  }
  set_new_servo_angle(jittery_servo_speed, fixated_angle - 20, fixated_angle + 20, jumpyness);
  return true;
}

void loop() {
  servo.write(servo_angle);
  photoresistor_value = analogRead(PHOTORESISTOR_PIN);
  if (personality == false) { 
    if (state == false) {
      state = smooth_search();
    } else {
      state = smooth_fixated();
    }
  } else {
    if (state == false) {
      state = jittery_search();
    } else {
      state = jittery_fixated();
    }
  }
}