Midterm: Sunflower

My inspiration for this project was a painting by my favorite artist Van Gogh  (the Sunflowers) and I also wanted to create something that I would want to keep in my personal space. Therefore, my midterm project was a mimic of a ‘sunflower’ – a flower that would follow the direction of light.

I initially also wanted to make a flower that would open and close (as recommended by Aaron but despite trying to different methods – I felt as if I would be limited by time if I continued. So instead, I created my final one using two photoresistors and a servo.

For my code, since I used only one pin for both my photoresistors, the code was not too complicated. I used the sensor value (servoValue) to produce the values for which the servo should turn (servoTurn) and initially let this begin at a 90 degree angle. I also added a delay at the end so that allows the servo to reach the position indicated before the sensor senses anymore changes.

#include <Servo.h>

int sensorPin = A0;
int servoPin  = 9;

int sensorValue = 0;
int servoTurn = 90;
int tolerance = 20;

Servo myservo;

void setup() {
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
  myservo.attach(servoPin);
  myservo.write(servoTurn);
  // sets the degrees to which the servo is to be positioned
}

void loop() {

  sensorValue = analogRead(sensorPin);
  Serial.println(sensorValue);
  // print the sensor values
  
  if (sensorValue < (340-tolerance) )
    // perform the first check if the value read from before is less than the difference between 340
  {
    if (servoTurn < 180) servoTurn++; 
    // ++ increments x by one and returns the old value of x
    // perform a further check that the servo is not > 180 
  }
  
  if (sensorValue > (340+tolerance) )
    // value read by the sensor is compared with the sum of 340 + tolerance
  {
    if (servoTurn > 0) servoTurn--;
   // check that the angle the servo is located is not less than 0
  }


  myservo.write(servoTurn); 

  delay(50);
  // allows the servo to reach the position indicated before sensor sensing changes
}

 

Other sources/help/inspiration:

  • https://www.instructables.com/id/Light-Controlled-Servo/
  • https://www.instructables.com/id/Ever-Blooming-Mechanical-Tulip/
  • https://www.instructables.com/id/Create-an-Arduino-Controlled-Light-Following-Flowe/

Leave a Reply