Wednesday, February 18, 2009

Servo Lab

Giving my Mom a special shout out this week for helping me talk it out.

I am really interested in biophilic design. While this aesthetic has several different characteristics, I find natural light to be one of the most important
of the lot.
In urban environments natural light must be optimised for both plants and people.
The idea for this week was to build a device that measures the strongest light source, and then orients itself towards that brightest value.

The device itself was pretty straightforward:
  • A box for the Servo to sit in
  • A tube for the photo sensor
    (a small bit of sponge to mount the photo sensor)

  • a little wire to mount the tube onto the box



The majority of my tinkering was relegated to the code.

The first iteration was just creating a for loop that controlled the sweep of the servo.

#include // include the servo library

Servo servoMotor; // creates an instance of the servo object to control a servo

int servoPin = 9; // Control pin for servo motor, must be a PWM pin
// the value representing the angle of the servo


void setup()
{

servoMotor.attach(servoPin); // attaches the servo on pin 2 to the servo object

} //end setup

void loop()
{
for (int servoAng = 0; servoAng <= 180; servoAng++)
{
servoMotor.write(servoAng);
delay(15);
} //end for
// waits for the servo to get there
}// end loop

I continued adding pauses because I didn't want to be overwhelmed by sensor values. So I had the servo move in 10 degree increments. Then I was going to make a variable for each one (Ham fisted).



#include // include the servo library

Servo servoMotor; // creates an instance of the servo object to control a servo

int analogPin = 0; // the analog pin that the sensor is on

int analogValue = 0; // the value returned from the analog sensor

int servoPin = 9; // Control pin for servo motor, must be a PWM pin
// the value representing the angle of the servo


void setup()
{

servoMotor.attach(servoPin); // attaches the servo on pin 2 to the servo object

} //end setup

void loop()
{
int servoAng = 0;
int sensArray [18];
servoAng = 0;
servoMotor.write(servoAng);
sensArray[0] = analogValue;

delay(1000);

servoAng = servoAng + 10;
servoMotor.write(servoAng);
sensArray[0] = analogValue;
delay(1000);

servoAng = servoAng + 10;
servoMotor.write(servoAng);
sensArray[1] = analogValue;
delay(1000);

servoAng = servoAng + 10;
servoMotor.write(servoAng);
sensArray[2] = analogValue;
delay(1000);

servoAng = servoAng + 10;
servoMotor.write(servoAng);
sensArray[3] = analogValue;
delay(1000);

servoAng = servoAng + 10;
servoMotor.write(servoAng);
sensArray[4] = analogValue;
delay(1000);

servoAng = servoAng + 10;
servoMotor.write(servoAng);
sensArray[5] = analogValue;
delay(1000);

servoAng = servoAng + 10;
servoMotor.write(servoAng);
sensArray[6] = analogValue;
delay(1000);

servoAng = servoAng + 10;
servoMotor.write(servoAng);
sensArray[7] = analogValue;
delay(1000);

servoAng = servoAng + 10;
servoMotor.write(servoAng);
sensArray[8] = analogValue;
delay(1000);

servoAng = servoAng + 10;
servoMotor.write(servoAng);
sensArray[9] = analogValue;
delay(1000);

servoAng = servoAng + 10;
servoMotor.write(servoAng);
sensArray[10] = analogValue;
delay(1000);

servoAng = servoAng + 10;
servoMotor.write(servoAng);
sensArray[11] = analogValue;
delay(1000);

servoAng = servoAng + 10;
servoMotor.write(servoAng);
sensArray[12] = analogValue;
delay(1000);

servoAng = servoAng + 10;
servoMotor.write(servoAng);
sensArray[13] = analogValue;
delay(1000);

servoAng = servoAng + 10;
servoMotor.write(servoAng);
sensArray[14] = analogValue;
delay(1000);

servoAng = servoAng + 10;
servoMotor.write(servoAng);
sensArray[15] = analogValue;
delay(1000);

servoAng = servoAng + 10;
servoMotor.write(servoAng);
sensArray[16] = analogValue;
delay(1000);

/* for (int servoAng = 0; servoAng <= 180; servoAng = servoAng+10) { servoMotor.write(servoAng); delay(1000); // this value is important because there is a minimum time the servo needs to return to it's point of origin }end for delay(10000); // set this delay to control reevaluation interval }// end loop

Having already informed my lab partner Laura C. that I am to be beat about the head and shoulders for such madness, I realized I needed to rethink my plan.

I decided to use an Array, to store the analog value. Using the Servo Degree (degCount) as the driver of the element indexing. This saved me from having to negotiate a multi-dimensional array. I Got some much needed clarity from
Craig Kapp and came up with this:





#include // include the servo library

Servo servoMotor; // creates an instance of the servo object to control a servo

int analogPin = 0; // the analog pin that the sensor is on
int analogValue = 0; // the value returned from the analog sensor

int servoPin = 9; // Control pin for servo motor, must be a PWM pin
// the value representing the angle of the servo


void setup()
{

servoMotor.attach(servoPin); // attaches the servo on pin 2 to the servo object

} //end setup

void loop()
{
/* Survey */
int capAng[180]= {0};
int degCount;

for (int degCount = 0; degCount <= 180; degCount++) { servoMotor.write(degCount); capAng[degCount] = analogRead(analogPin); delay(15); } //end suurvey int brightVal=0; int brightestDeg=0; for (int degCount = 0; degCount <= 180; degCount++) { if(capAng[degCount] > brightVal)
{
brightVal= capAng[degCount];
brightestDeg= degCount;
}//end if that evaluates brightest value and corresponding Angle
}// end analysis for loop

servoMotor.write(brightestDeg);
delay(10000);
}// end Loop
















No comments:

Post a Comment