Untitled
unknown
plain_text
2 years ago
3.7 kB
11
Indexable
// lab #3 Q3
// note: other light sources such as computer monitor, TV, and other light bulbs
// can produce somewhat unexpected maximums -- the light sensor might thus
// not perfectly point at your LED light unless you turn them off.
// the tilt of the light sensor can also affect the results.
// note: the first method seems faster and more simple but it doesn't follow small
// changes in the position of the light as well as the second method. it also has
// to move far away from the light during the sweep of all angles.
// In practice a combination of the two methods might be the best approach
// or maybe some other method like the first method but over a smaller
// range in some situations so it tracks better.
#include <Servo.h> // include file for servo control //UNDERSTOOD
Servo servo1; // create servo object to control servo #1 //UNDERSTOOD
void setup() {
servo1.attach(7); // connect pin 7 to servo #1 //UNDERSTOOD
// initialize serial communication at 115200 bits per second
Serial.begin(115200);
}
// first method -- a sweep over all angles
void loop() {
// note this program could also be put in setup() after Serial.begin
// note this loop function gets executed repeatedly unlike Q1
// -- see if you can spot the difference
int theta1_d; // desired servo angle //Q1:isn't that suppose to be a float value ?
int analog_input0; // analog input for pin A0 //UNDERSTOOD
float voltage0; // voltage input for pin A0 //UNDERSTOOD
float min; //Q2: why do we need this part ?
int theta1_d_min; // servo angle for minimum voltage / maximum light //UNDERSTOOD
float t; //UNDERSTOOD
// move to the initial position of 0 deg
theta1_d = 0; //UNDERSTOOD
servo1.write(theta1_d); //UNDERSTOOD
delay(1000); // wait for the servo to get to theta1 = 0 deg //UNDERSTOOD
// move servo #1 between 0 and 180 deg in 1 deg increments //UNDERSTOOD
// find the minimum voltage (i.e. maximum light)
min = 1.0e6; // bad minimum //Q3: what do you mean by bad minimum and why this value ?
for(theta1_d = 0;theta1_d<=180;theta1_d++) {
servo1.write(theta1_d); // move servo #1 to desired angle //UNDERSTOOD
delay(50); // wait for servo to get to desired angle //UNDERSTOOD
// smaller delays will move the servo faster but with more error //UNDERSTOOD
// ie difference between theta1 (actual) and theta1_d (desired) //UNDERSTOOD
analog_input0 = analogRead(A0); // read the analog input for pin A0 //UNDERSTOOD
voltage0 = analog_input0/1023.0*5; // convert input to V (0 to 5V) //UNDERSTOOD
// approximate value of t -- should use t0 as in Q2 solution
t = micros()*1.0e-6; //UNDERSTOOD
if(voltage0 < min) { //UNDERSTOOD
min = voltage0; //UNDERSTOOD
theta1_d_min = theta1_d; // also record theta1_d for min //UNDERSTOOD
}
// print out for testing/plotting purposes -- comment out for
// better performance
Serial.print("\n"); //UNDERSTOOD
Serial.print(t); //UNDERSTOOD
Serial.print(","); // for csv file //UNDERSTOOD
Serial.print(voltage0); //UNDERSTOOD
Serial.print(","); // for csv file //UNDERSTOOD
Serial.print(theta1_d); //UNDERSTOOD
}
// move to the min voltage / max light position
servo1.write(theta1_d_min); //Q4: what value is this exactly, in the loop we basicaly never use it except if the voltage is below min value
delay(1000); // wait for the servo to get to theta1_d_min //UNDERSTOOD
// wait 10s before beginning the maximizing procedure again
delay(10000); //UNDERSTOOD
}
Editor is loading...
Leave a Comment